Android page grayed out
Solution 1. The Android page drawing process is generally divided into measure
, layout
, and draw
, and the page graying draw
is implemented in the method. draw
The method parameters include canvas
, paint
, canvas
which is the canvas of the interface, paint
and the brush for drawing the interface. You can change the interface background color by changing paint
the properties, and change the hue, saturation and brightness of the brush by setting the color filter. Set the saturation of the gray effect to 0, and the setting code of the brush:
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setStaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Solution 2. Considering performance Use HardwareLayer (the Buffer inside the GPU) to cache the drawn graphics. The setter method setLayerType()
forces View
the creation of its own corresponding layer, and draws itself onto the layer.
Solution 3. Set graying on the Activity
top layer to achieve the global graying effect. View
Get the root of the interface View
:
//java
View view = activity. getWindow(). getDecorView();
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setStaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
view. setLayerType(View.LAYER_TYPE_HARDWARE, paint);
//kotlin
val view: View = window.decorView
val paint = Paint()
val cm = ColorMatrix()
cm.setSaturation(0f)
paint.colorFilter = ColorMatrixColorFilter(cm)
view.setLayerType(View.LAYER_TYPE_HARDWARE, paint)