具体怎样实现呢?
方法有几种:
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())
}
本文介绍了一种快速实现图片从彩色转换为黑白的方法。通过调整图片的饱和度来达到一键黑白的效果,提供了Java和Kotlin两种语言的具体实现示例。
2823

被折叠的 条评论
为什么被折叠?



