Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation( 0);//0:表示灰度显示,1:表示彩色显示
paint.setColorFilter(new ColorMatrixColorFilter(cm));
view.setLayerType(View.LAYER_TYPE_HARDWARE, paint);
特点:
【1】会对 子view 也灰度显示
【2】支持webview中的网页灰度显示
【3】Activity中拿到全局view的方法是getWindow().getDecorView()
@Override
protected void onCreate(Bundle savedInstanceState, int layoutResID) {
super.onCreate(savedInstanceState, R.layout.layout_main);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);//0:表示灰度显示,1:表示彩色显示
paint.setColorFilter(new ColorMatrixColorFilter(cm));
getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, paint);
}
【4】Fragment中拿到全局view的方法是onCreateView中的return view;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.fragment_main_my, container, false);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);//0:表示灰度显示,1:表示彩色显示
paint.setColorFilter(new ColorMatrixColorFilter(cm));
contentView.setLayerType(View.LAYER_TYPE_HARDWARE, paint);
return contentView;
}
网友评论