Android Basic Knowledge of frame Animation Effect

  • 2021-07-24 11:46:26
  • OfStack

In the last article, we talked about tween animation in Android, and in this article, we talked about frame animation. frame animation mainly realizes an effect similar to gif animation, that is, multiple pictures are continuously displayed at a preset time.
Create a new android project, named frameTest, and create a new folder under res folder called anim, where our frame animation xml file is placed.
Create a new frame. xml file in anim as follows:


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
 android:oneshot="false"><!-- true Indicates that only play 1 Times, false Indicates infinite loop playback  -->
 <item android:drawable="@drawable/girl_1" android:duration="100" />
 <item android:drawable="@drawable/girl_2" android:duration="100" />
 <item android:drawable="@drawable/girl_3" android:duration="100" />
 <item android:drawable="@drawable/girl_4" android:duration="100" />
 <item android:drawable="@drawable/girl_5" android:duration="100" />
 <item android:drawable="@drawable/girl_6" android:duration="300" />
 <item android:drawable="@drawable/girl_7" android:duration="400" />
 <item android:drawable="@drawable/girl_8" android:duration="300" />
 <item android:drawable="@drawable/girl_9" android:duration="100" />
 <item android:drawable="@drawable/girl_10" android:duration="100" />
 <item android:drawable="@drawable/girl_11" android:duration="100" />
</animation-list>

Here are 11 pictures. The previous android: oneshot attributes indicate the number of times the animation has been performed, false indicates that the animation has been played repeatedly, true indicates that the animation value has been played once, and duration indicates the time displayed by each picture in milliseconds.

Then look at the code in MainActivity:


public class MainActivity extends Activity {

 private ImageView iv;
 private AnimationDrawable ad;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  iv = (ImageView) this.findViewById(R.id.iv);
//  iv.setBackgroundResource(R.anim.frame);
//  ad = (AnimationDrawable) iv.getBackground();
  // The above two sentences can be replaced by the following two sentences, and the effect is 1 Like 
  iv.setImageResource(R.anim.frame);
  ad = (AnimationDrawable) iv.getDrawable();
 }
 public void start(View v){
  // If ad Is running, just stop it first 
  if(ad.isRunning())
   ad.stop();
  ad.start();
 }
}

Get an ImageView first, then set the frame animation as its background, and finally get the background of this picture and turn it into AnimationDrawable. When clicking on this picture, if the animation is already running, let it stop first, and then run again, otherwise it can be run directly.

Original link: http://blog.csdn.net/u012702547/article/details/45716757


Related articles: