setColorFilter not working

后端 未结 10 1443
春和景丽
春和景丽 2020-12-05 12:43

I\'m trying to implement a simple colorfilter on an imageview to turn the black image into a white image. In order to achieve that I do the following:

    we         


        
相关标签:
10条回答
  • 2020-12-05 13:37

    As much as I hate to answer my own questions I found the problem: I should've used:

       weatherImg.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2020-12-05 13:37

    We use this code

    Drawable drawable = DrawableCompat.wrap(getDrawable(drawableResource));
            drawable.mutate();
            DrawableCompat.setTint(drawable, getColor(color));
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    
    public static int getColor(int id) {
            return ContextCompat.getColor(getInstance().context, id);
        }
    
    public static Drawable getDrawable(int id) {
            return ContextCompat.getDrawable(getInstance().context, id);
        }
    
    0 讨论(0)
  • 2020-12-05 13:39

    Had the same issue on Android 6. Solved by using ImageView.getDrawable().setColorFilter() instead of ImageView.setColorFilter().

    0 讨论(0)
  • 2020-12-05 13:43

    For me, simply calling setColorFilter() on the ImageView wasn't working.

    imageView.setColorFilter(ResourcesCompat.getColor(resources, color, null)) //didnt work on 21, only 22+
    

    For whatever reason, on API 21, the only way I could get setColorFilter() to work properly was to post the change to the views message queue.

    imageView.post { imageView.setColorFilter(ResourcesCompat.getColor(resources, color, null)) } //this works on 21+
    
    0 讨论(0)
提交回复
热议问题