Frame Animation Not Running

2019-08-14 08:55发布

I have tried every sample of code I can find that defines and runs a frame animation, but the ImageView never changes. The original Image in the ImageView stays.

EDIT: After many days of trial and error, I ran the APK on my phone instead of the emulator. It works on my phone, but for some reason it does not on the emulator.

3条回答
小情绪 Triste *
2楼-- · 2019-08-14 09:16

Want to add a comment to schwiz answer.

I was able to get it working from calling the Handler.post from a worker thread. Below is the sample.

public void onCreate(Bundle savedInstanceState) {

    Thread t;
    final ImageView activityIndicator;
    final AnimationDrawable animiDrawable;
    LinearLayout contentPlaceholder;

    contentPlaceholder = (LinearLayout)this.findViewById(R.id.content_placeholder);
    activityIndicator = new ImageView(this);
    contentPlaceholder.addView(activityIndicator);
    activityIndicator.setBackgroundResource(R.drawable.ic_activity_indicator_bar);
    animiDrawable = (AnimationDrawable)activityIndicator.getBackground();

    t = new Thread(new Runnable() {
        @Override
        public void run() {
            mCbt.mThreadHandler.post(new Runnable() {
                @Override
                public void run() {
                    animiDrawable.start();
                }
            });
        };
    });
    t.start();
}
查看更多
叛逆
3楼-- · 2019-08-14 09:23

I struggled with it some time as well. It is explained in android frame animation tutorial, but let's say with "small letters". Let me copy the answer from there:

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

It is at the very bottom of http://developer.android.com/guide/topics/graphics/view-animation.html#frame-animation

查看更多
叼着烟拽天下
4楼-- · 2019-08-14 09:25

I am not exactly clear why you can't start animations from the onCreate thread, they need to be posted to whatever thread the handler of the view was created on.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ImageView view = (ImageView) findViewById(R.id.shell);
    view.setVisibility(ImageView.VISIBLE);
    view.setBackgroundResource(R.drawable.animation_frame);

    view.post(new Runnable(){

      public void run(){
        AnimationDrawable frameAnimation =  (AnimationDrawable) view.getBackground();
        frameAnimation.start();
      }
    });
   }
}

I have no idea why you need to do this but you do.

edit:Here is snippets from my working project should be everything you need...

in java in onCreate:

final ImageView image1 =  (ImageView) v.findViewById(R.id.coin1);
image1.post(new Runnable() {

        @Override
        public void run() {
            AnimationDrawable ani = (AnimationDrawable) image1.getBackground();
            ani.start();
        }
    });

in xml layout:

        <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/coin"
        android:id="@+id/coin1"/>

coin xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/coin_spin_a" android:duration="100"/>
    <item android:drawable="@drawable/coin_spin_b" android:duration="100"/>
    <item android:drawable="@drawable/coin_spin_c" android:duration="100"/>
    <item android:drawable="@drawable/coin_spin_d" android:duration="100"/>
    <item android:drawable="@drawable/coin_spin_e" android:duration="100"/>
    <item android:drawable="@drawable/coin_spin_f" android:duration="100"/>
</animation-list>
查看更多
登录 后发表回答