如何在Android中为动画制作动画

时间:2014-12-09 20:34:27

标签: android xml animation android-drawable

我想问一下,我想将这个动画制作为单个图像,并想在其上应用一些其他视图动画,如移动或alpha等,如何做到这一点?任何的想法 ?我想在可绘制的动画上应用视图动画。

我有一些像s1,s2,s3,s4到s16这样的抽象XML

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/s1" android:duration="200" />
<item android:drawable="@drawable/s2" android:duration="200" />
<item android:drawable="@drawable/s3" android:duration="200" />
<item android:drawable="@drawable/s12" android:duration="200" />
<item android:drawable="@drawable/s13" android:duration="200" />
<item android:drawable="@drawable/s14" android:duration="200" />
<item android:drawable="@drawable/s15" android:duration="200" />
<item android:drawable="@drawable/s16" android:duration="200" />

</animation-list>

和java代码是

anim = (ImageView) findViewById(R.id.black);
   anim.setBackgroundResource(R.drawable.smurf);
    AnimationDrawable hello = (AnimationDrawable) anim.getBackground();
    hello.start();

这里的drawur中的smurf是可绘制动画的xml文件。

1 个答案:

答案 0 :(得分:0)

我们可以做那个动画,但我们必须为此维护处理程序。

    private int index = 0;
    private Handler handler = new Handler();
    private AnimationDrawable drawable;

Runnable runnable = new Runnable() {

    @Override
    public void run() {
        imageView.setImageDrawable(drawable.getFrame(index));
        index++;
        if (index == drawable.getNumberOfFrames()) {
            index = 0;
        }
        handler.postDelayed(runnable, drawable.getDuration(index));
    }
};

在您的onclick或您想要开始动画的地方写如下:

handler.postDelayed(runnable, drawable.getDuration(index));

并且您的onDestroy()删除了这样的handeler回拨:

    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }