如何在Android动态壁纸中翻转图像

时间:2012-08-21 10:18:46

标签: android live-wallpaper

我想让我的动态壁纸每隔5秒翻转一次图片(例如)。如何使用此代码进行翻转操作?

public class MyWallpaperService extends WallpaperService {

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

    public class MyWallpaperServiceEngine extends Engine {

        private Thread thread;
        private Bitmap image;
        private SurfaceHolder holder;
        private boolean running;
        private boolean ready;
        private boolean vis;
        private Paint paint;
        private int nheight;
        private int r = 0;
        private int g = 64;
        private int b = 128;
        private int ox;
        private float angle;
        private float velocity;
        private Matrix matrix;

        @Override
        public void onTouchEvent(MotionEvent event) {
            // velocity=30;
            // super.onTouchEvent(event);
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xOffsetStep, float yOffsetStep, int xPixelOffset,
                int yPixelOffset) {
            ox = -xPixelOffset;
            super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                    xPixelOffset, yPixelOffset);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            image = BitmapFactory.decodeResource(getResources(),
                    mFullSizeIds[0]);
        }

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

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            ready = false;
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            vis = visible;
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            setTouchEventsEnabled(true);
            running = true;
            matrix = new Matrix();
            paint = new Paint();
            holder = surfaceHolder;
            thread = new Thread() {
                @Override
                public void run() {
                    while (running) {
                        if (ready && vis) {

                            final Canvas c = holder.lockCanvas();
                            if (c != null) {

                                r = inc(r, 1);
                                g = inc(g, 2);
                                b = inc(b, 3);
                                paint.setColor(0xff000000 | (r << 16)
                                        | (g << 8) | b);
                                c.drawPaint(paint);

                                angle += velocity;
                                velocity = velocity * 0.96f;
                                matrix.reset();
                                matrix.postRotate(angle);
                                matrix.preTranslate(-image.getWidth() >> 1,
                                        -image.getHeight() >> 1);
                                matrix.postTranslate(ox, nheight >> 1);
                                c.drawBitmap(image, matrix, null);

                                holder.unlockCanvasAndPost(c);
                            }
                            pause(1000 / 24);
                        } else {
                            pause(1000);
                        }
                    }
                }
            };
            thread.start();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            running = false;
        }

        private final void pause(long time) {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
            }
        }

        private final int inc(int v, int d) {
            return v + (d % 256);
        }

        private Integer[] mFullSizeIds = { R.drawable.p, R.drawable.p1,
                R.drawable.p2, R.drawable.p3, R.drawable.p4,
                R.drawable.p5 };
    }

}

在onSurfaceCreated中,应用程序只在pic上显示id为0并不是我拥有的所有6张照片。

onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            image = BitmapFactory.decodeResource(getResources(),
                    mFullSizeIds[0]);
        }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

放置处理程序,它将每隔5秒调用一次draw方法,并通过解锁来使用canvas绘制图像。你可以在android sdk中找到它的样本。希望这会有所帮助。