I have a layout that always rotate to the north. I want to put a pointer on it, but pointer should not rotate. how can I do that?
this is my code of Layout:
rll = (RelativeLayout) findViewById(co.iman.R.id.relativeLayout1);
rlllp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlllp.setMargins(190, 200, 200, 0);
and in other method I rotate the layout with:
rll.setRotation(mCurrentDegree);
I want put a pic or button exactlly in center of this layout? and I want to move this button independently.
Try making another RelativeLayout inside your first one, and do something like this:
rll.setRotation(mCurrentDegree);
rll2.setRotation(-mCurrentDegree);
Now the inside layout should be set to -10 degrees when its parent is set to 10, so it will look like it is not moving at all. Hope that helps!
Related
I want to display an image, but this image will reveal itself from top to bottom slowly. You can imagine that if this image is a text, then this text will be revealed line by line. And each line is revealed by the fade effect. Is there anyway to do this ? I don't even know which term should I search for.
There is one way I know to do that and it is just simple and it's by using the class called TransitionManager.
Here's a simple code you can put on your onCreate and test it.
final ImageView image = new ImageView(this);
image.setImageResource(R.mipmap.ic_launcher);
// Set the initial position of the image
RelativeLayout.LayoutParams imageDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
imageDetails.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageDetails.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
final ViewGroup layout = (ViewGroup)findViewById(R.id.mainLayout);
layout.setOnTouchListener(new ViewGroup.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
// Use the default transition of TransitionManager
TransitionManager.beginDelayedTransition(layout);
// Set the final position of the image
RelativeLayout.LayoutParams imageDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
imageDetails.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageDetails.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
image.setLayoutParams(imageDetails);
return true;
}
});
layout.addView(image, imageDetails);
There is one disadvantage that I know when using this approach. The minimum SDK version should be 19.
PS: If you want more control on the transitions or animations you make. Just read further on animations and transitions on Android.
OK, if you want to move the view from top to down, you could just use an animator to animate that. If you want to Reveal the image from top to bottom, then you can do that with a trick. Have an image view and load your image in it. Now have another view on top of it. All you have to is animate the height of that view until, the top coincides with the bottom. You will have your reveal animation.
If you want to create the reveal with fade effect, then you can mask the view on top and animate the mask. I used this in the past to create the circular reveal. For the mask view take a look at this : https://github.com/christophesmet/android_maskable_layout
I have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)
I've a big problem I'm trying to add an Imageview programmatically but I face a strange problem:
I've put in the XML just an empty Relative Layout matching the parent.
Code wise I'm adding an imageview to the relative layout.
Everything works, the imageview is visible and sized correctly, but the problem is that the "0,0" position is weird. It seems that it has a Top Margin while instead the Left Margin is correctly 0. Also forcing the position to "0,0" with SetX/SetY results with this wierd top border:
Image:
Code:
base_layout = (RelativeLayout) findVieById(R.id.base_layout);
image_logo = New Imageview(this);
base_layout.AddView(image_logo);
image_logo.SetPadding(0,0,0,0);
image_logo.SetX(0);
image_logo.SetY(0);
image_logo.SetImageResource(R.drawable.generic_image);
image_logo.SetVisibility(View.VISIBLE);
I don't understand why..the activity in the manifest has its usual Theme.NoTitleBar.Fullscreen flag.
Any help? It will be really appreciated!
I put together an example using RelativeLayout.LayoutParams and tried it myself and everything appears to be working fine.
base_layout = (RelativeLayout) findViewById(R.id.base_layout);
ImageView image_logo = new ImageView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
image_logo.setLayoutParams(lp);
base_layout.addView(image_logo);
The above shows the below effect when run.
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" was used in the manifest against this Activity.
I have an image with an item in the lower left. I would like at the bottom center, and not attached at the lower end but slightly raised. How can I do? Working with some of the calculations on the size of the screen? Obviously I mean in code, not xml. thanks
have a look at LayoutParams.
for exmaple
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
you can do all differnt alignments, params, margins and padding as in xml
i need to draw two imageviews, (+) and (-) symbols, on the bottom right corner of the screen, similar of the zoom objects from googlemaps.
I need to do it programatically, with Java, and without using XML files.
I'm trying to do with relativelayout, but i dont know how to do it. They must to be on the bottom right corner of the screen, with 5 or 10 pixels of separation between them.
How to do it?
Also will be cool if someone can tell me how to detect when a user has pressed each image.
You can use gravity.
http://developer.android.com/reference/android/widget/RelativeLayout.html#setGravity(int)
this might do it:
image.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
This should work
ImageView plusImage = new ImageView(this);
RelativeLayout.LayoutParams pp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
pp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
pp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
pp.leftMargin = 5;
plusImage.setId(501);
plusImage.setLayoutParams(pp);
plusImage.setImageResource(R.drawable.icon);
ImageView minusImage = new ImageView(this);
RelativeLayout.LayoutParams mp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mp.addRule(RelativeLayout.LEFT_OF,plusImage.getId());
mp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
mp.rightMargin = 5;
minusImage.setId(502);
minusImage.setLayoutParams(mp);
minusImage.setImageResource(R.drawable.icon);
//Add the images to the outer layout
((RelativeLayout)findViewById(R.id.outerlayout)).addView(plusImage);
((RelativeLayout)findViewById(R.id.outerlayout)).addView(minusImage);