Android 可变位图
有人可以向我解释一下什么是可变位图吗?可变位图和不可变位图有哪些优点/缺点或限制?
Can someone please explain to me what a mutable bitmap is? What advantages/disadvantages or what limitations do mutable and immutable bitmaps have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这与 String 与 StringBuilder 的区别大致相同 - String 是不可变的,因此您无法更改其内容(至少不能没有任何黑客攻击),而对于 StringBuilder 您可以更改其内容。
要将不可变位图转换为可变位图,请查看这篇文章:
https://stackoverflow.com/a/16314940/878126。
It's about the same difference as with String vs StringBuilder - String is immutable so you can't change its content (well at least not without any hacks), while for StringBuilder you can change its content.
In order to convert an immutable bitmap to a mutable one, check out this post:
https://stackoverflow.com/a/16314940/878126.
如果您的位图包含可变标志,则其像素可以更改,如果没有,则像素更改会引发错误。这就是他们之间的区别。
这里是 Android:将不可变位图转换为可变
If your bitmap include mutable flag, its pixels can be change, if doesn't, pixel changings throw an error. It's the difference between them.
And here is Android: convert Immutable Bitmap into Mutable
不确定使用不可变位图是否有任何性能提升。
通常不可变只是为了线程安全(或者如果您要与另一个进程共享图像,则进程安全)
Not sure if there is any performance gain for using immutable bitmaps.
Often immutable is just for being thread safe (or if you are going to share the image with another process, process safe)
尝试根据该位图的尺寸创建一个新位图,将新位图的配置设置为指定的配置,然后将此位图的像素复制到新位图中。
并且您可以设置图像的配置。
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap.
and you can set config of image.