为什么动画可绘制不会出现

时间:2012-05-04 09:08:06

标签: android xml animation layout drawable

我想用14 png图像创建动画drawable。 我将14个图像添加到所有drawable文件夹中,并创建了一个如下所示的动画列表,但没有出现,问题是什么?

circle.xml:     

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/f1" android:duration="50" />
    <item android:drawable="@drawable/f2" android:duration="50" />
    <item android:drawable="@drawable/f3" android:duration="50" />
    <item android:drawable="@drawable/f4" android:duration="50" />
    <item android:drawable="@drawable/f5" android:duration="50" />
    <item android:drawable="@drawable/f6" android:duration="50" />
    <item android:drawable="@drawable/f7" android:duration="50" />
    <item android:drawable="@drawable/f8" android:duration="50" />
    <item android:drawable="@drawable/f9" android:duration="50" />
    <item android:drawable="@drawable/f10" android:duration="50" />
    <item android:drawable="@drawable/f11" android:duration="50" />
    <item android:drawable="@drawable/f12" android:duration="50" />
    <item android:drawable="@drawable/f13" android:duration="50" />
    <item android:drawable="@drawable/f14" android:duration="50" />
</animation-list>

layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />

    <ImageView
        android:id="@+id/imgCircle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

java代码:

package pit.opensource.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CircleAnimationActivity extends Activity {
    /** Called when the activity is first created. */
    Button btnStart;
    ImageView imgCircle;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnStart = (Button)findViewById(R.id.btnStart);
        imgCircle = (ImageView) findViewById(R.id.imgCircle);
        imgCircle.setBackgroundResource(R.drawable.circle);

        AnimationDrawable ani = (AnimationDrawable) imgCircle.getBackground();
        ani.start();
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //  ani.start();

            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

背景应该是动画,你应该把circle.xml放到res / anim,然后改变

imgCircle.setBackgroundResource(R.drawable.circle);

imgCircle.setBackgroundResource(R.anim.circle);

尝试以下代码来启动动画

   imgCircle.post(new Runnable() {
        @Override
        public void run() {
            AnimationDrawable ani = (AnimationDrawable) imgCircle.getBackground();
            ani.start();
        }
    });

或实现将动画开始移动到onWindowFocusChanged

public void onWindowFocusChanged(boolean flag) {
    super.onWindowFocusChanged(flag);
    AnimationDrawable anim = (AnimationDrawable) imgCircle.getBackground();
    anim.start();
}

答案 1 :(得分:0)

可能有第三个原因。

第一个原因是 OutOfMemoryError 。你需要压缩你的图像。

Solution

第二个原因是你的circle.xml中的 android:oneshot 参数。此动画仅运行14帧。通过将列表的android:oneshot属性设置为true,它将仅循环一次然后停止并保持最后一帧。如果设置为false,则动画将循环。

更改

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">

第三个原因是,在您的Activity的onCreate()方法中,无法调用在AnimationDrawable上调用的 start() 方法,因为AnimationDrawable尚未完全附加到窗口。

使用以下代码更改MainActivity.java:

package pit.opensource.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CircleAnimationActivity extends Activity {

    Button btnStart;
    ImageView imgCircle;
    AnimationDrawable ani;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgCircle = (ImageView) findViewById(R.id.imgCircle);
        imgCircle.setBackgroundResource(R.drawable.circle);

        ani = (AnimationDrawable) imgCircle.getBackground();

        addListenerOnButton();
    }

    public void addListenerOnButton() {

        btnStart = (Button)findViewById(R.id.btnStart);

        btnStart.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                ani.start();

            }

        });

    }

}