i am writing an app for Android.
in an xml file defining layout I have a TabHost with 6 Tabs, which all have the same big background image "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" >
..
In styles.xml I specify that the window must be transparent:
<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>
The problem is that the background image "settingsdlg.gif" is a rounded rect, and the small areas at the edges, which should be transparent, are green.
I know that to use transparency in images on Android, the images must be in PNG format, and the pixels i want transparent should be saved in PNG as transparent.
Unfortunately I get the images from a database and I can't change them, because they are also used in some other application for Win32 and Mac.
Is there a way to tell Android that in the background image the green pixels should be rendered transparent?
Thanks!
You would have to change every green pixel into a transparent one. Here is an example: How to change colors of a Drawable in Android?
However if there are green pixels in the middle of the image then you can have a problem. So the other way is, if your image has constant size and shape, to create a mask and use xfer modes to create a new image with transparent rounded corners.
Just if anyone had the same problem, here is the code:
//remove green edges from bg image
Bitmap bgBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.settingsdlg)).getBitmap();
Bitmap transparentBgBitmap = Utils.getBitmapWithTransparentBG(bgBitmap, Color.GREEN);
tabHost.setBackgroundDrawable(new BitmapDrawable(transparentBgBitmap));
in 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;
}
This code snippet worked for me:
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(
getResources().getColor(R.color.your_color),
PorterDuff.Mode.MULTIPLY
);
imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);