Android ImageView实现类似Gif动图的效果

本文介绍了在Android中如何使用ImageView实现类似Gif动图效果的帧动画,包括创建animation-list资源文件,设置动画帧及播放控制,并提供了两种不同的显示方式。
摘要由CSDN通过智能技术生成

已经过时的方法:

android studio会报错:

错误行:imageView.setBackgroundResource(R.anim.framebyframe);  

错误为:expected resource of type

stackoverflow描述:http://stackoverflow.com/questions/24716385/android-studios-expected-resource-of-type-checks

错误原因; AS checks that some int parameters are not arbitrary integers, but rather have some properties.

新建一个framebyframe.xml文件

[java]  view plain copy
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:oneshot="false">  
  3.     <item android:drawable="@drawable/an01" android:duration="1000" />  
  4.     <item android:drawable="@drawable/an02" android:duration="1000" />  
  5.     <item android:drawable="@drawable/an03" android:duration="2000" />  
  6. </animation-list>  
  7. <!--   
  8. oneshot :是否只播放一遍动画 true 播放一遍 false 循环播放  
  9. 默认就是false  
  10.  -->  


下边 就是具体代码实现了:

[java]  view plain copy
  1. public class SplashActivity extends Activity {  
  2.   
  3.     private ImageView imageView;  
  4.     private AnimationDrawable animationDrawable;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         // 帧动画  
  12.         imageView = (ImageView) findViewById(R.id.imageView1);  
  13.   
  14.         // 第一种方式实现 动画  
  15.         /*animationDrawable = (AnimationDrawable) getResources().getDrawable( 
  16.                 R.anim.framebyframe); 
  17.         imageView.setBackgroundDrawable(animationDrawable);*/  
  18.   
  19.         // 第二种方式实现动画  
  20.   
  21.         imageView.setBackgroundResource(R.anim.framebyframe);  
  22.   
  23.         animationDrawable = (AnimationDrawable) imageView.getBackground();  
  24.   
  25.         animationDrawable.start();//开始  
  26.         // animationDrawable.setOneShot(false);是否循环播放  
  27.         // animationDrawable.stop();停止播放  
  28.         // animationDrawable.isRunning();//是否播放  
  29.         // animationDrawable.getNumberOfFrames();//播放帧  
  30.         // animationDrawable.getFrame(index); 返回制定索引的 Drawable对象  
  31.         // animationDrawable.getDuration(i);停留的时间  
  32.   
  33.         //延迟三秒 跳转   
  34.         new Handler() {  
  35.             public void handleMessage(android.os.Message msg) {  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值