安卓:canvas.drawBitmap和BitmapDrawable.draw之间的区别?区别、drawBitmap、canvas、draw

2023-09-04 10:28:17 作者:冰是睡着的水

当我想画一个BitmapDrawable在Android的画布,有两种可能性,做同样的,我不知道哪一个preFER:

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:

使用canvas.drawBitmap(),并从中提取绘制使用getBitmap位图()

Using canvas.drawBitmap() and extract the Bitmap from the drawable using getBitmap()

使用drawable.draw(画布),传递画布作为参数传递给被拉伸。

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.

感谢您的答案

推荐答案

永远不要选择1号,你做的方式。而不是创建一个位图了,你想画它绘制每一次的,创造摆在首位的位图。也就是说,不要创建一个可绘制,如果你要画一个位图。创建一个位图是这样的:

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:

mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.myImage);
mBitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);

这是你做的只有一次的东西。在此之后,只画像你一样(canvas.drawbitmap())。

And this is something you do just once. After that, just draw like you do (canvas.drawbitmap()).

至于选择号码2,你正在做正确的。

As for option number 2, you are doing it correctly.

现在,也有一些差异。 选项​​1是更快的绘制,通常为背景图像良好。有依赖,如果你画一个位图或绘制在显著改变FPS。位图速度更快。

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.

选项2的方式,如果你需要的东西像缩放,移动等各类图像的操纵去。如果你想要做任何刚才提到的,那些东西不是很快,但有没有其他的选择。

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.

希望这有助于!