当前位置 : 主页 > 编程语言 > java >

Android逐帧动画的实现

来源:互联网 收集:自由互联 发布时间:2022-08-10
一、代码实现: private ImageView iv ; private AnimationDrawable ad ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); iv = ( Ima


一、代码实现:

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

 

上一篇:2022年8月7日——Java中Integer的使用
下一篇:没有了
网友评论