首页 文章

如何在android中以编程方式为图像视图设置色调?

提问于
浏览
303

需要为图像视图设置色调...我使用以下方式:

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

但它没有改变......

18 回答

  • 720

    您可以通过以下方式在代码中轻松更改色调:

    imageView.setColorFilter(Color.argb(255, 255, 255, 255)); //白色调

    如果你想要色彩

    imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
    

    对于Vector Drawable

    imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);
    

    UPDATE
    @ADev在他的回答here中有更新的解决方案,但他的解决方案需要更新的支持库--25.4.0或更高版本 .

  • 12

    大多数答案都是指使用 setColorFilter ,这不是最初的问题 .

    用户@Tad的方向正确his answer但它仅适用于API 21 .

    要在所有Android版本上设置色调,请使用 ImageViewCompat

    ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
    

    请注意,在这种情况下 yourTint 必须是"color int" . 如果您有像 R.color.blue 这样的颜色资源,则需要先加载颜色int:

    ContextCompat.getColor(context, R.color.blue);
    
  • 1

    这对我有用

    mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
    
  • 3

    @Hardik没错 . 代码中的另一个错误是当您引用XML定义的颜色时 . 当您应该使用ID来定位颜色资源时,您只将id传递给 setColorFilter 方法,并将资源传递给 setColorFilter 方法 . 重写下面的原始代码 .

    If this line is within your activity:

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

    Else, you need to reference your main activity:

    Activity main = ...
    imageView.setColorFilter(main.getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);
    

    请注意,其他类型的资源也是如此,例如整数,bool,尺寸等 . 除了字符串,您可以直接在Activity中使用 getString() ,而无需先调用 getResources() (不要问我为什么) .

    否则,您的代码看起来不错 . (虽然我没有过多地调查 setColorFilter 方法......)

  • 5

    在我尝试了所有方法后,它们对我不起作用 .

    我通过使用另一个PortDuff.MODE得到解决方案 .

    imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
    
  • 2

    从Lollipop开始,BitmapDrawables的tint方法也适用于新的Palette类:

    public void setTintList(ColorStateList tint)

    public void setTintMode(PorterDuff.Mode tintMode)

    在旧版Android上,您现在可以使用DrawableCompat

  • -3

    试试这个 . 它应该适用于支持库支持的所有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;
    }
    

    您可以使用上述任何一项来使其工作 .

    您可以在文档here上阅读有关DrawableCompat的更多有趣功能 .

  • 0

    简单而且一条线

    imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));
    
  • 34

    从Lollipop开始,有一个名为ImageView#setImageTintList()的方法,您可以使用...优点是它需要 ColorStateList 而不是单一颜色,从而使图像的色调状态感知 .

    在前Lollipop设备上,您可以通过着色drawable然后将其设置为 ImageView 的图像drawable来获得相同的行为:

    ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_clr_selector);
    Drawable drawable = DrawableCompat.wrap(imageView.getDrawable());
    DrawableCompat.setTintList(drawable, csl);
    imageView.setImageDrawable(drawable);
    
  • 4
    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);
    
  • 17

    由于第一个答案对我不起作用:

    //get ImageView
    ImageView myImageView = (ImageView) findViewById(R.id.iv);
    
    //colorid is the id of a color defined in values/colors.xml
    myImageView.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.colorid)));
    

    这似乎只适用于API 21,但对我来说这不是问题 . 您可以使用ImageViewCompat来解决该问题 .

    我希望我能帮助任何人:-)

  • 35

    如果您的颜色具有十六进制透明度,请使用以下代码 .

    ImageViewCompat.setImageTintMode(img,PorterDuff.Mode.SRC_ATOP);
    ImageViewCompat.setImageTintList(img,ColorStateList.valueOf(Color.parseColor("#80000000")));
    

    清除色彩

    ImageViewCompat.setImageTintList(img, null);
    
  • 1

    我发现我们可以使用颜色选择器来设置色调:

    mImageView.setEnabled(true);
    

    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>
    
  • 101

    不要使用 PoterDuff.Mode ,使用 setColorFilter() 它适用于所有人 .

    ImageView imageView = (ImageView) listItem.findViewById(R.id.imageView);
    imageView.setColorFilter(getContext().getResources().getColor(R.color.msg_read));
    
  • 2

    正如@milosmns所说,你应该使用 imageView.setColorFilter(getResouces().getColor(R.color.blue),android.graphics.PorterDuff.Mode.MULTIPLY);

    此API需要颜色值而不是颜色资源ID,这是您的语句无效的根本原因 .

  • 6

    我在聚会上迟到了,但我没有看到我的上述情绪 . 我们也可以通过 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)
    
  • 9

    如果您想将选择器设置为您的色调:

    ImageViewCompat.setImageTintList(iv, getResources().getColorStateList(R.color.app_icon_click_color));
    
  • 9

    不是确切的答案,但更简单的替代方案:

    • 将另一个视图放在图像顶部

    • 根据需要(以编程方式)更改视图的 alpha 值以获得所需的效果 .

    这是一个代码片段:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="@dimen/height120"
            android:contentDescription="@string/my_description"
            android:scaleType="fitXY"
            android:src="@drawable/my_awesome_image"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/height120"
            android:alpha="0.5"
            android:background="@color/my_blue_color"/>
    </FrameLayout>
    

相关问题