Android动画效果(帧动画)
首先在res下创建一个anim文件,在文件下创建一个.xml文件类型为animation-list
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 3 android:oneshot="false" > 4 5 <item 6 android:drawable="@drawable/girl_1" 7 android:duration="200"/> 8 9 <item 10 android:drawable="@drawable/girl_2" 11 android:duration="200"/> 12 13 <item 14 android:drawable="@drawable/girl_3" 15 android:duration="200"/> 16 17 <item 18 android:drawable="@drawable/girl_4" 19 android:duration="200"/> 20 21 <item 22 android:drawable="@drawable/girl_5" 23 android:duration="200"/> 24 25 <item 26 android:drawable="@drawable/girl_6" 27 android:duration="200"/> 28 29 <item 30 android:drawable="@drawable/girl_7" 31 android:duration="500"/> 32 33 <item 34 android:drawable="@drawable/girl_8" 35 android:duration="200"/> 36 37 <item 38 android:drawable="@drawable/girl_9" 39 android:duration="200"/> 40 41 <item 42 android:drawable="@drawable/girl_10" 43 android:duration="200"/> 44 45 <item 46 android:drawable="@drawable/girl_11" 47 android:duration="200"/> 48 49 </animation-list>
layout文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/iv" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:background="@drawable/anim" /> 12 13 <Button 14 android:id="@+id/button1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:onClick="click" 18 android:text="播放动画" /> 19 20 </LinearLayout>
Activity代码
1 package cn.itcast.frame; 2 3 import android.app.Activity; 4 import android.graphics.drawable.AnimationDrawable; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.ImageView; 8 9 public class DemoActivity extends Activity { 10 ImageView rocketImage; 11 AnimationDrawable animationDrawable; 12 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 18 rocketImage = (ImageView) findViewById(R.id.iv); 19 rocketImage.setBackgroundResource(R.drawable.anim); 20 animationDrawable = (AnimationDrawable) rocketImage.getBackground(); 21 } 22 23 public void click(View view) { 24 animationDrawable.start(); 25 } 26 }