Android:使背景图像中的特定(绿色)颜色透明
问题描述:
我正在为Android编写应用程序。Android:使背景图像中的特定(绿色)颜色透明
在定义布局的xml文件中我有一个带有6个选项卡的TabHost,它们都具有相同的大背景图像“settingsdlg.gif”。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/settingsdlg" >
..
在styles.xml我指定该窗口必须是透明的:
<resources>
<style name="my_app_style" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
的问题是,在背景图像“settingsdlg.gif”是四舍五入 RECT和小区域在边缘应该是透明的,是绿色的。
我知道要在Android上的图片中使用透明度,图片必须采用PNG格式,并且我想透明的像素应该保存为PNG透明。
不幸的是,我从数据库中获取图像,我无法更改它们,因为它们也用于Win32和Mac的其他应用程序。
有没有办法告诉Android,在背景图像中,绿色像素应该呈现透明?
谢谢!
答
您必须将每个绿色像素更改为透明。这里是一个例子:How to change colors of a Drawable in Android?
但是,如果在图像中间有绿色像素,那么你可以有一个问题。因此,另一种方式是,如果图像具有恒定的大小和形状,则可以创建蒙版并使用xfer模式创建带有透明圆角的新图像。
答
只是,如果任何人有同样的问题,这里是代码:
在utils的:
public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap, int bgColor) {
Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
int nWidth = result.getWidth();
int nHeight = result.getHeight();
for (int y = 0; y < nHeight; ++y)
for (int x = 0; x < nWidth; ++x) {
int nPixelColor = result.getPixel(x, y);
if (nPixelColor == bgColor)
result.setPixel(x, y, Color.TRANSPARENT);
}
return result;
}
答
这段代码为我工作:
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(
getResources().getColor(R.color.your_color),
PorterDuff.Mode.MULTIPLY
);
imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);
谢谢!解决了这个问题 – iseeall 2011-03-02 10:13:21