在动态壁纸中设置背景位图

发布于 2024-10-18 21:13:01 字数 163 浏览 1 评论 0原文

我刚刚开始写动态壁纸。

我想做的是将背景设置为我的资源中的位图。我将在此之上绘制我的动画。

做到这一点最有效的方法是什么?有没有办法将背景设置为动态壁纸中的位图?我是否只需要每次在绘制动画之前将位图绘制到画布中(这似乎效率低得可怕)?

谢谢,

杰伊

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-10-25 21:13:02

这就是我发现的。

来自 eclipse 图形文档:

注意:在每次从 SurfaceHolder 检索 Canvas 时,Canvas 的先前状态将被保留。为了正确地为图形设置动画,您必须重新绘制整个表面。例如,您可以通过使用drawColor()填充颜色或使用drawBitmap()设置背景图像来清除Canvas的先前状态。否则,你会看到你之前绘制的痕迹

所以我使用的代码如下:

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

在 onSurfaceChanged() 中
// 调整背景位图的大小,以便高效绘制
mBitmapBackground = Bitmap.createScaledBitmap(mBitmapBackground, xMax, yMax, true);

并在 Run() 中
尝试
{
画布 = sHolder.lockCanvas();
if(画布!= null)
canvas.drawBitmap(mBitmapBackground, 0, 0, null);

据我所知,这是尽可能有效的。尽管如果有人能纠正我这一点,我很乐意听到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文