来自图像的Android动画壁纸

时间:2013-01-11 16:38:30

标签: android live-wallpaper android-image

我正在尝试制作动画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上的背景?
  • 将图像以适当的分辨率存储在xml和中是否是个好主意 只需使用setWallpaper()方法更改图片?请问 setWallpaper()方法更快?

1 个答案:

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