一聚教程网:一个值得你收藏的教程网站

热门教程

Android利用animation-list实现帧动画

时间:2022-06-25 23:07:38 编辑:袖梨 来源:一聚教程网

本文实例为大家分享了利用animation-list实现帧动画的具体代码,供大家参考,具体内容如下

将要顺序播放的图片放在资源目录下

再drawable目录下新建animation1文件和animation2文件  一个是按顺序显示动画,一个是倒序显示动画,

顺序显示动画文件:animation1.xml

 
 
 
   
   
   
   
   
   
 

倒序显示动画文件:animation2.xml

 
 
 
   
   
   
   
   
   
 

布局文件

 
 
   
    
       
  

Activity文件

package org.shuxiang.test; 
 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class Activity10 extends Activity 
{ 
  private ImageView animationIV; 
  private Button buttonA, buttonB, buttonC; 
  private AnimationDrawable animationDrawable; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.test10); 
     
     
    animationIV = (ImageView) findViewById(R.id.animationIV); 
    buttonA = (Button) findViewById(R.id.buttonA); 
    buttonB = (Button) findViewById(R.id.buttonB); 
    buttonC = (Button) findViewById(R.id.buttonC); 
     
    buttonA.setOnClickListener(new OnClickListener() 
    { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        animationIV.setImageResource(R.drawable.animation1); 
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        animationDrawable.start(); 
      } 
       
    });  
     
    buttonB.setOnClickListener(new OnClickListener() 
    { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        animationDrawable.stop(); 
      } 
       
    }); 
     
    buttonC.setOnClickListener(new OnClickListener() 
    { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        animationIV.setImageResource(R.drawable.animation2); 
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        animationDrawable.start(); 
      }       
    });     
  } 
} 

热门栏目