Android App 的“黑白化”,有一行代码实现的方案吗?,Android面试题集2024版

<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 方法即可:

public class TestActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test);

}

@Override

public View onCreateView(String name, Context context, AttributeSet attrs) {

return super.onCreateView(name, context, attrs);

}

}

我们在这个方法中把content view 对应的 FrameLayout 换成 GrayFrameLayout.

@Override

public View onCreateView(String name, Context context, AttributeSet attrs) {

if(“FrameLayout”.equals(name)){

int count = attrs.getAttributeCount();

for (int i = 0; i < count; i++) {

String attributeName = attrs.getAttributeName(i);

String attributeValue = attrs.getAttributeValue(i);

if (attributeName.equals(“id”)) {

int id = Integer.parseInt(attributeValue.substring(1));

String idVal = getResources().getResourceName(id);

if (“android:id/content”.equals(idVal)) {

GrayFrameLayout grayFrameLayout = new GrayFrameLayout(context, attrs);

return grayFrameLayout;

}

}

}

}

return super.onCreateView(name, context, attrs);

}

代码应该都能看明白吧,我们找到 id 是 android:id/content 的,换成了我们的 GrayFrameLayout。

最后看一眼GrayFrameLayout:

public class GrayFrameLayout extends FrameLayout {

private Paint mPaint = new Paint();

public GrayFrameLayout(Context context, AttributeSet attrs) {

super(context, attrs);

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

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

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

img

img

img

img

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

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

作为一名从事Android的开发者,很多人最近都在和我吐槽Android是不是快要凉了?而在我看来这正是市场成熟的表现,所有的市场都是温水煮青蛙,永远会淘汰掉不愿意学习改变,安于现状的那批人,希望所有的人能在大浪淘沙中留下来,因为对于市场的逐渐成熟,平凡并不是我们唯一的答案!

资料.png
资料图.jpg

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

担。**

[外链图片转存中…(img-Pxw6wJgU-1712334837199)]

[外链图片转存中…(img-kbw6NGol-1712334837199)]

[外链图片转存中…(img-2kwR455U-1712334837200)]

[外链图片转存中…(img-bOJRgYbk-1712334837200)]

[外链图片转存中…(img-ghGQbUzQ-1712334837200)]

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

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

作为一名从事Android的开发者,很多人最近都在和我吐槽Android是不是快要凉了?而在我看来这正是市场成熟的表现,所有的市场都是温水煮青蛙,永远会淘汰掉不愿意学习改变,安于现状的那批人,希望所有的人能在大浪淘沙中留下来,因为对于市场的逐渐成熟,平凡并不是我们唯一的答案!

[外链图片转存中…(img-CoPBZ2y8-1712334837201)]
[外链图片转存中…(img-neeYu2cU-1712334837201)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值