1

I would like to develop a simple Live Wallpaper with a sequence of images. The only animation required is for each image to fade in and fade out.

All the tutorials I have found online for LWP demonstrate how to use the Draw canvas for fancy animations and drawing. This is not necessary for my app, I only want to iterate through a collection of images.

Being a novice programmer, I need some help learning how to loop through an array of images and how to display them as a wallpaper.

Can anyone share some code or point me towards a good tutorial for this?

UPDATE

The LWP loads on my device but the wallpaper does not change. It is stuck on image3, ironman

Here is the code I have so far. I assume I am doing something wrong in draw()

public class Wallpaper extends WallpaperService {
public void onCreate() {
    super.onCreate();
}

public void onDestroy() {
    super.onDestroy();
}

public Engine onCreateEngine() {
    return new CercleEngine();
}

class CercleEngine extends Engine {

    private final Handler handler = new Handler();
    private final Runnable drawRunner = new Runnable() {
        @Override
        public void run() {
            draw();
        }
    };
    private boolean visible = true;
    public Bitmap image1, image2, image3;

    CercleEngine() {
        image1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.batman);
        image2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.captainamerica);
        image3 = BitmapFactory.decodeResource(getResources(),
                R.drawable.ironman);
    }


    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visible = visible;
        if (visible) {
            handler.post(drawRunner);
        } else {
            handler.removeCallbacks(drawRunner);
        }
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        this.visible = false;
        handler.removeCallbacks(drawRunner);
    }

    public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
            float yStep, int xPixels, int yPixels) {
        draw();
    }

    void draw() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                c.drawBitmap(image1, 0, 0, null);
                c.drawBitmap(image2, 0, 0, null);
                c.drawBitmap(image3, 0, 0, null);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }

        handler.removeCallbacks(drawRunner);
        if (visible) 
        {
           handler.postDelayed(drawRunner, 1000); // delay 1 sec
        }

    }
}
4

1 回答 1

1

循环遍历一组图像没有简单的方法。它必须手动完成。

您可以采用的一种方法是将图像保存在 /res/drawable 中,然后使用 int[] 数组存储图像的残差,然后循环遍历它。

可以在此处找到关于动态壁纸的详细解释教程: http ://www.vogella.com/articles/AndroidLiveWallpaper/article.html

祝你好运

于 2012-07-03T12:28:10.580 回答