简体   繁体   中英

Alternative to setColorFilter(int,Mode)

I have seen that the following method is deprecated: public void setColorFilter (int color, PorterDuff.Mode mode) . In the Android documentation I have seen the following recommendation:

This method was deprecated in API level 29.
use setColorFilter(android.graphics.ColorFilter) with an instance of BlendModeColorFilter

I am trying to replace that method to a non-deprecated method but I have not achieved it. I have seen similar posts to this one but they are kotlin version. How could I replace this deprecated method for a non-deprecated method in Java?

Thanks

For me, this works in Java:

Drawable drawable = AppCompatResources.getDrawable(context, R.id.some_drawable_id);
drawable.setColorFilter(
   BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
      color, 
      BlendModeCompat.SRC_ATOP
   )
);

Change the color to you desired color and the some_drawable_id to your drawable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM