Alternative to MULTIPLY mode

Viewed 32

I had a function that was using a deprecated method (public void setColorFilter (int color, PorterDuff.Mode mode)) and I have tried to change it using a non-deprecated method but the problem is that now using the non-deprecated method the MULTIPLY method needs API level 29:

"Field requires API level 29 (current min is 21): MULTIPLY

This is the function I was using with the deprecated method (here the MULTIPLY doesn't give that warning of required API level) ->

 public static void setcol(TextView dLabel) {
       
dLabel.getBackground().setColorFilter(color,android.graphics.PorterDuff.Mode.MULTIPLY);
}

I have tried to replace the above function to stop using the deprecated method. This is what I have, but the problem is that using this alterntive, the MULTIPLY gives that warning I have said.

 public static void setcol(TextView dLabel) {
           
     dLabel.getBackground().setColorFilter(
                BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
                        color,
                        BlendModeCompat.MULTIPLY
                )
        );
}

How could I solve it? I have thought of changing the MULTIPLY mode for a similar mode, but I don´t know how could I do it so that it is the same. Thanks in advance.

EDIT:

@SuppressWarnings("deprecation")
public static void setcol(TextView dLabel) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        dLabel.getBackground().setColorFilter(
            color, 
            android.graphics.PorterDuff.Mode.MULTIPLY
        );
    } else {
        dLabel.getBackground().setColorFilter(
            BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
                color,
                BlendModeCompat.MULTIPLY
            )
        );
    }
}
0 Answers
Related