簡體   English   中英

逐幀動畫未運行

[英]frame by frame animation not running

好吧,我是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>

我的java文件

ImageView img = (ImageView)findViewById(R.id.s);
img.setBackgroundResource(R.anim.shape_animation);


// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();


// Start the animation (looped playback by default).
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();
            }
        });

    }

}

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:

<?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>

在聲明ImageView之后設置Image可見性。

  img.setVisibility(ImageView.VISIBLE);

在按下的按鈕上,檢查frameAnimation是否正在運行。

如果是,則調用stop()方法

     frameAnimation.stop();

並調用您的方法來start()動畫。

     frameAnimation.start();

使用runnable開始動畫:-

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

要么

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

請參閱以下現有問題:

框架動畫未運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM