522

画像ビューの色合いを設定する必要があります...次のように使用しています:

imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);

でも変わらない…

4

24 に答える 24

68

これは私のために働いた

mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
于 2015-10-14T17:59:01.067 に答える
25

すべての方法を試した後、うまくいきませんでした。

別の PortDuff.MODE を使用して解決策を取得します。

imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
于 2016-10-25T13:53:21.263 に答える
17

Lollipop から、新しい Palette クラスで動作する BitmapDrawablesの色合いメソッドもあります。

public void setTintList (ColorStateList ティント)

public void setTintMode (PorterDuff.Mode tintMode)

古いバージョンの Android では、 DrawableCompatライブラリを使用できるようになりました

于 2015-01-13T18:43:08.733 に答える
14

これを試して。サポート ライブラリがサポートするすべての Android バージョンで動作するはずです。

public static Drawable getTintedDrawableOfColorResId(@NonNull Context context, @NonNull Bitmap inputBitmap, @ColorRes int colorResId) {
    return getTintedDrawable(context, new BitmapDrawable(context.getResources(), inputBitmap), ContextCompat.getColor(context, colorResId));
}

public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Bitmap inputBitmap, @ColorInt int color) {
    return getTintedDrawable(context, new BitmapDrawable(context.getResources(), inputBitmap), color);
}

public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Drawable inputDrawable, @ColorInt int color) {
    Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable);
    DrawableCompat.setTint(wrapDrawable, color);
    DrawableCompat.setTintMode(wrapDrawable, PorterDuff.Mode.SRC_IN);
    return wrapDrawable;
}

上記のいずれかを使用して、機能させることができます。

DrawableCompat のさらに興味深い機能については、こちらのドキュメントを参照してください。

于 2015-12-27T09:08:54.563 に答える
11

色合い属性にカラーセレクターを使用できることがわかりました。

mImageView.setEnabled(true);

activity_main.xml:

<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_arrowup"
    android:tint="@color/section_arrowup_color" />

section_arrowup_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" android:state_enabled="true"/>
    <item android:color="@android:color/black" android:state_enabled="false"/>
    <item android:color="@android:color/white"/>
</selector>
于 2017-06-14T09:41:36.437 に答える
10

Kotlin とその便利な拡張機能が広く採用されているため、 ADev回答(私の意見ではこれが最も正しい) に追加します。

fun ImageView.setTint(context: Context, @ColorRes colorId: Int) {
    val color = ContextCompat.getColor(context, colorId)
    val colorStateList = ColorStateList.valueOf(color)
    ImageViewCompat.setImageTintList(this, colorStateList)
}

これは、どの Android プロジェクトでも役立つ機能だと思います。

于 2019-04-25T08:14:45.967 に答える
6
Random random=new Random;
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
ColorFilter cf = new PorterDuffColorFilter(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)),Mode.OVERLAY);

imageView.setImageResource(R.drawable.ic_bg_box);
imageView.setColorFilter(cf);
于 2014-04-28T12:16:32.270 に答える
3

@milosmnsが言ったように、使用する必要があります imageView.setColorFilter(getResouces().getColor(R.color.blue),android.graphics.PorterDuff.Mode.MULTIPLY);

この API には、カラー リソース ID の代わりにカラー値が必要です。これが、ステートメントが機能しなかった根本的な原因です。

于 2015-10-08T15:09:23.070 に答える
2

私はパーティーに遅れましたが、上記の解決策が見られませんでした。を介して色合いを設定することもできsetImageResource()ます (私の minSdkVersion は 24 です)。

そのため、まずセレクターを作成し、それをアセット フォルダーに保存する必要があり/drawableます (私はそれを と呼びますic_color_white_green_search.xml) 。

<!-- Focused and not pressed -->
<item android:state_focused="true"
      android:state_pressed="false">

    <bitmap android:src="@drawable/ic_search"
            android:tint="@color/branding_green"/>
</item>

<!-- Focused and pressed -->
<item android:state_focused="true"
      android:state_pressed="true">

    <bitmap android:src="@drawable/ic_search"
            android:tint="@color/branding_green"/>
</item>

<!-- Default -->
<item android:drawable="@drawable/ic_search"/>

次に、次のようなコードで設定します。

val icon = itemView.findViewById(R.id.icon) as ImageButton
icon.setImageResource(R.drawable.ic_color_white_green_search)
于 2018-12-13T19:27:30.223 に答える