动画图像的AnimationDrawable或OnDraw

时间:2014-12-29 03:45:34

标签: android

我想在屏幕上有20个对象。每个对象都是动画的,基本上由5个图像帧组成。例如,假设有烟雾出现的房子。我想知道使用哪种方法(以及为什么)

1- Animation Drawable,其中图像是在xml文件中设置的动画中定义的。动画设置为无限

2-自定义视图扩展ImageView,我在OnDraw方法中设置某个帧所需的图像,并在方法结束时继续调用onDraw(或无效)

3-其他一些方法

我倾向于使用第一种方法轻松,但我不确定正确的方法(或为什么我不应该使用我的)

非常感谢

1 个答案:

答案 0 :(得分:0)

是的,第一个是简单而好的方法。我之前做过。活动初始化如下:

    ImageView imageViewAnimation;
    private AnimationDrawable independentAnimation;

    imageViewAnimation = (ImageView) findViewById(R.id.imageViewAnimation);

        // set the animation drawable as background
        // here demo_logo_animation is an XML

        imageViewAnimation
                .setBackgroundResource(R.drawable.demo_logo_animation);
        // create an animation drawable using the background


        independentAnimation = (AnimationDrawable) imageViewAnimation
                .getBackground();

        // start the animation
        imageViewAnimation.post(new Runnable() {

            @Override
            public void run() {
                // fOR 2.3 DEVICES
                independentAnimation.start();
            }
        });

放置所有图像& res / drawable 文件夹中的demo_logo_animation.xml。 demo_logo_animation.xml中的属性 ,从上到下依次运行。所以,小心地放置它。然后demo_logo_animation.xml看起来像

    <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/itv001"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv003"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv006"
        android:duration="@string/log_in_duration"/>


    <item
        android:drawable="@drawable/itv009"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv012"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv015"
        android:duration="@string/log_in_duration"/>


    <item
        android:drawable="@drawable/itv018"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv021"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv024"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv027"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv030"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv033"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv036"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv039"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv042"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv045"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv048"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv051"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv054"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv057"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv060"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv063"
        android:duration="@string/log_in_duration"/>




    <item
        android:drawable="@drawable/itv066"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv069"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv072"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv075"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv078"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv081"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv084"
        android:duration="@string/log_in_duration"/>

    <item
        android:drawable="@drawable/itv087"
        android:duration="@string/log_in_duration"/>


</animation-list>