1.在/res文件夹下创建一个drawable文件夹,然后创建一个animation-list 的xml文件,比如girl.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/girl_1" android:duration="200"/> <item android:drawable="@drawable/girl_2" android:duration="200"/> <item android:drawable="@drawable/girl_3" android:duration="200"/> <item android:drawable="@drawable/girl_4" android:duration="200"/> <item android:drawable="@drawable/girl_5" android:duration="200"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_8" android:duration="200"/> <item android:drawable="@drawable/girl_9" android:duration="200"/> <item android:drawable="@drawable/girl_10" android:duration="200"/> <item android:drawable="@drawable/girl_11" android:duration="200"/> </animation-list>
2.然后在layout中创建一个 xml文件,xml文件中有ImageView控件。比如activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iamgeview" /> </RelativeLayout>
3.然后把animation-list 所在的xml文件,设置到imageview上面。MainActivity.java
public class MainActivity extends Activity { ImageView imageview; AnimationDrawable animation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview = (ImageView) findViewById(R.id.iamgeview); //获取动画资源 imageview.setBackgroundResource(R.drawable.girl); //开启动画 animation = (AnimationDrawable) imageview.getBackground(); } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ animation.start(); return true; } return super.onTouchEvent(event); } }