setting background bitmap in live wallpaper

淺唱寂寞╮ 提交于 2019-12-11 12:15:59

问题


I just started writing a live wallpaper.

What I would like to do is to set the background to a bitmap from my resources. And I will draw my animations on top of this.

What is the most efficient way to do this? Is there a way to set the background to a bitmap in a live wallpaper? Do I just need to draw the bitmap into the canvas each time before I draw my animations (which seems like it might be hideously inefficient)?

thanks,

Jay


回答1:


This is what I found.

From the eclipse graphic documentation:

Note: On each pass you retrieve the Canvas from the SurfaceHolder, the previous state of the Canvas will be retained. In order to properly animate your graphics, you must re-paint the entire surface. For example, you can clear the previous state of the Canvas by filling in a color with drawColor() or setting a background image with drawBitmap(). Otherwise, you will see traces of the drawings you previously performed

So the code I used is as follows:

in onCreate() mBitmapBase = BitmapFactory.decodeResource(getResources(), R.drawable.bidBackground);

in onSurfaceChanged() // size the background bitmap so it draws efficiently mBitmapBackground = Bitmap.createScaledBitmap(mBitmapBackground, xMax, yMax, true);

and in Run() try { canvas = sHolder.lockCanvas(); if (canvas != null) canvas.drawBitmap(mBitmapBackground, 0, 0, null);

As far as I can tell this is as efficient as it can get. Although if anybody can correct me on this I would love to hear it.



来源:https://stackoverflow.com/questions/5110913/setting-background-bitmap-in-live-wallpaper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!