image animation cropped by layout bounds - android

I have an image that gets scaled from large to small as it spins. It's mean to be a fancy animation to simulate a tile being place on a board. The animation works as expected except for one thing: This image is cropped.
Each row of tiles is inside of a Horizontal LinearLayout. Each LinearLayout is inside of another LinearLayout (Vertical). This give the board. Sadly, the image animation is getting cropped to not excede the bounds of the parent layout. I don't want that. I want the image to be displayed and animated as if it's on top of everything else. I want it to exceed the layout.
Any idea how to fix this?
Here's a couple of screenshots:

Your image is constrained to fit within it's parent view as per design. You might want to use a canvas to hold all your tiles instead (keep in mind the bounds of the canvas for the corners otherwise you will have the same issue), or look into other layouts that might be able to accommodate your needs.
From: http://developer.android.com/guide/topics/graphics/view-animation.html
Note: Regardless of how your animation may move or resize, the bounds of the View that holds your animation will not automatically adjust to accommodate it. Even so, the animation will still be drawn beyond the bounds of its View and will not be clipped. However, clipping will occur if the animation exceeds the bounds of the parent View.

Related

Android special layout

I need to create a layout in Android like the image below:
The red rectange is the viewport. it should be possible to zoom in and pan until the borders of the image that contains the 4 imageviews.
I've tried putting the images in a gridview, that didn't really work
After that i tried putting them in a tablelayout, but the images have to keep aspect ratio.
so each row in the tablelayout had the correct width but the height was only half the rowview.
So when i pan it's possible to see the white edges from the rowview that was not filled completely
What would be the best way to achieve this layout?

Scale down ImageButton / ImageView without leaving space around

I have some problems resizing ImageViews and ImageButtons.
Let's say that I have a Layout that has a rectangular shape (I don't want to know if it is a horizontal or vertical rectangle) and a ImageButton that contains a transparent background and as ImageResource a square image.
I want to keep the button square, so I use setScaleType(ScaleType.FIT_CENTER) to stretch the button. It works well.
The problems come when the button needs to be REDUCED to fit the rectangular layout, instead of stretched: in that case, the image is reduced correctly, but the space reserved in the layout is the one that would be reserved by the image if I hade made it crop.
This is what I think that happens:
the image is put in the layout
the space in the layout is reserved
AFTER THIS the image is resized
if the space asked is increased, the layout is enlarged, otherwise nothing is done
as a consequence in the layout the image results rounded by A LOT of empty space if the image needed to be reduced.
The classical problem is: I have a layout that should contain one row with - say - six square buttons. IF the button size is larger than the height of the Horizontal LinearLayout, the buttons end to be distantiated with a lot of empty space, instead of touching them.
I tried using fixed sizes for the images, to force them resize before putting them in the layout, but this is not a solution for me. First of all I want it to be dynamic (ie: the layout could change size during the app lifetime and the images should follow that). Second of all, when I put the image into the layout it can easily happen that the layout is not set yet and its size returns zero.
Can anyone help me?
Thank you.
Just add the attribute
android:adjustViewBounds="true"
to your image view element in your layout. You can have a look to the post Unwanted padding around an ImageView

Android: creating custom 2D avatar

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.

Android: How do you scale multiple views together?

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();

Android: Stretching ImageView with background

I have a landscape layout that features a vertical LinearLayout of buttons on the left side of the screen and a user-defined picture on the right of the screen. The design I'm working from calls for a double-stroke border around it, which I implemented by creating a rectangular shape background with the border being the outer color and the background of the shape being the inner color. I then just put some padding around the picture, and you get the double-stroke border. The problem is expanding the picture to fill the space in the layout. I don't know the dimensions of the picture, since it is user defined, and I'd like it to expand to exactly fill either dimension while preserving the aspect ratio. Setting fill_parent for both width and height does that, but it also expands the background all the way to completely fill the cell, resulting in a sort of letter boxing effect. Is there any way, short of just adjusting the size of the view after layout, of getting this to only expand the view as much as necessary?
I never got this to work properly, and just ended up adjusting my layout to a different arrangement.

Categories

Resources