java - 安卓位图: Convert transparent pixels to a color

标签 java android image

我有一个 Android 应用程序将图像加载为位图并将其显示在 ImageView 中。问题是图像似乎具有透明背景;这会导致图像上的一些黑色文本在黑色背景下消​​失。

如果我将 ImageView 背景设置为白色,则可以,但我会在图像上得到难看的大边框,它被拉伸(stretch)以适合父级(实际图像在中间缩放)。

所以 - 我想将位图中的透明像素转换为纯色 - 但我不知道该怎么做!

任何帮助将不胜感激!

谢谢 克里斯

最佳答案

如果您将图像作为资源包含在内,最简单的方法是自己在 gimp 之类的程序中编辑图像。 .你可以在那里添加你的背景,并确定它会是什么样子,并且不需要在每次加载时修改图像的处理能力。

如果您自己无法控制图像,则可以通过执行类似的操作来修改它,假设您的 Bitmap 被称为 image

Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig());  // Create another image the same size
imageWithBG.eraseColor(Color.WHITE);  // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle();  // clear out old image 

关于java - 安卓位图: Convert transparent pixels to a color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14531674/

相关文章:

java - 当我通过 java 应用程序中的进程运行 pg_restore 时,应用程序卡住

java - 如何在 Jackson 自定义 JSON 反序列化器中使用常规 JSON 解释

android - 如何从 Rikulu 调用 Android 原生代码?

jquery - 如何通过Ajax使用fileuploader保存图像

java - 在 Gradle 构建期间排除类路径中的文件?

java - JsonPath DocumentContext 更新而不更新原始对象

android - 如何在 Canvas 上绘制 API 级别低于 21 的圆角矩形

java - eclipse 中的 R.menu 错误

c# - 优化 : VB. 从文件中加载图像并即时调整大小;目标:快速

css - 图像左侧的 Div