2012-09-22 Android的滚动条实现细节
在Android的UI系统中,每个View都有一个ScrollabilityCache的单例对象。该对象定义在View.java中,用于保存ScrollBar的相关实例和属性(ScrollBar实际上是一个ScrollBarDrawable对象),并且实现了淡入淡出动画效果的线程代码。这些会在该View的所有ScrollBar中共用。
以垂直滚动条为例,画出滚动条的过程大致是:
1
1. View::Draw
2
2. View::onDrawScrollBars
3
scrollBar.setParameters(computeVerticalScrollRange(),
4
computeVerticalScrollOffset(),
5
computeVerticalScrollExtent(), true);
6
7
3. View::onDrawVerticalScrollBar
8
scrollBar.draw
9
10
4. ScrollBarDrawable::draw
11
12
Rect r = getBounds();
13
14
if (drawTrack) {
15
drawTrack(canvas, r, vertical);
16
}
17
18
if (drawThumb) {
19
int size = vertical ? r.height() : r.width();
20
int thickness = vertical ? r.width() : r.height();
21
int length = Math.round((float) size * extent / range);
22
int offset = Math.round((float) (size - length) * mOffset / (range - extent));
23
24
// avoid the tiny thumb
25
int minLength = thickness * 2;
26
if (length < minLength) {
27
length = minLength;
28
}
29
// avoid the too-big thumb
30
if (offset + length > size) {
31
offset = size - length;
32
}
33
34
drawThumb(canvas, r, offset, length, vertical);
35
}
Copied!
最近更新 1yr ago
复制链接