The animation I am running inside an imageview refuses to maintain the aspect-ratio of the image frames. The following answers in SO are quite informative, but don't seem to work for me: How to scale an Image in ImageView to keep the aspect ratio
Here is the code:
private void startAnimation(){
mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setBackgroundResource(R.anim.my_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
}
R.anim.my_animation is just an animation list:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected"
android:oneshot="false">
<item
android:drawable="@drawable/photo_1"
android:duration="100" />
<item
android:drawable="@drawable/photo__2"
android:duration="100" />
... and so on...
</animation-list>
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.
private void startAnimation(){
mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setImageDrawable(getResources().getDrawable(R.anim.my_animation));
AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getDrawable();
// Start the animation (looped playback by default).
frameAnimation.start();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With