1.使用ColorFilter
MaskFilter是对一个Paint的alpha通道的转换,而ColorFilter则是对每一个RGB通道应用转换 。所有由ColorFilter所派生的类在执行它们的转换时,都会忽略alpha通道。
Android包含三个ColorFilter:
ColorMatrixColorFilter 可以指定一个4×5的ColorMatrix并将其应用到一个Paint中。ColorMatrixes通常在程序中用于对图像进行处理 ,而且由于它们支持使用矩阵相乘的方法来执行链接转换,所以它们很有用。
LightingColorFilter 乘以第一个颜色的RGB通道,然后加上第二个颜色。每一次转换的结果都限制在0到255之间。
PorterDuffColorFilter 可以使用数字图像合成的16条Porter-Duff 规则中的任意一条来向Paint应用一个指定的颜色。
使用setColorFilter方法应用ColorFilter,如下所示:
- myPaint.setColorFilter(new LightingColorFilter(Color.BLUE, Color.RED));
API中的ColorMatrixSample是说明如何使用ColorFilter和Color Matrix的非常好的例子。
2.
- private static void setContrast(ColorMatrix cm, float contrast) {
- float scale = contrast + 1.f;
- float translate = (-.5f * scale + .5f) * 255.f;
- cm.set(new float[] {
- scale, 0, 0, 0, translate,
- 0, scale, 0, 0, translate,
- 0, 0, scale, 0, translate,
- 0, 0, 0, 1, 0 });
- }
- float contrast = mAngle / 180.f;
- setContrast(cm, contrast);
- paint.setColorFilter(new ColorMatrixColorFilter(cm));
- canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);