android - 动态壁纸教程

标签 android graphics canvas sprite live-wallpaper

我正在尝试根据我发现的动态壁纸教程执行以下操作 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.");
}

但是好像不行。我究竟做错了什么。 “画完了”。和“更新物理学”。正在打印消息。但我只看到第一张图片。我正在模拟器上对其进行测试。

如有任何帮助,我们将不胜感激。谢谢

最佳答案

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

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

关于android - 动态壁纸教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302709/

相关文章:

android - 使用 YuvImage 压缩时出现奇怪的错误

algorithm - 如何计算多条曲线的 OBB?

javascript - 在图像上使用 "getImageData(...)"(或类似)的方法?

android - 使用 Path.lineTo 在 x 轴上增加一个小数点

android - WebView 中带参数的 URL 在 Android 中不起作用?

java - Android Studio中的任务 ':app:dexDebug'执行失败

android.graphics.Picture 未在 API 14+ 中绘制

Android网页开发问题

php - Android 应用程序未与数据库同步

java - RemoteViewsFactory 中的 onClickPendingIntent