Apply many color filters to the same drawable

那年仲夏 提交于 2019-12-22 04:49:22

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/16346545/apply-many-color-filters-to-the-same-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!