应用多彩色滤光片,以相同的绘制 [英] Apply many color filters to the same drawable

查看:199
本文介绍了应用多彩色滤光片,以相同的绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在链应用几个彩色滤光片为绘制。那可能吗?或者,也许要创建一个过滤器是我想要应用的过滤器的组合。

例如,我想:

 绘制对象D = ...;
d.setColorFilter(0x3F000000,Mode.OVERLAY).setColorFilter(0xFF2D2D2D,Mode.SCREEN)
 

解决方案

这是我最后使用方法:操纵上的绘制对象位图帆布键,因为我需要申请成为许多层,使用油漆,它的作品不仅为彩色滤光片,而且对于任何种类的图像混合的。

  ...
可绘制myBackground = createBackground(getResources()的getColor(R.color.Green)。);
setBackgroundDrawable(myBackground);
...

私人绘制对象createBackground(INT颜色){

    帆布油画=新的Canvas();
    位图按钮画面= BitmapFactory.de codeResource(getResources(),R.drawable.btn_image);
    位图buttonShadows = BitmapFactory.de codeResource(getResources(),R.drawable.btn_shadows);
    位图buttonHighLights = BitmapFactory.de codeResource(getResources(),R.drawable.btn_highlights);
    位图的结果= Bitmap.createBitmap(buttonImage.getWidth(),buttonImage.getHeight(),Bitmap.Config.ARGB_8888);

    canvas.setBitmap(结果);
    涂料粉刷=新的油漆();
    paint.setFilterBitmap(假);

    // 颜色
    paint.setColorFilter(新PorterDuffColorFilter(颜色,Mode.MULTIPLY));
    canvas.drawBitmap(按钮画面,0,0,油漆);
    paint.setColorFilter(空);
    //阴影
    paint.setXfermode(新PorterDuffXfermode(Mode.MULTIPLY));
    canvas.drawBitmap(buttonShadows,0,0,油漆);
    //亮点
    paint.setXfermode(新PorterDuffXfermode(Mode.SCREEN));
    canvas.drawBitmap(buttonHighLights,0,0,油漆);

    paint.setXfermode(空);
    返回新BitmapDrawable(getResources(),结果);
}
 

警告: setBackgroundDrawable(可绘制D)是德precated,而的setBackground(可绘制D)只能从API 16日,所以如果你有像我这样的一个分目标的API-14的最大目标API-17,你有没有干净的方式来设置可绘制作为背景。我贴的德precated电话。

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)

解决方案

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.

这篇关于应用多彩色滤光片,以相同的绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆