8

Android でビットマップの色を変更する方法を見つける必要があります。値に応じて、アプリケーションで楕円形の画像の色をスムーズに置換/変更する必要がありintます。myValue=5画像の色を に変更し、色を に変更する場合REDのようなものが必要です。私がこれを行うことができる唯一の方法は、次のようなxmlファイルを使用することでした:myValue=322BLUE

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
 <solid android:color="#cccccc"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
     android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>
</shape>

その後、画像リソースmyValueを設定するように変更されます。ImageViewしかし、この方法では、35 の異なる xml ファイルを作成する必要があります...これは良い考えではないと思います。

これを行うためのより良い解決策を提案できる人はいますか?

4

6 に答える 6

21

これが私がこの問題を解決した方法です:

  1. ImageViewwith を宣言するsrc="@drawable/button"
  2. を作成してそれDrawableに設定ColorFilterし、その後、次のImageViewように宣言された src として使用します。

>

Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
color.setImageDrawable(myIcon);
于 2012-05-07T14:19:03.433 に答える
16

この解決策は私にはうまくいきません。一部の画像では、最終的な色が間違っていました。代わりにこのソリューションを使用します:

Drawable myIcon = getResources().getDrawable(R.drawable.your_image); 
myIcon.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP); 
((ImageView)findViewById(R.id.view_to_change)).setImageDrawable(myIcon);
于 2014-01-24T21:23:14.683 に答える
3
getResources().getDrawable( R.drawable.button );

現在は推奨されていません。このようにすることもできます:

((ImageView) findViewById(R.id.my_icon))
  .setColorFilter(new LightingColorFilter(Color.BLUE, Color.BLUE));
于 2015-10-22T01:17:54.757 に答える
1

あなたはこれをする必要があります。

Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);
于 2013-03-07T08:52:38.510 に答える
0

これを実現するために TransitionDrawable を使用できます - http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html

于 2012-05-04T13:40:22.220 に答える