ilo*_*mbo 7 android chaining colorfilter android-drawable
我想在链中应用几个彩色滤镜到drawable.那可能吗?或者也许创建一个过滤器,它是我想要应用的过滤器的组合.
例如,我想:
Drawable d = ...;
d.setColorFilter(0x3F000000, Mode.OVERLAY).setColorFilter(0xFF2D2D2D, Mode.SCREEN)
Run Code Online (Sandbox Code Playgroud)
这是我最终使用的方法:Drawable在a上操作位图Canvas并根据需要应用尽可能多的图层,使用Paint它不仅适用于彩色滤镜,还适用于任何类型的图像混合.
...
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);
}
Run Code Online (Sandbox Code Playgroud)
警告: setBackgroundDrawable(Drawable d)已被弃用,虽然setBackground(Drawable d)只能从api 16开始使用,所以如果你有像我这样的最小目标api-14最大目标api-17你没有"干净"的方式将drawable设置为背景.我坚持不推荐的电话.