Android的位图:将透明像素颜色 [英] Android Bitmap: Convert transparent pixels to a color

查看:128
本文介绍了Android的位图:将透明像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Android app that loads an image as a bitmap and displays it in an ImageView. The problem is that the image appears to have a transparent background; this causes some of the black text on the image to disappear against the black background.

如果我设置ImageView的背景为白色,那种作品,但我得到它被拉伸以适应父(实际的图像缩放在中间),在图像上难看的大边框

If I set the ImageView background to white, that sort of works, but I get ugly big borders on the image where it is stretched to fit the parent (the actual image is scaled in the middle).

所以 - 我要转换的透明像素的位图,以纯色 - 但我无法弄清楚如何做到这一点。

So - I want to convert the transparent pixels in the Bitmap to a solid colour - but I cannot figure out how to do it!

任何帮助将是AP preciate!

Any help would be appreciate!

谢谢 克里斯

推荐答案

如果您是包括图像作为一种资源,这是最简单的,只是你自己编辑图像像的 GIMP 。您可以添加你的背景有,并确保它是什么将会是什么样的,并没有使用到的处理能力在加载每次修改图像。

If you are including the image as a resource, it is easiest to just edit the image yourself in a program like gimp. You can add your background there, and be sure of what it is going to look like and don't have use to processing power modifying the image each time it is loaded.

如果您还没有过像自己控制,你可以做一些类似,假设你的位图图片

If you do not have control over the image yourself, you can modify it by doing something like, assuming your Bitmap is called 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 

这篇关于Android的位图:将透明像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆