如何实现Android平台上的逐帧动画效果?

2026-06-10 08:5210阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计441个文字,预计阅读时间需要2分钟。

如何实现Android平台上的逐帧动画效果?

一、代码实现:

javaprivate ImageView iv;private AnimationDrawable ad;

@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv=(ImageView) findViewById(R.id.iv); ad=(AnimationDrawable) iv.getDrawable();}

二、简写内容:

java

一、创建ImageView和AnimationDrawable对象,并在onCreate中设置布局和初始化组件。


一、代码实现:

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="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>


如何实现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);

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

本文共计441个文字,预计阅读时间需要2分钟。

如何实现Android平台上的逐帧动画效果?

一、代码实现:

javaprivate ImageView iv;private AnimationDrawable ad;

@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv=(ImageView) findViewById(R.id.iv); ad=(AnimationDrawable) iv.getDrawable();}

二、简写内容:

java

一、创建ImageView和AnimationDrawable对象,并在onCreate中设置布局和初始化组件。


一、代码实现:

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="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>


如何实现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);

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