制作流畅的动画

时间:2013-02-12 09:50:44

标签: android image animation

我有16张图片代表有人投球。我想用这些图像制作流畅的动画。有20张图像/秒,眼睛认为这是一个动作,而不是一连串的图像。所以我想每50ms显示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);
  }
}

但动画不流畅我看到第一张图片,然后没有,然后是最后一张,可能是因为50毫秒太短而无法区分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)

3 个答案:

答案 0 :(得分:2)

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

它允许您定义一组drawable和每个应该显示的持续时间。

该平台本身支持它,并且更加简单,无需实施。

答案 1 :(得分: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();

答案 2 :(得分:1)

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