In Android, I know how to overlay two images and to animate them. The question is how to overlay the second image at a specific location over the first image.
As an example, the first image is the image of an appliance with a power switch, the second image is the power LED that will glow when animated.
I would like to know how I can specify the location of the power LED in the overlay so that it covers the power switch.
One thing you can do is to put both images inside a FrameLayout, with the width/height on both set to wrap_content. The last one you add to the FrameLayout will be on top. Then you can position the two images independently in one of several ways:
1) You could set the layout_gravity of both items to top|left, then set layout_marginTop and layout_marginLeft to position the two images relative to the upper left corner of the FrameLayout.
2) If you want to, say center image B on top of image A, you could set image A to layout_gravity top|left, layout_marginLeft 0, layout_marginTop 0, then set layout_gravity on image B to center. This approach is a bit more robust with respect to running on various devices. You can even use the margins to offset image B with respect to its centered position.
You should get the idea at this point. You can combine layout_gravity along with margins to position the images as you need.
Some caveats:
1) This is not a very efficient way to go about things. If it's just two images in a larger layout, no big deal. But rendering performance will suffer if you do this technique many times in a layout. If that's the case, you should consider creating a custom view instead.
2) You need to explicitly set the layout_gravity on an item contained within a FrameLayout. Otherwise FrameLayout will ignore the layout_marginTop and layout_marginLeft attributes on your child views.
Finally, you can do this same trick with RelativeLayout, using its attributes to position the images in relation to one another. Like FrameLayout, the last view added to the layout shows up on top of the others.
Related
I am looking for an android layout (if there exists) that can do the following.
2 images that occupies the entire width of the screen, stacked on top of each other. A slider say 2 or three pixels wide that allows a user to slide across the width of the screen revealing or hiding portions of the images.
Note that the layout like slidingPane does not work in this scenario as it resizes the contents in each pane rather than cropping them which is what I want.
To better describe the question here are some images.
Not really a layout, but achieves what I was after.
Added images to textureviews(1,2), added one of the textureviews(2) to a linearlayout. Added the texureView1 and linearlayout to a framelayout such that the linearlayout is on top. To achieve the above said effect the linearlayout and the textureView(2) that it holds is moved in opposite directions.
I am creating an app that allows a user to build a custom 2D avatar by specifying things like shoes, socks, skin color, etc...
Currently my solution has been to create a .PNG of each item and then to 'stack' them all on top of each other in a RelativeLayout. So for example, I create an ImageView of two shoes and align the ImageView to the center of the relative layout and the bottom of the Relative Layout. Next I 'stack' the bottom edge of the socks to the top edge of the shoe. And on and on.
This method works, but I feel like I don't have much control over where the parts sit and would much rather be able to calculate the x,y coordinates at run time and place the images that way. For instance, this works well if all of the ImageViews are stacked, but if I need to place one ImageView 10 pixels below the top edge of another ImageView I can't do it (or at least I haven't figured out how yet).
I am looking for a solution that will allow me to control the x,y position of ImageViews and allow ImageView to be offset from each other.
If you'd rather place the images by x,y coordinates, then you should consider using a single view and simply drawing the images on top. See this doc on custom drawing. You'll find drawBitmap and some of its overloads useful.
I have an application where I want to achieve a layout where the product image will be in the background and top of that to the right bottom, I want to place the price of the product and to the left bottom I want to place an add button.
Should I use frame layout or relative layout ???
As a practical rule, I think it's up to you. I personally tend to use RelativeLayouts because they're more flexible, but you can achieve the same effect with either.
This SO post explains the performance differences between the two layouts in more detail: FrameLayout vs RelativeLayout for overlays
Relative layout: When you have relation between siblings or parent.
Frame layout: When siblings are placed independent of each other and are only dependent on parent.
Based on your situation, you can opt in of any of these.
My advice, If you have specific size for your background for each device, then go for relative layout, and set the background to your image. Because when using background in RelativeLayout, It'll fit the size of the relative layout itself, whether the image suits the size of the RelativeLayout or not. (Can be stretched/pixelated/Not properly added)
If you're not sure about specific size, you should use FrameLayout, with ImageView, that handles the ScaleType, which can be centerCrop, and it'll fit the layout in good shape.
And for the TextView, use layout_gravity, which will handle the position based on the parent layout.
I'm working on the controls for a game, and require part of the control panel (gray in the figure below) to change dynamically, either showing a single canvas (left) or 5 buttons (right). The border between the lower-row views should always be positioned at exactly the same x-position as the border between the buttons on the upper row, as shown. At the same time, all twelve upper buttons should be scaled and distributed evenly.
I've considered several approaches, but as of yet none do all of what I want:
Using two LinearLayouts, one for each row of controls: reliably aligning the borders seems to be impossible, and replacing part of the layout is difficult at best.
Using a TableLayout: again, replacing a portion of the layout is difficult.
Using a RelativeLayout: resizing and aligning buttons independently of the screen size doesn't seem possible
Any suggestions for an alternative method, or on how to make one of the above approaches work? It would also be nice if there were some way to animate the change of views, i.e. sliding in the buttons from the left over the canvas. Thanks!
Interesting, I've done this several weeks ago. What I did is to make use of this property of View object: "Visibility". So that means at a fixed position, I can set any View to display on to, not depending on any type of Layout, it can be Visibility.GONE, Visibility.VISIBLE or Visibility.INVISIBLE.
In my app, I used RelativeLayout to set relative position to the right side TextView.
Give it a try :)
In order to close this question: I have solved the problem by writing a custom layout class that places and sizes the child views without heeding the measured size of the children. Effectively this gives me the behavior of a linear layout with layout weights, but is more deterministic with border placement.
A ViewAnimator is used to switch between the Canvas and the Buttons.
I'm trying to layer graphics one top of each other, like an icon over a background, with the second layer (icon) at a certain pixel offset from the top left corner of first layer (background). Since each layer will eventually have its own animation, I'm placing each in its own View.
For my implementation I have two ImageViews, one for each layer, inside a RelativeLayout, which in turn is inside a ScrollView. ImageViews are positioned using layout_margin relative to the top left corner (0,0). The first ImageView is larger than the screen (background), while the second ImageView is smaller than it (icon). ScrollView automatically resizes the first ImageView (background) since it is larger than the screen, it does not resize the second since it is smaller (icon).
I need both of them to scale together, and I also need the positioning of the second layer over the first layer to adjust itself accordingly. This actually works well in a layer-list, but due to the animations I am forced to use Views. How can I scale and position multiple Views together, or do I need to build my own class for something that seems like it should be fairly basic?
Thanks in advance.
I had a similar problem in my android application. Android has introduced new set of API's to help us on this, setScaleX() and setScaleY().
Just call layout.getParent().setScaleX(); and layout.getParent().setScaleY();