美文网首页
Android全局黑白或调整颜色

Android全局黑白或调整颜色

作者: itfitness | 来源:发表于2021-08-25 14:17 被阅读0次

目录

效果展示

实现方法

实现的方法有两种

1.设置DecorView的Paint
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //主要是如下代码:
        Window window = getWindow();
        View decorView = window.getDecorView();
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        decorView.setLayerType(View.LAYER_TYPE_HARDWARE,paint);
    }
2.替换DecorView的直接子View

自定义的黑白布局如下

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

    public GrayFrameLayout(@NonNull Context context) {
        this(context,null);
    }

    public GrayFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

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


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

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int i = canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(i);
    }
}

Activity中的代码如下,由于这种方法不能替换状态栏颜色,因此需要手动调整状态栏颜色为黑白的

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //主要是如下代码:
        Window window = getWindow();
        View decorView = window.getDecorView();
        if(decorView instanceof ViewGroup){
            ViewGroup viewGroup = (ViewGroup) decorView;
            int childCount = viewGroup.getChildCount();
            if(childCount > 0){
                GrayFrameLayout grayFrameLayout = new GrayFrameLayout(this);
                grayFrameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                View childAt = viewGroup.getChildAt(0);
                viewGroup.removeViewAt(0);
                grayFrameLayout.addView(childAt);
                viewGroup.addView(grayFrameLayout);
            }
            //修改状态栏颜色
            BarUtils.setStatusBarColor(this, Color.GRAY);
            BarUtils.setStatusBarLightMode(this,false);
        }
    }

扩展

我们可以利用这个思路进行扩展,比如搞一个全局翻转的效果

public class FlipFrameLayout extends FrameLayout {
    private Camera mCamera;
    public FlipFrameLayout(@NonNull Context context) {
        this(context,null);
    }

    public FlipFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlipFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mCamera = new Camera();
    }
    @Override
    protected void dispatchDraw(Canvas canvas) {
        mCamera.save();
        canvas.save();
        //镜像反转
        mCamera.rotateY(180);
        Matrix matrix = new Matrix();
        mCamera.getMatrix(matrix);
        matrix.preTranslate(-getWidth()/2,-getHeight()/2);
        matrix.postTranslate(getWidth()/2,getHeight()/2);
        canvas.setMatrix(matrix);
        mCamera.restore();
        super.dispatchDraw(canvas);
        canvas.restore();
    }
}

案例源码

https://gitee.com/itfitness/black-and-white-demo

相关文章

网友评论

      本文标题:Android全局黑白或调整颜色

      本文链接:https://www.haomeiwen.com/subject/kxvsiltx.html