动态壁纸教程 [英] Live Wallpaper Tutorial

查看:350
本文介绍了动态壁纸教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做从动态壁纸教程下面我发现这里

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.

任何帮助将是AP preciated。谢谢

Any help would be appreciated. Thanks

推荐答案

我已经做了一个简单的示例动态壁纸,其中的颜色随着时间逐渐变化。也许你可以使用它作为一个出发点:

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

这篇关于动态壁纸教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆