如何更改已保存透明位图的背景颜色
我正在将 png 图像打开为位图,对其进行一些修改,然后将其作为 jpg 保存到磁盘。如果 png 有一些透明区域,它们将保存为黑色。有没有办法更改此默认行为,以便使用不同的颜色背景(例如白色)保存图像?
谢谢
I am opening a png image into a Bitmap, making some modifications to it and then saving it to disk as a jpg. In the case where the png has some transparent areas, they are saved as black. Is there a way to change this default behavior so the image is saved with a different color background such as white?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将其绘制到新的位图,例如
然后保存新的位图
You could draw it to a new bitmap, e.g.
then save new bitmap instead
要保存图像并保留其透明区域,您不能将其保存为 JPG,而必须将其保存为 PNG,不仅如此,还要将
setHasAlpha()
的设置设置为 true 在保存图像之前,所以它会像这样:在保存之前:
并且在保存时,使用您用于保存的任何方法将图像保存为PNG,例如:
To save an image and retain its transparent areas you can't save it as JPG, you have to save it as PNG, and not only that, but setting the setting of
setHasAlpha()
to true BEFORE saving the image, so it would be like that:before saving:
And when saving, save the image as PNG using whatever the method you are using for saving, for example:
对于 Kotlin,您可以使用此扩展函数
For Kotlin you can use this Extension function