思路:

1、我们View的层级是一层嵌套一层的,而绘制的画笔也是由顶层给到下层的。那我们拿到顶部的容器,改它的画笔颜色即可

如何拿到最顶部的容器呢?
View decorView = this.getWindow().getDecorView();
2、画笔要怎么设置?

我们可以通过矩阵,设置它的饱和度为0即可
ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); Paint paint = new
Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cm));
3、修改容器的画笔
decorView.setLayerType(View.LAYER_TYPE_SOFTWARE,paint);
* 如何作用于所有UI呢?
我们不是有BaseActivity吗?所以我们在onCreate的时候,设置了contentView,去修改即可。不行我们再要求一下重新布局/重新绘制。
代码:

 
@Override protected void onCreate(@Nullable
@org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(getLayoutResId()); //
1、拿到顶部容器 View decorView = getWindow().getDecorView(); // 2、修改矩阵的饱和度为0
ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); //
3、创建画笔 Paint paint = new Paint(); paint.setColorFilter(new
ColorMatrixColorFilter(colorMatrix)); // 4、修改容器的画笔
decorView.setLayerType(View.LAYER_TYPE_SOFTWARE, paint); mBind =
ButterKnife.bind(this); initView(); initEvent(); initPresenter(); }

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信