zhangsr / blog Goto Github PK
View Code? Open in Web Editor NEWBlog by issue
Blog by issue
各种 IPC 方式的优缺点和适用场景(参考《Andoid开发艺术探索》,并按自己的理解做了改动)
名称 | 优点 | 缺点 | 适用场景 | 实例 |
---|---|---|---|---|
Bundle | 简单易用 | 只能传输 Bundle 支持的数据类型 | Activity、Service、Broadcast 之间单向单次的进程间通信 | 传参启动 Activity |
文件共享 | 简单易用 | (1)不适合高并发场景(2)不适合即时通信 | (1)无并发访问(2)不要求实时 | SharedPreference |
AIDL | (1)支持一对多并发通信(2)实时通信 | (1)使用稍复杂(2)需要处理好线程同步 | (1)一对多双向并发通信(2)有RPC需求 | 音乐播放器 |
Messenger | (1)支持一对多串行通信(2)实时通信 | (1)低并发(2)不支持RPC(3)只能传输 Bundle 支持的数据类型 | (1)一对多双向低并发通信(2)无RPC或无需返回的RPC | |
ContentProvider | 标准数据共享接口 | 提供数据共享,包括CRUD操作 | 通讯录信息 | |
Socket | 网络传输 | 实现繁琐 | 网络传输 | 聊天室 |
其中 AIDL、Messenger、ContentProvider 都是基于 Binder
(1)Client 调用 Stub 接口的方法 A
(2)直接由
(3)Service 中该接口的实现响应。
(4)直接由
(5)Client 方法 A 返回
(1)Client 调用 Stub 接口的方法 A
(2)在 Client 端,由 Stub 的内部类 Proxy 响应,并将方法id 和参数序列化,通过 transact 方法传到底层,Client 端线程阻塞。在 Service 端,系统(线程池)回调onTransact 方法,反序列化,并调用不同的 Stub 接口方法。
(3)Service 中该接口的实现响应。
(4)onTransact 方法得到结果并序列化,返回 true 标记成功,transact 方法线程阻塞解除,得到结果并反序列化,返回结果。
(5)Client 方法 A 返回
可以看出来非IPC 与 IPC 的区别只有第(2)(4)步,而这一步的代码是通过 .aidl 文件生成的,也就是系统屏蔽了进程间通信的具体实现,只暴露给开发者(1)(3)(5)三步,使得在开发者的眼里,IPC 就好像在同一个进程中一样,实现起来非常简单。
主要绘制流程如下:
measure、layout、draw的遍历如下:
measure() | onMeasure() | |
---|---|---|
SomeView | 不重写,用View的默认实现 | 重写,用自己特定的计算方式 |
SomeViewGroup | 不重写,用View的默认实现 | 重写,用自己特定的计算方式 |
View的measure()默认实现做了一些缓存检查之类的工作,最终还是调用了onMeasure(),下面展示onMeasure()做了什么:
onMeasure() | |
---|---|
SomeView | (1)测量实际内容(2)setMeasured()设置自己的值 |
SomeViewGroup | (1)读取子View的layout_*,结合自己的measuredSpec生成子View的measuredSpec(2)调用子View onMeasure()(3)获取子View测量值(4)setMeasured()设置自己的值 |
View大小的控制是由父View、layout.xml文件、以及View本身共同完成的,父View会提供给子View参考的大小,而开发人员可以在layout.xml文件中指定View的大小,然后View本身会对最终的大小进行拍板
measure 过程的结果:所有的View、ViewGroup都调用setMeasured()设置了自己的宽高,以备 layout 过程使用。
layout() | onLayout() | |
---|---|---|
SomeView | 可重写,也可用View的默认实现 | 不重写,View的为空实现 |
SomeViewGroup | 不重写,用View的默认实现 | 重写,用自己特定的布局方式布局子View |
无论是View还是ViewGroup,layout()都可以使用View的默认方式:
layout 过程的结果:设置好View、ViewGroup的左上右下坐标
draw() | onDraw() | |
---|---|---|
SomeView | 不重写,用View的默认实现 | 重写,View的为空实现 |
SomeViewGroup | 不重写,用View的默认实现 | �不重写,�无内容绘制 |
onDraw()原理简单,自定义View需要重点实现,以绘制出想要的内容。
draw()虽都不重写,但很重要,View的默认实现已经把所有步骤都做好了:
ViewGroup已经重写了dispatchDraw方法,实现了遍历子View绘制,所以一般自定义ViewGroup时不需要重写
以上四个步骤按序调用,完成了所有绘制。
draw 过程的结果:将图像绘制到 Canvas 上。
http://blog.csdn.net/yanbober/article/details/46128379
http://blog.csdn.net/guolin_blog/article/details/16330267
http://developer.android.com/intl/zh-cn/guide/topics/ui/how-android-draws.html
http://blog.csdn.net/luoshengyang/article/details/8372924
http://www.codekk.com/blogs/detail/54cfab086c4761e5001b253f
主要:重写 onDraw() 控制绘制的内容
其它:
主要:实现 measure() layout() 过程
其它:
dispatchTouchEvent() | onInterceptTouchEvent() | onTouchEvent() | |
---|---|---|---|
SomeView | 不重写,用View的默认实现 | 无 | 重写 |
SomeViewGroup | 一般不重写,用ViewGroup的默认实现 | 重写,默认返回false | (1)不重写,用View的默认实现如LinearLayout;(2)重写,如ListView |
http://wangkuiwu.github.io/2015/01/01/TouchEvent-Introduce/ 系列
http://blog.csdn.net/guolin_blog/article/details/9097463 系列
常用分类:
特点:
List:
Set:
Map
遍历:
为了清晰,下面用伪代码简化
调用textView.setText(),首先会经过几层简单的封装,方法名都是setText()
// TextView.java
setText() {
notifyListeners();
checkForRelayout();
}
首先通知到文本改变的监听器,如TextChangedListener()
// TextView.java
checkForRelayout() {
......
if (needReLayout) {
requestLayout(); //重新布局
}
invalidate(); //即调用onDraw()重新绘制
}
检查是否需要改变布局,如果需要,则在重绘之前会调用requestLayout()
// TextView.java
onDraw() {
drawBackground();
drawIcon();
drawShadow();
......
layout.draw();
}
针对整个TextView的范围,绘制好文字以外或者底部的各种部件
// Layout.java
draw() {
drawBackground(); //绘制段落的背景
drawText();
}
针对文字段落,绘制背景,然后终于轮到了主角
// Layout.java
drawText() {
...... // 计算渲染的位置坐标
for (每一行) {
if (!mSpannedText) {
// 普通无样式文本
canvas.drawText()
} else {
// TODO
}
}
}
针对每一行文字,逐行进行绘制
// Canvas.java
drawText() {
native_drawText(canvas, paint, x, y, ...);
}
调用native渲染方法,并提供必须的参数:画布、画笔(包括各个属性,大小、粗细、颜色等)、坐标。
这就是TextView setTextView渲染的主要流程,给出个大概框架之后,可以供后面深入优化参考。
发送方式 | 特点 | 应用场景 | |
---|---|---|---|
普通广播 | sendBroadcast() | 异步、无序 | 普通场景 |
有序广播 | sendOrderedBroadcast() | Receivers 轮流,可附加信息 | 拨号广播的拦截、格式化 |
黏性广播 | sendSticktBroadcast() | 接收者主动 | 监听电池状态信息的广播 |
使用LocalBroadcastManager,避免其他应用发来的恶意广播,避免内部广播被其他应用接收
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.