Java 逐帧动画未运行

Java 逐帧动画未运行,java,android,Java,Android,嗯,我是android的新手,我不知道我的代码出了什么问题 这是我的xml文件 <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false"> <item android:drawable="@drawable/

嗯,我是android的新手,我不知道我的代码出了什么问题

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">  
    <item android:drawable="@drawable/w1" android:duration="50" />
    <item android:drawable="@drawable/w2" android:duration="50" />
</animation-list>


// Start the animation (looped playback by default).
frameAnimation.start();

在声明ImageView后设置图像可见性

  img.setVisibility(ImageView.VISIBLE);
在按下按钮时,检查frameAnimation是否正在运行

如果是,则调用
stop()
方法

     frameAnimation.stop();
并调用您的方法来启动动画

     frameAnimation.start();
使用runnable启动动画:-

new Runnable {
        public void run() {
            frameAnimation.start();
        }
}

请参阅以下现有问题:-


我用按钮点击事件测试代码,它工作得很好

维护性:

public class FrameAnimationActivity extends Activity {

    AnimationDrawable frameAnimation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView img = (ImageView) findViewById(R.id.imageView1);

        img.setBackgroundResource(R.anim.frames);
        frameAnimation = (AnimationDrawable) img.getBackground();

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                frameAnimation.start();
            }
        });

    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="Button" android:id="@+id/button1" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:layout_width="wrap_content"></ImageView>
</LinearLayout>
Frames.xml

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/img_0" android:duration="50"  />
    <item android:drawable="@drawable/img_1" android:duration="50" />
    <item android:drawable="@drawable/img_2" android:duration="50" />
    <item android:drawable="@drawable/img_3" android:duration="50" />
    <item android:drawable="@drawable/img_4" android:duration="50" />
    <item android:drawable="@drawable/img_5" android:duration="50" />
    <item android:drawable="@drawable/img_6" android:duration="50" />
    <item android:drawable="@drawable/img_7" android:duration="50" />
    <item android:drawable="@drawable/img_8" android:duration="50" />
    <item android:drawable="@drawable/img_9" android:duration="50" />
</animation-list>

Main.xml:

public class FrameAnimationActivity extends Activity {

    AnimationDrawable frameAnimation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView img = (ImageView) findViewById(R.id.imageView1);

        img.setBackgroundResource(R.anim.frames);
        frameAnimation = (AnimationDrawable) img.getBackground();

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                frameAnimation.start();
            }
        });

    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="Button" android:id="@+id/button1" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:layout_width="wrap_content"></ImageView>
</LinearLayout>


您遇到了什么错误?请尝试这样实现。在onclick listener中使用一个按钮并编写frameAnimation.start()方法,因为它需要强制启动。否则,在处理程序中启动方法。@siva我没有任何错误。@sony我已尝试实现可运行,但它仍然不工作。@sony它完全正确…但您必须从按钮或触摸按钮强制启动动画事件…是否有可能在启动应用程序时直接运行。我想创建一个可见性消失的按钮,并执行类似btn.performClick()的单击事件。这是自动执行单击事件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="Button" android:id="@+id/button1" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:layout_width="wrap_content"></ImageView>
</LinearLayout>