Android:如何叠加位图并在位图上绘制?
我实际上有三个问题:
- 在位图上绘制图像或创建位图作为资源然后在位图上绘制它更好吗?性能方面,哪一个更好?
- 如果我想在位图上绘制透明的东西,我该怎么做?
- 如果我想将一个透明位图覆盖在另一个透明位图上,我该怎么做?
很抱歉列出了很长的清单,但为了学习的兴趣,我想探索这两种方法。
I have three questions actually:
- Is it better to draw an image on a bitmap or create a bitmap as resource and then draw it over a bitmap? Performance wise, which one is better?
- If I want to draw something transparent over a bitmap, how would I go about doing it?
- If I want to overlay one transparent bitmap over another, how would I do it?
Sorry for the long list, but in the interest of learning, I would like to explore both the approaches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以这样做:
这个想法非常简单:一旦将位图与画布关联起来,您就可以调用画布的任何方法来绘制位图。
这适用于具有透明度的位图。如果位图具有 Alpha 通道,则该位图将具有透明度。查看 Bitmap.Config。您可能想使用 ARGB_8888。
重要提示:请查看此 Android 示例,了解执行绘图的不同方式。这会对你有很大帮助。
就性能而言(确切地说,就内存而言),位图是最好使用的对象,因为它们只是包装本机位图。 ImageView 是 View 的子类,BitmapDrawable 内部包含 Bitmap,但它还包含许多其他内容。但这过于简单化了。您可以建议特定于性能的场景以获得准确的答案。
You can do something like this:
The idea is very simple: Once you associate a bitmap with a canvas, you can call any of the canvas' methods to draw over the bitmap.
This will work for bitmaps that have transparency. A bitmap will have transparency, if it has an alpha channel. Look at Bitmap.Config. You'd probably want to use ARGB_8888.
Important: Look at this Android sample for the different ways you can perform drawing. It will help you a lot.
Performance wise (memory-wise, to be exact), Bitmaps are the best objects to use, since they simply wrap a native bitmap. An ImageView is a subclass of View, and a BitmapDrawable holds a Bitmap inside, but it holds many other things as well. But this is an over-simplification. You can suggest a performance-specific scenario for a precise answer.
如果目的是获取位图,这很简单:
最终image将包含image和image2的重叠部分。
If the purpose is to obtain a bitmap, this is very simple:
In the end, image will contain the overlap of image and image2.
对于 Kotlin 粉丝:
For Kotlin fans:
我不敢相信还没有人回答这个问题! SO 上罕见发生!
1
这个问题对我来说不太有意义。但我会尝试一下。
如果您询问直接绘制到画布(多边形、阴影、文本等)与加载位图并将其位图传输到画布上,这将取决于绘图的复杂性。
随着绘图变得更加复杂,所需的 CPU 时间也会相应增加。
然而,将位图传输到画布上始终需要恒定的时间,该时间与位图的大小成正比。
2
在不知道“某事”是什么的情况下,我如何向您展示如何做?
您应该能够从#3 的答案中找出#2。
3
假设:
您希望它们都从左上角重叠。
I can't believe no one has answered this yet! A rare occurrence on SO!
1
The question doesn't quite make sense to me. But I'll give it a stab.
If you're asking about direct drawing to a canvas (polygons, shading, text etc...) vs. loading a bitmap and blitting it onto the canvas that would depend on the complexity of your drawing.
As the drawing gets more complex the CPU time required will increase accordingly.
However, blitting a bitmap onto a canvas will always be a constant time which is proportional to the size of the bitmap.
2
Without knowing what "something" is how can I show you how to do it?
You should be able to figure out #2 from the answer for #3.
3
Assumptions:
You want them both overlaid from the top left corner.