2

我正在尝试使用我正在绘制的一些图像制作动画 Android 背景。我一步一步地画图,但手机画得很慢。

我正在尝试这个:

    public class MyWallpaperService extends WallpaperService {
        private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
                R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
                R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11,
                R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15,
                R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19,
                R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23,
                R.drawable.p24 };

        @Override
        public Engine onCreateEngine() {
            return new MyWallpaperEngine();
        }

        private class MyWallpaperEngine extends Engine {
            private final Handler handler = new Handler();
            private final Runnable drawRunner = new Runnable() {
                @Override
                public void run() {
                    drawImage(images[0]);
                }

            };

            private Paint paint = new Paint();
            private boolean touch = true;

            @Override
            public void onVisibilityChanged(boolean visible) {
                if (visible) {
                    handler.post(drawRunner);
                } else {
                    handler.removeCallbacks(drawRunner);
                }
            }

            @Override
            public void onSurfaceDestroyed(SurfaceHolder holder) {
                super.onSurfaceDestroyed(holder);
                handler.removeCallbacks(drawRunner);
            }

            @Override
            public void onSurfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                super.onSurfaceChanged(holder, format, width, height);
            }

            @Override
            public void onTouchEvent(MotionEvent event) {
                if (partOne) {
                    if (touch) {
                        touch = false;
                        for (int i = 0; i < images.length; i++)
                            imagePostDelay(i, i * 100, i == images.length - 1 ? true
                                    : false);
                    }
                }
                super.onTouchEvent(event);
            }

            private void drawImage(int id) {
                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                @SuppressWarnings("deprecation")
                int width = display.getWidth(); // deprecated 960
                @SuppressWarnings("deprecation")
                int height = display.getHeight(); // deprecated 540
                final Resources res = getResources();
                Bitmap bitmap;
                Canvas canvas = null;
                SurfaceHolder holder = getSurfaceHolder();
                bitmap = BitmapFactory.decodeResource(res, id);
                bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                try {
                    canvas = holder.lockCanvas();
                    canvas.drawBitmap(bitmap, 0, 0, paint);
                } catch (NullPointerException e) {
                } finally {
                    if (canvas != null)
                        holder.unlockCanvasAndPost(canvas);
                }
            }

            private void imagePostDelay(final int index, int delay,
                    final boolean allowTouch) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        try {
                            drawImage(images[index]);
                        } catch (ArrayIndexOutOfBoundsException e) {
                        }
                        if (allowTouch)
                            touch = true;
                    }
                }, delay);
            }
        }

我尝试在该onCreate方法中以适当的分辨率保存图像,但没有帮助。然后我尝试使用较少的图像,但它引发了OutOfMemory异常。这可能是由图像数量引起的。最主要的是捕捉图像并没有太大帮助。它仍然很慢。

  • 有没有什么好的教程可以帮助在 Android 上制作现场幻灯片背景?
  • setWallpaper()在 xml 中以适当的分辨率存储图像并仅使用该方法更改图像是个好主意吗?方法会 setWallpaper()更快吗?
4

1 回答 1

1

为了更有效地绘制移动

bitmap = BitmapFactory.decodeResource(res, id);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);

....
private class MyWallpaperEngine extends Engine {
bitmap = BitmapFactory.decodeResource(res, id);

....
    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);

        // size the background bitmap so it draws efficiently 
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);

        drawFrame();
    }

....
void drawFrame() {
Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                // This will draw on every cycle of the Runnable, 
                // so don't do any decoding of Bitmap here, 
                // that would be very inefficient.  
               canvas.drawBitmap(bitmap, 0, 0, paint);
         }
        } finally {
            if (c != null) holder.unlockCanvasAndPost(c);
        }

        mHandler.removeCallbacks(drawRunner);
        if (mVisible) {
            mHandler.postDelayed(drawRunner,  1000/25);
        }
    }
于 2013-01-11T16:48:47.590 回答