动态壁纸教程

发布于 2024-10-05 03:39:13 字数 901 浏览 0 评论 0原文

我正在尝试从此处。

/**
 * Do the actual drawing stuff
 */
private void doDraw(Canvas canvas) {
    Bitmap b = BitmapFactory.decodeResource(context.getResources(), IMAGES[current]);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(b, 0, 0, null);
    Log.d(TAG, "Drawing finished.");
}

/**
 * Update the animation, sprites or whatever.
 * If there is nothing to animate set the wait
 * attribute of the thread to true
 */
private void updatePhysics() {
    // if nothing was updated :
    // this.wait = true;
    if(previousTime - System.currentTimeMillis() >= 41) { //24 FPS
        current = current < IMAGES.length ? current++ : 0;
    }
    Log.d(TAG, "Updated physics.");
}

但这似乎不起作用。我做错了什么。 “画完了”。和“更新的物理学”。正在打印消息。但我只看到第一张图片。我正在模拟器上测试它。

任何帮助将不胜感激。谢谢

I am trying to do the following from a live wallpaper tutorial I found here.

/**
 * Do the actual drawing stuff
 */
private void doDraw(Canvas canvas) {
    Bitmap b = BitmapFactory.decodeResource(context.getResources(), IMAGES[current]);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(b, 0, 0, null);
    Log.d(TAG, "Drawing finished.");
}

/**
 * Update the animation, sprites or whatever.
 * If there is nothing to animate set the wait
 * attribute of the thread to true
 */
private void updatePhysics() {
    // if nothing was updated :
    // this.wait = true;
    if(previousTime - System.currentTimeMillis() >= 41) { //24 FPS
        current = current < IMAGES.length ? current++ : 0;
    }
    Log.d(TAG, "Updated physics.");
}

But it doesn't seem to work. What am I doing wrong. The "Drawing finished." and "Updated physics." messages are getting printed. But I see the first image only. I'm testing it on the emulator.

Any help would be appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-10-12 03:39:13

我制作了一个简单的动态壁纸示例,其中颜色随着时间的推移而变化。也许您可以以此为起点:

package com.cmwmobile.android.samples;

import android.graphics.Canvas;

import android.os.Handler;

import android.service.wallpaper.WallpaperService;

import android.view.SurfaceHolder;

/**
 * The SampleLiveWallpaperService class is responsible for showing the
 * animation and is an interface to android. 
 * @author Casper Wakkers - www.cmwmobile.com
 */
public class SampleLiveWallpaperService extends WallpaperService {
    private Handler handler = null;

    /**
     * Inner class representing the actual implementation of the
     * Live Wallpaper {@link Engine}.
     */
    private class SampleLiveWallpaperEngine extends Engine {
        private boolean visible = false;

        private int[] colors = {0, 0, 0} ;

        /**
         * Runnable implementation for the actual work.
         */
        private final Runnable runnableSomething = new Runnable() {
            /**
             * {@inheritDoc}
             */
            public void run() {
                drawSomething();
            }
        };
        /**
         * The drawSomething method is responsible for drawing the animation.
         */
        private void drawSomething() {
            final SurfaceHolder holder = getSurfaceHolder();

            Canvas canvas = null;

            try {
                canvas = holder.lockCanvas();

                if (canvas != null) {
                    canvas.drawARGB(200, colors[0], colors[1], colors[2]);
                }

                updateColors(colors);
            }
            finally {
                if (canvas != null) {
                    holder.unlockCanvasAndPost(canvas);
                }
            }

            // Reschedule the next redraw.
            handler.removeCallbacks(runnableSomething);

            if (visible) {
                // Play around with the delay for an optimal result.
                handler.postDelayed(runnableSomething, 25);
            }
        }
        /**
         * Method updateColors updates the colors by increasing the value
         * per RGB. The values are reset to zero if the maximum value is
         * reached.
         * @param colors to be updated.
         */
        private void updateColors(int[] colors) {
            if (colors[0] < 255) {
                colors[0]++;
            }
            else {
                if (colors[1] < 255) {
                    colors[1]++;
                }
                else {
                    if (colors[2] < 255) {
                        colors[2]++;
                    }
                    else {
                        colors[0] = 0;
                        colors[1] = 0;
                        colors[2] = 0;
                    }
                }
            }
        }
        /**
         * {@inheritDoc}
         */
        public void onDestroy() {
            super.onDestroy();

            handler.removeCallbacks(runnableSomething);
        }
        /**
         * {@inheritDoc}
         */
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);

            this.visible = visible;

            if (visible) {
                drawSomething();
            }
            else {
                handler.removeCallbacks(runnableSomething);
            }
        }
    }

    /**
     * Constructor. Creates the {@link Handler}. 
     */
    public SampleLiveWallpaperService() {
        handler = new Handler();
    }
    /**
     * {@inheritDoc}
     */
    public Engine onCreateEngine() {
        return new SampleLiveWallpaperEngine();
    }
}

I have worked out a simple sample live wallpaper where the color shift over time. Maybe you can use this as a starting point:

package com.cmwmobile.android.samples;

import android.graphics.Canvas;

import android.os.Handler;

import android.service.wallpaper.WallpaperService;

import android.view.SurfaceHolder;

/**
 * The SampleLiveWallpaperService class is responsible for showing the
 * animation and is an interface to android. 
 * @author Casper Wakkers - www.cmwmobile.com
 */
public class SampleLiveWallpaperService extends WallpaperService {
    private Handler handler = null;

    /**
     * Inner class representing the actual implementation of the
     * Live Wallpaper {@link Engine}.
     */
    private class SampleLiveWallpaperEngine extends Engine {
        private boolean visible = false;

        private int[] colors = {0, 0, 0} ;

        /**
         * Runnable implementation for the actual work.
         */
        private final Runnable runnableSomething = new Runnable() {
            /**
             * {@inheritDoc}
             */
            public void run() {
                drawSomething();
            }
        };
        /**
         * The drawSomething method is responsible for drawing the animation.
         */
        private void drawSomething() {
            final SurfaceHolder holder = getSurfaceHolder();

            Canvas canvas = null;

            try {
                canvas = holder.lockCanvas();

                if (canvas != null) {
                    canvas.drawARGB(200, colors[0], colors[1], colors[2]);
                }

                updateColors(colors);
            }
            finally {
                if (canvas != null) {
                    holder.unlockCanvasAndPost(canvas);
                }
            }

            // Reschedule the next redraw.
            handler.removeCallbacks(runnableSomething);

            if (visible) {
                // Play around with the delay for an optimal result.
                handler.postDelayed(runnableSomething, 25);
            }
        }
        /**
         * Method updateColors updates the colors by increasing the value
         * per RGB. The values are reset to zero if the maximum value is
         * reached.
         * @param colors to be updated.
         */
        private void updateColors(int[] colors) {
            if (colors[0] < 255) {
                colors[0]++;
            }
            else {
                if (colors[1] < 255) {
                    colors[1]++;
                }
                else {
                    if (colors[2] < 255) {
                        colors[2]++;
                    }
                    else {
                        colors[0] = 0;
                        colors[1] = 0;
                        colors[2] = 0;
                    }
                }
            }
        }
        /**
         * {@inheritDoc}
         */
        public void onDestroy() {
            super.onDestroy();

            handler.removeCallbacks(runnableSomething);
        }
        /**
         * {@inheritDoc}
         */
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);

            this.visible = visible;

            if (visible) {
                drawSomething();
            }
            else {
                handler.removeCallbacks(runnableSomething);
            }
        }
    }

    /**
     * Constructor. Creates the {@link Handler}. 
     */
    public SampleLiveWallpaperService() {
        handler = new Handler();
    }
    /**
     * {@inheritDoc}
     */
    public Engine onCreateEngine() {
        return new SampleLiveWallpaperEngine();
    }
}
扎心 2024-10-12 03:39:13

我在我的雇主博客上创建了一个关于在动态壁纸中使用 SVG 的快速教程,如果您愿意,请查看。

第 1 部分 http://blog.infrared5.com/2012/03/android-动态壁纸/

第 2 部分 http://blog.infrared5.com/2012/ 03/android-live-wallpaper-part-2/

I created a quick tutorial on my employers blog about using SVG in a Live Wallpaper, check it out if you like.

Part 1 http://blog.infrared5.com/2012/03/android-live-wallpaper/

Part 2 http://blog.infrared5.com/2012/03/android-live-wallpaper-part-2/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文