Android图片动画播放(AnimationDrawable)
大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个
AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间,本例子中间隔时间为5S,
最后通过start 方法就可。
以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。
03 | import android.app.Activity; |
04 | import android.graphics.drawable.AnimationDrawable; |
05 | import android.os.Bundle; |
06 | import android.view.View; |
07 | import android.view.View.OnClickListener; |
08 | import android.widget.Button; |
09 | import android.widget.ImageView; |
11 | public class TupianActivity extends Activity { |
12 | /** Called when the activity is first created. */ |
13 | private Button start = null ; |
14 | private Button stop = null ; |
15 | private ImageView image = null ; |
16 | private AnimationDrawable animationDrawable = null ; |
18 | public void onCreate(Bundle savedInstanceState) { |
19 | super .onCreate(savedInstanceState); |
20 | setContentView(R.layout.main); |
22 | start = (Button)findViewById(R.id.start); |
23 | start.setOnClickListener( new StartListener()); |
24 | stop = (Button)findViewById(R.id.stop); |
25 | stop.setOnClickListener( new StopListener()); |
27 | image = (ImageView)findViewById(R.id.imageview); |
29 | animationDrawable = new AnimationDrawable(); |
30 | for ( int i = 0 ;i< 8 ;i++){ |
34 | int id = getResources().getIdentifier( "a" +i, "drawable" , "xiaosi.bu" ); |
35 | System.out.println( "ID:" + id); |
36 | animationDrawable.addFrame(getResources().getDrawable(id), 2000 ); |
39 | animationDrawable.setOneShot( false ); |
40 | image.setImageDrawable(animationDrawable); |
43 | private class StartListener implements OnClickListener{ |
45 | public void onClick(View v) |
47 | animationDrawable.start(); |
51 | private class StopListener implements OnClickListener{ |
53 | public void onClick(View v) |
55 | animationDrawable.stop(); |
main.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" > |
07 | android:orientation = "horizontal" |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" > |
10 | < Button android:id = "@+id/start" |
12 | android:layout_width = "wrap_content" |
13 | android:layout_height = "wrap_content" /> |
14 | < Button android:id = "@+id/stop" |
16 | android:layout_width = "wrap_content" |
17 | android:layout_height = "wrap_content" /> |
19 | < ImageView android:id = "@+id/imageview" |
20 | android:layout_width = "fill_parent" |
21 | android:layout_height = "fill_parent" |
22 | android:scaleType = "fitXY" |
23 | android:background = "#ffffff" /> |