`
dcj3sjt126com
  • 浏览: 1825617 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android逐帧动画的实现

 
阅读更多

一、代码实现:

private ImageView iv;
	private AnimationDrawable ad;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv = (ImageView) findViewById(R.id.iv);
		
		ad = new AnimationDrawable();
		ad.addFrame(getResources().getDrawable(R.drawable.pic1), 100);
		ad.addFrame(getResources().getDrawable(R.drawable.pic2), 100);
		ad.addFrame(getResources().getDrawable(R.drawable.pic3), 100);
		ad.addFrame(getResources().getDrawable(R.drawable.pic4), 100);
		ad.addFrame(getResources().getDrawable(R.drawable.pic5), 100);
		ad.setOneShot(false);//true则只运行一次,false可以循环
		
		iv.setBackgroundDrawable(ad);
		iv.setOnClickListener(new View.OnClickListener()//按钮点击的时候运行,再次点击停止
		{
			
			@Override
			public void onClick(View v)
			{
				if (ad.isRunning())
				{
					ad.stop();
				} else {
					ad.start();
				}
			}
		});
	}

 

第二种,配置文件的实现方式

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/ic_launcher" android:duration="100"></item>
    <item android:drawable="@drawable/newsdetails_titlebar_btn_next" android:duration="100"></item>
    <item android:drawable="@drawable/newsdetails_titlebar_btn_next_selected" android:duration="100"></item>
    <item android:drawable="@drawable/newsdetails_titlebar_btn_previous" android:duration="100"></item>
    <item android:drawable="@drawable/newsdetails_titlebar_btn_previous_selected" android:duration="100"></item>
    
</animation-list>

 代码调用部分:

private ImageView iv;
	private AnimationDrawable ad;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv = (ImageView) findViewById(R.id.iv);
		
		iv.setBackgroundResource(R.drawable.pic_anim);
		ad = (AnimationDrawable) iv.getBackground();
		
		iv.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				if (ad.isRunning())
				{
					ad.stop();
				} else {
					ad.start();
				}
			}
		});
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics