我有 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)