1

我开始实现android动态壁纸,遵循互联网上的示例和教程,我不能将png背景作为壁纸。还在这里检查了类似的问题,但仍然无法使其工作。

这是代码:

public class LiveWallpaper extends WallpaperService {

    /* IDs of recurces needed for animations*/
    private SurfaceHolder holder;
    private static final String TAG = "MyActivity";

    @Override
    public void onCreate() {
        super.onCreate();
    }

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

    @Override
    public Engine onCreateEngine() {
        return new WallpaperEngine();
    }

    class WallpaperEngine extends Engine {
    public final Runnable mDrawWallpaper = new Runnable(){
         public void run(){
            drawWallpaper();
         }
    };

    @Override
    public void onCreate(SurfaceHolder surfaceHolder){
        super.onCreate(surfaceHolder);
        setTouchEventsEnabled(false);
        loadImagesIntoMemory(R.drawable.wallpaper);
        holder = getSurfaceHolder();

    }

    void drawWallpaperContent(Canvas c, int resourceId){
        Bitmap decodeResoure = BitmapFactory.decodeResource (getResources(), resourceId);
        c.drawBitmap(decodeResoure, 0, 0, null);
    }

    void drawWallpaper(){
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
            c = holder.lockCanvas();

        if(c!=null){
            c.save();
            drawWallpaperContent(c, R.drawable.wallpaper);
                    c.restore();
        }
        }

        private void loadImagesIntoMemory(int resourceId){
        Resources res = getResources();
        BitmapFactory.decodeResource(res, resourceId);
        }

        @Override
        public void onDestroy(){
        super.onDestroy();
        mHandler.removeCallbacks(mDrawWallpaper);           
        }
    }
}

位图存放在drawable文件夹中,android sdk版本为2.2。启动动态壁纸后,我只得到“加载壁纸”而不显示壁纸图像。

有谁知道可能是什么问题?

谢谢你。DJ。

4

2 回答 2

1

在你的平局中使用它

'位图图像 = BitmapFactory.decodeResource(getResources(),R.drawable.image);'

 canvas.drawBitmap(image, 0, 0, paint);

你可以在paint参数中传递null。m 使用它及其工作

于 2015-01-02T08:08:07.440 回答
0

c.drawColor(0xff000000);在绘制位图是我的解决方案之前,我遇到了类似的问题。

于 2011-06-22T21:50:22.577 回答