Careful children’s shoes will find that on special festivals such as Tomb-sweeping Day, many apps have set the grey and white mode in line with the theme, such as JINGdong, as shown in the picture:

Let’s take a look at the final implementation:

Then today we will introduce three schemes to set the global gray mode:

Solution a:

This is also my reply to the child’s plan: set the top View of the Activity to gray, to achieve the global gray effect, let’s look at the specific implementation process.

You can set the grayscale using the ColorMatrix in the onCreate method of BaseActivity

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        / / a
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);// Grayscale effect
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE,paint);
    }
Copy the code

That’s how it works, and it’s relatively easy.

Scheme 2:

This method uses custom layout and adds a layer of black and white bitmap to the DispatchDraw method to make the interface open in black and white mode. But the shortcomings are obvious, and the application is quite stuck.

1. First you need to define a GrayFrameLayout layout


public class GrayFrameLayout extends FrameLayout {
    private Paint mPaint = new Paint();

    public GrayFrameLayout(@NonNull Context context) {
        super(context);
    }

    public GrayFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.onDraw(canvas);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas); }}Copy the code

2. In the BaseActivity onCreateView method, do the following

 	@Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        
        / / 2
        if("FrameLayout".equals(name)){
            int attributeCount = attrs.getAttributeCount();
            for (int i = 0; i < attributeCount; i++) {
                String attributeName = attrs.getAttributeName(i);
                String attributeValue = attrs.getAttributeValue(i);
                if(attributeName.equals("id")) {int id = Integer.parseInt(attributeValue.substring(1));
                    String resourceName = getResources().getResourceName(id);
                    if("android:id/content".equals(resourceName)){
                        GrayFrameLayout frameLayout  = new GrayFrameLayout(this,attrs);
                        returnframeLayout; }}}}return super.onCreateView(parent, name, context, attrs);
    }
Copy the code

Plan 3

Some special controls need to be grayed out, such as WebView, H5 page, video, etc

1. Create a greyed management class


public class GrayManager {

    private static GrayManager mInstance;
    private Paint mGrayPaint;
    private ColorMatrix mGrayMatrix;

    public static GrayManager getInstance(a) {
        if (mInstance == null) {
            synchronized (GrayManager.class) {
                if (mInstance == null) {
                    mInstance = newGrayManager(); }}}return mInstance;
    }

    / / initialization
    public void init(a) {
        mGrayMatrix = new ColorMatrix();
        mGrayPaint = new Paint();
        mGrayMatrix.setSaturation(0);
        mGrayPaint.setColorFilter(new ColorMatrixColorFilter(mGrayMatrix));
    }


    // Hardware-accelerated deashing method
    public void setLayerGrayType(View view) {
        if (mGrayMatrix == null || mGrayPaint == null) { init(); } view.setLayerType(View.LAYER_TYPE_HARDWARE, mGrayPaint); }}Copy the code

Call setLayerGrayType() to pass the view to a special control that needs to be grayed. For example, if you want an Activity to be grayed in demo, call the setLayerGrayType() function inside the Activity:

GrayManager.getInstance().setLayerGrayType(getWindow().getDecorView());
Copy the code

The above three schemes can realize the gray mode, which has also been verified by demo test. However, due to the narrow test scope, there may be other cases. It will be added later when encountered, and that’s all for today.

Need source code of children’s shoes can be in [Dragon xuan] public number dialog box reply keyword: gray mode, you can obtain the source link.