Android:canvas.drawBitmap 和 BitmapDrawable.draw 之间的区别?
当我想在 Android 中将 BitmapDrawable 绘制到 Canvas 时,有两种可能性可以完成相同的操作,但我不知道更喜欢哪一种:
使用 canvas.drawBitmap() 并使用 getBitmap 从可绘制对象中提取位图()
使用drawable.draw(canvas),将画布作为参数传递给
我现在使用第一个选项,但它似乎完全任意,因为我看不出任何区别。
感谢您的回答
When I want to draw a BitmapDrawable to a Canvas in Android, there are two possibilities that do the same and I don't know which one to prefer:
Using canvas.drawBitmap() and extract the Bitmap from the drawable using getBitmap()
Using drawable.draw(canvas), passing the canvas as an argument to the drawable.
I'm using the first option now, but it seems completely arbitrary as I can't see any difference.
Thanks for your answers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
切勿按照您的方式执行选项 1。与其每次要绘制可绘制对象时都创建位图,不如首先创建一个位图。也就是说,如果要绘制位图,请不要创建 Drawable。创建如下所示的位图:
您只需执行一次此操作。之后,就像你一样绘制(canvas.drawbitmap())。
至于选项2,你做得正确。
现在,存在一些差异。
选项 1 绘制速度更快,通常适用于背景图像。 FPS 会发生重大变化,具体取决于您绘制的是位图还是可绘制图。位图更快。
如果您需要对图像进行缩放、移动和其他类型的操作,则可以选择选项 2。速度没那么快,但如果您想做刚才提到的任何事情,就没有其他选择。
希望这有帮助!
Never do option number 1 the way you do it. Instead of creating a bitmap out of a drawable every time you want to draw it, create a bitmap in the first place. That is, don't create a Drawable if you are going to draw a bitmap. Create a bitmap like this:
And this is something you do just once. After that, just draw like you do (canvas.drawbitmap()).
As for option number 2, you are doing it correctly.
Now, there are some differences.
Option 1 is faster to draw and usually good for background images. There is a significant change to FPS depending on if you draw a bitmap or drawable. Bitmaps are faster.
Option 2 is the way to go if you need to things like scaling, moving and other kinds of manipulations of the image. Not as fast but there's no other option if you want to do any of those things just mentioned.
Hope this helps!