简体   繁体   English

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

[英]How to flip images in android live wallpaper

I want to make my live wallpaper flip between pictures every 5 seconds (for example). 我想让动态壁纸每5秒在图片之间切换一次(例如)。 how can i make the flip action with this code ? 如何使用此代码执行翻转动作?

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 };
    }

}

In the onSurfaceCreated, the app shows just on pic which have the id 0 not all of the 6 pic i have. 在onSurfaceCreated,应用程序只显示其上有ID为0不是所有的6 PIC我有照片。

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

Any idea pls ? 有什么想法吗?

put handler which will call your draw method on every 5 second and draw image using canvas by unlocking it. 放置处理程序,它将每5秒调用一次draw方法,并通过解锁画布使用画布绘制图像。 you can find sample of that in android sdk. 您可以在android SDK中找到该示例。 Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM