具体怎样实现呢?

方法有几种:

1,运营 配置更换黑白图片

2,技术强行更换渲染方式,达到“一键黑白”的目的

那么 怎样才是最快捷的方式呢?上代码

/**
 * 
 * @param type 0:正常 1黑白
 * @return
 */
public static Paint getMatrix(int type){
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    switch (type){
        case 0:
            cm.setSaturation(1);
            break;
        case 1:
            cm.setSaturation(0);
            break;
        default:
    }

    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    return paint;
}

使用方法:

JAVA:

针对 activity:
在onCreate方法中:
getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, paint);

会对view:
setLayerType(View.LAYER_TYPE_HARDWARE, paint);

KOTLIN:


针对activity:
window.decorView.setLayerType(View.LAYER_TYPE_HARDWARE, getMatrix())
针对fragment:
override fun initView() {
    getView()?.setLayerType(View.LAYER_TYPE_HARDWARE, getMatrix())
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐