Android AnimationDrawable scaling - android

I have an ImageView that initially has a Bitmap Image:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
By default that imageview scales the bitmap to fill the width that is what I want.
But the problem is that when I change the source of the ImageView to an AnimationDrawable, that is build by several images of the same size of the original, then animation isn't scaled at all and returns to the original.
How can I do that scale the animation drawable in the same way that is scaled the bitmap?

I dealt with this programmatically by using ImageView setPadding(). I calculated the difference between the ImageView height and width and what I wanted to display and set my own padding so that the usable size of the ImageView exactly matched my animation.
It's brute force and not very flexible but I've not seen any better solution posted and this issue has come up many times.

Related

how to scale down the image from drawable folder in android?

I am setting a imgage in an ImageView. I want to scale down the image size without reducing the imageview size. Please guide me step by step.
Use scaleType parameter for your imageviews and set the value to centerFit or centerCrop. More here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Taken from this link: source
You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.

Image View doesn't show clear image defined in xml

In my code I want to set a default image for my imageview. All my images are of 174px X 174px. So, I want the default image to be same size. This is my xml for image view
//....code
<ImageView
android:id="#+id/ivCover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="Default Image"
android:src="#drawable/default_cover" />
//....some other code
Now, my default_cover.jpg is also 174px X 174px. But it shows a smaller image. I've tried all
android:scaleType="centerInside"
android:scaleType="center"
android:scaleType="matrix"
even when most of them were senseless for me but still I tried all of 'em & none of them works. Then I specified the height & width of image by specifying
android:layout_width="174px"
android:layout_height="174px"
(I know using dp is better than px but because this image size is fixed & I was just testing it). So it shows
[default image]
while when loaded from an external url using
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url_source_for_image).getContent());
ivCover.setImageBitmap(bitmap);
desired image
[]
The two pictures may not look very different here but the difference is very clear on my android device. And please don't say that I didn't try other solutions..
Here is a list of links of so itself..
ImageView fit without stretching the image
Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?
ImageView one dimension to fit free space and second evaluate to keep aspect ration
Resizing ImageView to fit to aspect ratio
Maximum width and height for ImageView in Android
Image in ImageView is stretched - Android
Android: How to prevent image from being scaled in ImageView or ImageButton?
Sounds like the answer is to use drawable-nodpi. For future reference, if you have the same named image in both the ldpi/hdpi/etc and in no-dpi, I think the no-dpi one will be ignored.

Setting a Bitmap to a ImageView

I have downloaded a Bitmap from a WebService, and set it to an ImageView. The ImageView has a set height of 120dip and a width set to FILL_PARENT, so it fills the complete width of the screen. Now i want to stretch the Bitmap to fit the width of the ImageView, but the if the height of the Bitmap is greater then the height of the ImageView, i want the remainder of the image to flow off the top and bottom of the ImageView. So in way, like i have a frame infront of the image and the only portion of the image visible is the contents of the frame. How can i do this ?
See Android Developers on ImageView ScaleType
You are interested in the scale_type enum. Use CENTER_CROP.
Here is complete example:
<ImageView android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/eureka"
android:scaleType="centerCrop">
</ImageView>
Here is also a cool visualization of all possible options for ImageView.

Android Animation in ImageView is stretched vertically

Everything looks perfect when I use the ImageView to show a png image like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/png_picture"
android:src="#drawable/paos_connection"/>
The image is scaled perfectly, fills the screen horizontally and has the correct aspect ratio.
Now, I want to replace the image with an animation. I decided on using Frame Animation as shown here: http://developer.android.com/guide/topics/graphics/view-animation.html. I am using the same size png files (slightly modified copies of the original one).
My problem is that the animation is playing and it is filling up the screen horizontally as before, but it is now stretched vertically. I can force it to have the correct height by hard-coding a height:
<ImageView
android:layout_width="fill_parent"
android:layout_height="210px"
android:id="#+id/animation" />
...but obviously this is not a good solution because the image should have the correct aspect ratio on all screen sizes.
This may help. I've found without a lot of footwork, you're stuck using a fixed element unless the image is drawn in order with the rest of the layout on initialization.
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
A quick hack to try would be to set a transparent image in its place with the same dimensions as the animated image. Programatically create an ImageView using the width & height of the rendered transparent image. Following that, animate the the instantiated image.
Instead of setting the animation drawable in the background of the imageview, set it in the foreground using src and let the animation play there. All images in the frame animation will be resized with aspect ratio intact provided you set a suitable scale type for the imageview.
Althought the question is 11 years old, for those who search:
ImageView img_anim;
img_anim = view.findViewById(R.id.img_anim);
img_anim.setImageResource(R.drawable.anim_youranimation);
AnimationDrawable frameAnimation = (AnimationDrawable) img_anim.getDrawable();
frameAnimation.start();
instead of
ImageView img_anim;
img_anim = view.findViewById(R.id.img_anim);
img_anim.setBackgroundResource(R.drawable.anim_youranimation);
AnimationDrawable frameAnimation = (AnimationDrawable) img_anim.getBackground();
frameAnimation.start();

How to create an ImageView that fills the parent height and displays an Image as big as possible?

I have an ImageView that is defined in the following way:
<ImageView
android:id="#+id/cover_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_above="#id/divider"
android:adjustViewBounds="true"
android:src="#drawable/image_placeholder"
android:scaleType="fitStart"/>
Now after downloading a new bitmap I change the drawable. The image now appears in the top left corner of the ImageView. Is there a way to have the image fill up the whole height that is possible and then adjust the width of the view to enable scaling the image without changing the ascpect ratio?
The image fills up all the space on a standard screen but on a WVGA Resolution the image takes only about half of the actual height of the ImageView.
If I'm understanding you correctly, what you need to use is the centerCrop scaleType. fitStart scales the image proportionally, but neither the width nor height will exceed the size of the view, and the image will, as you said, have a top|left gravity.
Using centerCrop scales the image proportionally, but causes the shortest edge of the image to match the size of the view, and if there is additional data on the long side that does not fit, it is simply cropped off. The gravity is, of course, center. The below worked for me:
<ImageView
android:src="#drawable/image_placeholder"
android:id="#+id/cover_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
/>
You can change scale type to fitXY via call to
imageView.setScaleType(ScaleType.FIT_XY);
simply do it in xml like
android:scaleType="fitXY"
Basically the answer here is that there is no predefined value for what you are trying to achieve. The solution is to create a Matrix that fits to your needs and call setImageMatrix(matrix) on your ImageView.

Categories

Resources