androidchainingandroid-drawablecolorfilter

Apply many color filters to the same drawable


I want to apply several color filters in chain to a drawable. Is that possible? Or maybe to create a filter that is the combination of the filters I want to apply.

For example, I would like:

Drawable d = ...;
d.setColorFilter(0x3F000000, Mode.OVERLAY).setColorFilter(0xFF2D2D2D, Mode.SCREEN)

Solution

  • This is the approach I ended using: Manipulate the Drawable bitmap on a Canvas and apply as many layers as I need, using Paint, it works not only for color filters, but also for any kind of image blending.

    ...
    Drawable myBackground = createBackground(getResources().getColor(R.color.Green)); 
    setBackgroundDrawable(myBackground);
    ...
    
    private Drawable createBackground(int color) {
    
        Canvas canvas = new Canvas();
        Bitmap buttonImage = BitmapFactory.decodeResource(getResources(), R.drawable.btn_image);
        Bitmap buttonShadows = BitmapFactory.decodeResource(getResources(), R.drawable.btn_shadows);
        Bitmap buttonHighLights = BitmapFactory.decodeResource(getResources(), R.drawable.btn_highlights);
        Bitmap result = Bitmap.createBitmap(buttonImage.getWidth(), buttonImage.getHeight(), Bitmap.Config.ARGB_8888);
    
        canvas.setBitmap(result);
        Paint paint = new Paint();
        paint.setFilterBitmap(false);
    
        // Color
        paint.setColorFilter(new PorterDuffColorFilter(color, Mode.MULTIPLY));
        canvas.drawBitmap(buttonImage, 0, 0, paint);
        paint.setColorFilter(null);
        // Shadows
        paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
        canvas.drawBitmap(buttonShadows, 0, 0, paint);
        // HighLights
        paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
        canvas.drawBitmap(buttonHighLights, 0, 0, paint);
    
        paint.setXfermode(null);
        return new BitmapDrawable(getResources(), result);
    }
    

    Caveat: setBackgroundDrawable(Drawable d) is deprecated, while setBackground(Drawable d) is only available from api 16 on, so if you have like me a min target api-14 max target api-17 you have no "clean" way to set the drawable as background. I sticked with the deprecated call.