如何在Android上去饱和视图?

时间:2016-05-16 13:34:07

标签: android view viewgroup

我有一些包含一些视图的LinearLayout。是否可以这样做,使LinearLayout内的所有内容都以灰度颜色模式绘制?

1 个答案:

答案 0 :(得分:0)

您需要扩展LinearLayout并覆盖dispatchDraw方法。

然后创建一个大小为LinearLayout和画布的Bitmap。并使用新画布作为参数调用super.dispatchDraw。之后,使用绘图设置ColorFilter在原始画布上绘制位图。

这里的例子是:

private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;

public {Constructor}{
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);

    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
    mPaint.setColorFilter(colorFilter);
}

@Override
public void onSizeChanged(int w, int h, in oldw, int oldh){
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Camvas(mBitmap);
}

@Override
public void dispatchDraw(Canvas canvas){
    super.dispatchDraw(mCanvas);

    canvas.drawBitmap(mBitmap, 0, 0, mPaint);
}