安卓 : Frame by Frame animation not working?

标签 android animation frame

我正在尝试逐帧动画,但它给了我一个强制关闭,我不确定为什么它给我一个强制关闭。在我看来一切都很好。

这是我的代码,我希望有人能帮忙吗? 提前致谢。

动画测试.java

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class AnimationTest extends Activity {

AnimationDrawable animation;

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


    AnimationDrawable animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.up), 500);
    animation.addFrame(getResources().getDrawable(R.drawable.down), 500);

    animation.setOneShot(false);

    ImageView imageAnim =  (ImageView) findViewById(R.id.imageView1);
    imageAnim.setBackgroundDrawable(animation);

    // start the animation!
    animation.start();
} 

}

activity_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">

    <ImageView android:id="@+id/imageView1"
        android:src="@drawable/down"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

最佳答案

您在 onCreate 方法中启动动画,这将不起作用,因为您的 Activity 尚未显示,请改用延迟的可运行对象或 clickListener。

您可以使用此代码测试动画:

// inside onCreate
   setContentView(R.layout.activity_main);


final AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.up), 500);
animation.addFrame(getResources().getDrawable(R.drawable.down), 500);

animation.setOneShot(false);

ImageView imageAnim =  (ImageView) findViewById(R.id.imageView1);
imageAnim.setBackgroundDrawable(animation);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){

public void run(){

animation.start();

}
}, 3000);

请确保您的 ImageView 有大小,并且没有设置 src 以查看发生了什么,在您的布局 xml 文件中执行以下操作:

    android:layout_width="50dp"
    android:layout_height="50dp"

并删除 src 属性(如果有的话)

关于安卓 : Frame by Frame animation not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14603220/

相关文章:

android - 在 finishMatch 上结束多人游戏

android - Android中String类型的RealmList

android - 依次播放 2 个 animationdrawables

javascript - jQuery:跨页面/url 移动 div

wpf - 将滚动条添加到框架 WPF

resize - 鼠标调整大小后,框架集无法正确调整大小

android - 错误无法解析符号 TabLayout 和“设计”

android - 在android studio中添加未实现的方法

jquery - 如何在 jquery 中级联动画

javascript - 帧中断检测