具体怎样实现呢?
方法有几种:
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())
}