帧动画的两种使用

1.代码中的应用。

public class MainActivity extends Activity {

	private ImageView im;
	private AnimationDrawable animationDrawable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		im = (ImageView) findViewById(R.id.im);
		
	}
	//开始动画的按钮
   @SuppressLint("NewApi")
   //在点击事件中实现动画效果
public void jump(View v){
	   animationDrawable = new AnimationDrawable();
	   //
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g1), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g2), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g3), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g4), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g5), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g6), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g7), 2000);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g8), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g9), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g10), 200);
	   animationDrawable.addFrame(getResources().getDrawable(R.drawable.g11), 200);
	   //给imageView 设置展示动画的资源
	   im.setBackground(animationDrawable);
	   animationDrawable.start();
	   
   }
public void jump1(View v){
	animationDrawable.stop();
}
}
2.第二种方法。xml中实现,首先创建一个drawable文件夹。在文件中创建立一个animation-list的xml文件,内容如下:


   
   

   
   
    
    
    
     
    
    
      
    
    
       
    
    
        
    
    
         
    
    
          
    
    


   
   
代码实现如下:

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView im;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		im = (ImageView) findViewById(R.id.im);
	}

	public void jump(View v){
		
		
		
		im.setBackgroundResource(R.drawable.wwwww);
		AnimationDrawable background = (AnimationDrawable) im.getBackground();
		background.start();
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值