0

我想让我的动态壁纸每 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 中,该应用程序仅显示在 id 为 0 的图片上,而不是我拥有的 6 张图片的全部。

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

有什么想法吗?

4

1 回答 1

0

放置处理程序,它将每 5 秒调用一次您的绘图方法,并通过解锁画布使用画布绘制图像。您可以在 android sdk 中找到该示例。希望这会有所帮助。

于 2012-08-24T06:11:53.310 回答