gpt4 book ai didi

安卓 : Frame by Frame animation not working?

转载 作者:行者123 更新时间:2023-11-29 21:50:15 27 4
gpt4 key购买 nike

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

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

动画测试.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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com