0

我有 16 个图像代表某人扔球。我想用这些图像制作流畅的动画。每秒 20 幅图像时,眼睛会认为这是一个运动,而不是一系列图像。所以我想每 50 毫秒显示 1 张图像

public void fire() {
final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
final int drawables[] = new int[] {R.drawable.fire1,R.drawable.fire2,R.drawable.fire3,R.drawable.fire4,R.drawable.fire5,R.drawable.fire6,R.drawable.fire7,R.drawable.fire8,R.drawable.fire9,R.drawable.fire10,R.drawable.fire11,R.drawable.fire12,R.drawable.fire13,R.drawable.fire14,R.drawable.fire15,R.drawable.fire16};
for (int i=0;i<drawables.length;i++) {
    final int j=i;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            image3.setImageResource(drawables[j]);
            Log.w("GAMEACTIVITY","image"+j);
        }
    };
    gameactivity.handler.postDelayed(runnable, 50*j);
  }
}

但是动画不流畅我看到第一张图像,然后什么都没有,然后是最后一张,可能是因为 50ms 太短,无法区分 Runnables 的动作?

编辑 使用动画可绘制

ImageView image2 = (ImageView) findViewById(R.id.imageView3);
        image2.setBackgroundResource(R.drawable.fire);
        AnimationDrawable fireAnimation = (AnimationDrawable) image2.getBackground();
        image2.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                fireAnimation.start();
                return false;
            }

我还有另一个内存错误。这不是第一次发生,我真的不知道如何处理它(阅读了很多关于内存泄漏的信息,但我不认为是那样)

02-12 10:20:30.279: E/dalvikvm-heap(1349): 1535664-byte external allocation too large for this process.
02-12 10:20:30.319: E/GraphicsJNI(1349): VM won't let us allocate 1535664 bytes
02-12 10:20:30.319: W/dalvikvm(1349): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-12 10:20:30.329: E/AndroidRuntime(1349): FATAL EXCEPTION: main
02-12 10:20:30.329: E/AndroidRuntime(1349): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.view.View.setBackgroundResource(View.java:7533)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at com.example.kersplatt.GameActivity.onCreate(GameActivity.java:69)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.os.Looper.loop(Looper.java:130)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invoke(Method.java:507)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

2

如果你有一堆图片,你应该看看Android Drawable Animation而不是使用 runnables。

它允许您定义一组可绘制对象以及每个应显示的持续时间。

它由平台原生支持,实现起来更加简单和轻松。

于 2013-02-12T09:53:38.507 回答
2

使用FrameAnimation,例如res/drawable/movie.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/frame1" android:duration="50" />
    <item android:drawable="@drawable/frame2" android:duration="50" />
    <item android:drawable="@drawable/frame3" android:duration="50" />
    etc...
</animation-list>

然后在Java中:

imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
anim.start();
于 2013-02-12T09:55:36.187 回答
1

您需要预加载图像,否则setImageResource()将在每次调用时加载(从存储中读取)图像,这应该花费超过 50 毫秒(我猜您的图像不仅仅是 20x20)

于 2013-02-12T09:53:10.303 回答