Android图片动画播放(AnimationDrawable)

Android图片动画播放(AnimationDrawable)

您的评价:
         

Android图片动画播放(AnimationDrawable)

大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个

AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间,本例子中间隔时间为5S,

最后通过start 方法就可。

以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。

01 package xiaosi.bu;
02  
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;
10  
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;
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21          
22         start = (Button)findViewById(R.id.start);
23         start.setOnClickListener(new StartListener());
24         stop = (Button)findViewById(R.id.stop);
25         stop.setOnClickListener(new StopListener());
26          
27         image = (ImageView)findViewById(R.id.imageview);
28          
29         animationDrawable = new AnimationDrawable();
30         for(int i =0;i<8;i++){
31             //第一个 就是我们的资源名称(图片名) 
32             //第二个 就是我们存放图片的文件夹drawable 
33             //第三个 包名也可以用Context的getPackageName返回应用程序的包名 
34             int id = getResources().getIdentifier( "a"+i, "drawable""xiaosi.bu");
35             System.out.println("ID:" + id);
36             animationDrawable.addFrame(getResources().getDrawable(id), 2000);
37         }
38       //设置手否重复播放,false为重复
39         animationDrawable.setOneShot(false);
40         image.setImageDrawable(animationDrawable);
41  
42     }
43     private class StartListener implements OnClickListener{
44  
45         public void onClick(View v)
46         {
47             animationDrawable.start();
48         }
49     }
50      
51     private class StopListener implements OnClickListener{
52  
53         public void onClick(View v)
54         {
55             animationDrawable.stop(); 
56         }
57     }
58 }
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">
06        <LinearLayout
07         android:orientation="horizontal"
08         android:layout_width="fill_parent"
09         android:layout_height="wrap_content">
10        <Button android:id="@+id/start"
11            android:text="Start"
12            android:layout_width="wrap_content"
13            android:layout_height="wrap_content"/>
14        <Button android:id="@+id/stop"
15            android:text="End"
16            android:layout_width="wrap_content"
17            android:layout_height="wrap_content"/>
18        </LinearLayout>
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" />
24  </LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值