Android: how to maintain aspect ratio in animations

The animation that I run inside the image refuses to maintain the aspect ratio of the image frames. The following answers in SO are pretty informative, but don't seem to work for me: How to scale an image in ImageView to maintain 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 a list of animations:

 <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> 
+8
android aspect-ratio android-animation
source share
2 answers

Instead of tweaking the animation in the background of the image, set it in the foreground with src and let the animation play there. All images in the frame animation will be changed while maintaining the aspect ratio if you set the appropriate scale type for the image.

  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(); } 
+10
source share

It is a bit complicated, but it works. I have two pictures in my animation list

 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false"> <item android:drawable="@drawable/logo1" android:duration="5000" /> <item android:drawable="@drawable/logo2" android:duration="300" /> </animation-list> 

Then I added a third image (logo0), which is the same size as the 1/2 logo, but completely transparent. Finally, I use this ImageView:

 <ImageView android:id="@+id/imageViewLogo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:adjustViewBounds="true" android:layout_margin="5dp" android:src="@drawable/logo0" /> 

Now my animation saves the aspect ratio of my image. *.

The code:

  @Override public void onCreate(Bundle savedInstanceState) { ... imageView = (ImageView) findViewById(R.id.imageViewLogo); imageView.setBackgroundResource(R.drawable.logo_animation); ... public void onWindowFocusChanged (boolean hasFocus) { super.onWindowFocusChanged(hasFocus); AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground(); if(hasFocus) { frameAnimation.start(); } else { frameAnimation.stop(); } } 

It is very simple, and you only need additional dummy pictures for your resources: no additional code, complex calculations.

0
source share

All Articles