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();
Related
I have an imageview that has wrap_content for the width and height. I want to add animationDrawable.
Should I use setBackgroundResource() or setImageResource()?
I have searched SO for differences and it talks about possibly stretching in case of background. However my image is wrap_content, so would there be any difference?
Thank you
Unless you want the the ImageView to retain the size that you initially gave it (by its current android:src attribute drawble) use setImageResource().
If you apply the animation drawable to the background via setBackgroundResource(), the animation drawable WILL scale to fit exactly in the size of the ImageView. And unless that animation drawable matches the aspect ratio of the ImageView dimensions (very unlikely) it will not look uniform and appear stretched.
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.
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src
I have made a set of 3 images that will represent the move of a animal. Each image represents a frame and consists only in the animal with transparent background.
I have made a AnimationDrawable in code and created the animation:
AnimationDrawable staticAnimation = new AnimationDrawable();
staticAnimation.addFrame(ctx.getResources().getDrawable("frame1"),150);
staticAnimation.addFrame(ctx.getResources().getDrawable("frame2"),150);
staticAnimation.addFrame(ctx.getResources().getDrawable("frame3"),150);
I set the animation as a background for a Imageview and start it
imgAnimal.setBackgroundDrawable(staticAnimation );
staticAnimation.start()
Because the frames have different width (the drawing needs to recreate steps move) looks like the Imageview resizes the images in the animation. Better to say, if frame1 is bigger then frame2, frame 1 is displayed ok but frame 2 is stretched to be as big as frame1 was. Looks really awful.
Any suggestions on how to stop this behavior ? Thank you.
The solution was quite simple, because images in frames have same height but different width, I simply made a transparent border rectangle and put it over each frame. In this case each frame will have the same width and the animatin will run as supposed to.
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.