当前位置:首页 > 开发 > 移动开发 > 正文

Android逐帧动画的实现

发表于: 2014-05-21   作者:dcj3sjt126com   来源:转载   浏览:
摘要: 一、代码实现: private ImageView iv; private AnimationDrawable ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout

一、代码实现:

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();
				}
			}
		});
	}

 

Android逐帧动画的实现

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号