【问题标题】:android imageview's tint set in xml overrides tint set on DrawableCompat programmaticallyandroid imageview在xml中设置的色调以编程方式覆盖在DrawableCompat上设置的色调
【发布时间】:2018-02-17 04:36:15
【问题描述】:

所以改天,另一个问题!我设置了一个带有色调的ImageView,如下:

<ImageView
    android:id="@+id/image_money"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:padding="3dp"
    android:src="@drawable/ic_money_icon"
    android:tint="#c7c7c7"/>

而且我需要还原或设置tintColor,所以我使用DrawableCompat如下:

Drawable imagem = holder.view.getContext().getResources().getDrawable(R.drawable.ic_money_icon);
imagem = DrawableCompat.wrap(imagem);
imagem = imagem.mutate();
DrawableCompat.setTint(imagem,Color.parseColor("#43a085"));
holder.imageDebito.setImageDrawable(imagem);

我检查了imagem 上的位图,它看起来应该是我设置的颜色,但是当应用于 ImageView imageDebito 时,它会恢复为 XML 上设置的色调。如果我拍摄视图中未使用的不同图像,应用色调,然后在 ImageView 上设置它,它会从 XML 中获得相同的色调...我尝试设置 setImageTintList(),但它不是'在 API 级别 17 上不可用...

因此,我需要删除 tint 属性,或者在 xml 中对 ImageView 的色调强制执行图像上的色调。

【问题讨论】:

    标签: android android-layout android-support-library android-drawable


    【解决方案1】:

    使用setColorFilter() 方法。

    Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
    ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
    myIcon.setColorFilter(filter);
    

    编辑:刚刚改进了格式...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2013-01-06
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2013-07-23
      相关资源
      最近更新 更多