Activity实现

1
2
3
4
5
Paint mPaint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);

加在Activity的onCreate()中

Fragment实现

1
2
3
4
5
Paint mPaint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
getView().setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);

加在Fragment的onViewCreated()中

总结

把View的饱和度设置为0即为变灰
主要是最后的setLayerType(View.LAYER_TYPE_HARDWARE, mPaint),在需要变灰的View层级执行。