画像ビューの色合いを設定する必要があります...次のように使用しています:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
でも変わらない…
これは私のために働いた
mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
すべての方法を試した後、うまくいきませんでした。
別の PortDuff.MODE を使用して解決策を取得します。
imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
Lollipop から、新しい Palette クラスで動作する BitmapDrawablesの色合いメソッドもあります。
public void setTintList (ColorStateList ティント)
と
public void setTintMode (PorterDuff.Mode tintMode)
古いバージョンの Android では、 DrawableCompatライブラリを使用できるようになりました
これを試して。サポート ライブラリがサポートするすべての 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 のさらに興味深い機能については、こちらのドキュメントを参照してください。
色合い属性にカラーセレクターを使用できることがわかりました。
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>
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 プロジェクトでも役立つ機能だと思います。
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);
@milosmnsが言ったように、使用する必要があります
imageView.setColorFilter(getResouces().getColor(R.color.blue),android.graphics.PorterDuff.Mode.MULTIPLY);
この API には、カラー リソース ID の代わりにカラー値が必要です。これが、ステートメントが機能しなかった根本的な原因です。
私はパーティーに遅れましたが、上記の解決策が見られませんでした。を介して色合いを設定することもでき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)