在动态壁纸设置背景位图 [英] setting background bitmap in live wallpaper
问题描述
我刚开始写一个动态壁纸。
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)?
感谢,
周杰伦
推荐答案
这是我发现了什么。
从Eclipse的图形文件:
From the eclipse graphic documentation:
请注意:在每一个与你擦肩而过检索来自SurfaceHolder画布,画布的previous状态将被保留。为了正确动画图形,你必须重新绘制整个表面。例如,您可以通过在颜色drawColor()与drawBitmap填充或设置背景图片清除画布的previous状态()。否则,你会发现你previously进行图纸的痕迹
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
所以,code我用如下:
So the code I used is as follows:
在的onCreate()
mBitmapBase = BitmapFactory.de codeResource(getResources(),R.drawable.bidBackground);
in onCreate() mBitmapBase = BitmapFactory.decodeResource(getResources(), R.drawable.bidBackground);
在onSurfaceChanged()
//大小的背景位图,因此有效地吸引
mBitmapBackground = Bitmap.createScaledBitmap(mBitmapBackground,XMAX,YMAX,真实);
in onSurfaceChanged() // size the background bitmap so it draws efficiently mBitmapBackground = Bitmap.createScaledBitmap(mBitmapBackground, xMax, yMax, true);
和在run()
尝试
{
画布= sHolder.lockCanvas();
如果(帆布!= NULL)
canvas.drawBitmap(mBitmapBackground,0,0,NULL);
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.
这篇关于在动态壁纸设置背景位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!