2011-03-01 10 views
0

私はAndroid用のアプリを書いています。Android:背景画像の特定の(緑の)色を透明にする

xmlファイルのレイアウトを定義するTabHostには6つのタブがあり、すべて同じ背景画像「settingsdlg.gif」を持っています。 styles.xmlで

<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" > 
.. 

私はウィンドウが透明でなければならないことを指定:

<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に伝える方法はありますか?

ありがとうございます!

答えて

1

すべての緑色のピクセルを透明なものに変更する必要があります。次に例を示します:How to change colors of a Drawable in Android?

画像の中央に緑色のピクセルがある場合、問題が発生する可能性があります。つまり、イメージに一定のサイズと形状がある場合は、マスクを作成し、xferモードを使用して透明な丸いコーナーで新しいイメージを作成します。

+0

感謝を!問題を解決しました – iseeall

1

は、誰もが同じ問題を抱えていただけであれば、ここでのコードは次のとおりです。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; 
} 
1

このコードスニペットは、私の仕事:

PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(
    getResources().getColor(R.color.your_color), 
    PorterDuff.Mode.MULTIPLY 
); 
imgView.getDrawable().setColorFilter(porterDuffColorFilter); 
imgView.setBackgroundColor(Color.TRANSPARENT); 
関連する問題