Android App 的“黑白化”,有一行代码实现的方案吗

那么是不是我们在控件绘制的时候,比如 draw 之前设置个灰度效果就可以呢?

好像发现了什么玄机。

_1_尝试给 ImageView 上个灰度效果

那么我们首先通过 ImageView 来验证一下灰度效果的可行性。

我们编写个自定义的 ImageView,叫做:GrayImageView

布局文件是这样的:

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:context=“.TestActivity”>

<ImageView

android:layout_width=“100dp”

android:layout_height=“wrap_content”

android:src=“@mipmap/logo”>

<com.imooc.imooc_wechat_app.view.GrayImageView

android:layout_width=“100dp”

android:layout_height=“wrap_content”

android:src=“@mipmap/logo” />

很简单,我们放了一个 ImageView 用来做对比。

看下 GrayImageView 的代码:

public class GrayImageView extends AppCompatImageView {

private Paint mPaint = new Paint();

public GrayImageView(Context context, AttributeSet attrs) {

super(context, attrs);

ColorMatrix cm = new ColorMatrix();

cm.setSaturation(0);

mPaint.setColorFilter(new ColorMatrixColorFilter(cm));

}

@Override

public void draw(Canvas canvas) {

canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);

super.draw(canvas);

canvas.restore();

}

}

在分析代码之前,我们看下效果图:

很完美,我们成功把 wanandroid图标搞成了灰色。

看一眼代码,代码非常简单,我们复写了draw 方法,在该方法中给canvas 做了一下特殊处理。

什么特殊处理呢?其实就是设置了一个灰度效果。

在 App中,我们对于颜色的处理很多时候会采用颜色矩阵,是一个4*5的矩阵,原理是这样的:

[ a, b, c, d, e,

f, g, h, i, j,

k, l, m, n, o,

p, q, r, s, t ]

应用到一个具体的颜色[R, G, B, A]上,最终颜色的计算是这样的:

R’ = aR + bG + cB + dA + e;

G’ = fR + gG + hB + iA + j;

B’ = kR + lG + mB + nA + o;

A’ = pR + qG + rB + sA + t;

是不是看起来很难受,没错我也很难受,看到代数就烦。

既然大家都难受,那么Android 就比较贴心了,给我们搞了个ColorMartrix类,这个类对外提供了很多 API,大家直接调用 API 就能得到大部分想要的效果了,除非你有特别特殊的操作,那么可以自己通过矩阵去运算。

像灰度这样的效果,我们可以通过饱和度 API来操作:

setSaturation(float sat)

传入 0 就可以了,你去看源码,底层传入了一个特定的矩阵去做的运算。

ok,好了,忘掉上面说的,就记得你有个 API 能把 canvas 绘制出来的东西搞成灰的就行了。

那么我们已经实现了把 ImageView 弄成了灰度,TextView 可以吗?Button可以吗?

尝试举一反三


我们来试试TextView、Button。

代码完全一样哈,其实就是换了个实现类,例如 GrayTextView:

public class GrayTextView extends AppCompatTextView {

private Paint mPaint = new Paint();

public GrayTextView(Context context, AttributeSet attrs) {

super(context, attrs);

ColorMatrix cm = new ColorMatrix();

cm.setSaturation(0);

mPaint.setColorFilter(new ColorMatrixColorFilter(cm));

}

@Override

public void draw(Canvas canvas) {

canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);

super.draw(canvas);

canvas.restore();

}

}

没任何区别,GrayButton 就不贴了,我们看布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:context=“.TestActivity”>

<ImageView

android:layout_width=“100dp”

android:layout_height=“wrap_content”

android:src=“@mipmap/logo”>

<com.imooc.imooc_wechat_app.view.GrayImageView

android:layout_width=“100dp”

android:layout_height=“wrap_content”

android:src=“@mipmap/logo” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“鸿洋真帅”

android:textColor=“@android:color/holo_red_light”

android:textSize=“30dp” />

<com.imooc.imooc_wechat_app.view.GrayTextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“鸿洋真帅”

android:textColor=“@android:color/holo_red_light”

android:textSize=“30dp” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“鸿洋真帅”

android:textColor=“@android:color/holo_red_light”

android:textSize=“30dp” />

<com.imooc.imooc_wechat_app.view.GrayButton

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“鸿洋真帅”

android:textColor=“@android:color/holo_red_light”

android:textSize=“30dp” />

对应的效果图:

可以看到 TextView,Button 也成功的把红色的字体换成了灰色。

这个时候你是不是忽然感觉自己会了?

其实我们只要把各种相关的 View 换成这种自定义 View,利用 appcompat换肤那一套,不需要 Server 参与了,客户端搞搞就行了。

是吗?我们需要把所有的 View 都换成自定义的 View吗?

这听起来成本也挺高呀。

再想想还有更简单的吗?

往上看一眼


虽然刚才的布局文件很简单,但是邀请你再去看一眼刚才的布局文件,我要问你问题了:

看好了吧。

  1. 请问上面的 xml 中,ImageView的父 View 是谁?

  2. TextView 的父 View 是谁?

  3. Button 的父 View 是谁?

有没有一点茅塞顿开!

我们需要一个个自定义吗?

父 View 都是 LinearLayout,我们搞个 GrayLinearLayout 不就行了,其内部的 View 都会变成灰色,毕竟 Canvas 对象是往下传递的

我们来试试:

GrayLinearLayout:

public class GrayLinearLayout extends LinearLayout {

private Paint mPaint = new Paint();

public GrayLinearLayout(Context context, AttributeSet attrs) {

super(context, attrs);

ColorMatrix cm = new ColorMatrix();

cm.setSaturation(0);

mPaint.setColorFilter(new ColorMatrixColorFilter(cm));

}

@Override

public void draw(Canvas canvas) {

canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);

super.draw(canvas);

canvas.restore();

}

@Override

protected void dispatchDraw(Canvas canvas) {

canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);

super.dispatchDraw(canvas);

canvas.restore();

}

}

代码很简单,但是注意有个细节,注意我们也复写了 dispatchDraw,为什么呢?自己思考。

我们更换下 xml:

<?xml version="1.0" encoding="utf-8"?>

<com.imooc.imooc_wechat_app.view.GrayLinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:context=“.TestActivity”>

<ImageView

android:layout_width=“100dp”

android:layout_height=“wrap_content”

android:src=“@mipmap/logo” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“鸿洋真帅”

android:textColor=“@android:color/holo_red_light”

android:textSize=“30dp” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“鸿洋真帅”

android:textColor=“@android:color/holo_red_light”

android:textSize=“30dp” />

</com.imooc.imooc_wechat_app.view.GrayLinearLayout>

我们放了蓝色 Logo 的 ImageView,红色字体的 TextView 和 Button,看一眼效果:

完美!

是不是又有点茅塞顿开!

只要我们换了 我们设置的Activity 的根布局就可以了!

Activity 的根布局可能是 LinearLayout,FrameLayout,RelativeLayout,ConstraintLayout…

换个鸡儿…这得换到啥时候,跟刚才有啥区别。

还有思路吗,没什么确定的 View 吗?

再想想。

我们的设置的 Activity 的根布局会放在哪?

android.id.content

是不是这个 Content View 上?

这个 content view 目前一直是 FrameLayout !

那么我们只要在生成这个android.id.content 对应的 FrameLayout,换成 GrayFrameLayout 就可以了。

怎么换呢?

appcompat 那一套?去搞 LayoutFactory?

确实可以哈,但是那样要设置 LayoutFactory,还需要考虑 appcompat 相关逻辑。

有没有那种不需要去修改什么流程的方案?

LayoutInflater 中的细节


还真是有的。

我们的 AppCompatActivity,可以复写 onCreateView 的方法,这个方法其实也是LayoutFactory在构建 View 的时候回调出来的,一般对应其内部的mPrivateFactory。

他的优先级低于 Factory、Factory2,相关代码:

if (mFactory2 != null) {

view = mFactory2.onCreateView(parent, name, context, attrs);

} else if (mFactory != null) {

view = mFactory.onCreateView(name, context, attrs);

} else {

view = null;

}

if (view == null && mPrivateFactory != null) {

view = mPrivateFactory.onCreateView(parent, name, context, attrs);

}

if (view == null) {

final Object lastContext = mConstructorArgs[0];

mConstructorArgs[0] = context;

try {

if (-1 == name.indexOf(‘.’)) {

view = onCreateView(parent, name, attrs);

} else {

view = createView(name, null, attrs);

}

} finally {

mConstructorArgs[0] = lastContext;

}

}

但是目前对于 FrameLayout,appcompat 并没有特殊处理,也就是说你可以在 onCreateView 回调中去构造 FrameLayout 对象。

很简单,就复写 Activity 的 onCreateView 方法即可:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

下图是我进阶学习所积累的历年腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节

整理不易,望各位看官老爷点个关注转发,谢谢!祝大家都能得到自己心仪工作。

[外链图片转存中…(img-tptrGvGa-1711907649440)]
[外链图片转存中…(img-QCTdluNc-1711907649440)]
[外链图片转存中…(img-ip30ZTVA-1711907649440)]
[外链图片转存中…(img-TDNglIyN-1711907649441)]
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-8VYfYwPn-1711907649441)]

最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

下图是我进阶学习所积累的历年腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节

[外链图片转存中…(img-4f5SZzNX-1711907649442)]

整理不易,望各位看官老爷点个关注转发,谢谢!祝大家都能得到自己心仪工作。

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值