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.
user1222217
source share