# 2012-09-22  Android的滚动条实现细节

在Android的UI系统中，每个View都有一个ScrollabilityCache的单例对象。该对象定义在View\.java中，用于保存ScrollBar的相关实例和属性(ScrollBar实际上是一个ScrollBarDrawable对象)，并且实现了淡入淡出动画效果的线程代码。这些会在该View的所有ScrollBar中共用。

以垂直滚动条为例，画出滚动条的过程大致是:

```
1. View::Draw
2. View::onDrawScrollBars
scrollBar.setParameters(computeVerticalScrollRange(),
            computeVerticalScrollOffset(),
            computeVerticalScrollExtent(), true);

3. View::onDrawVerticalScrollBar
    scrollBar.draw

4. ScrollBarDrawable::draw

Rect r = getBounds();

if (drawTrack) {
    drawTrack(canvas, r, vertical);
}

if (drawThumb) {
    int size = vertical ? r.height() : r.width();
    int thickness = vertical ? r.width() : r.height();
    int length = Math.round((float) size * extent / range);
    int offset = Math.round((float) (size - length) * mOffset / (range - extent));

    // avoid the tiny thumb
    int minLength = thickness * 2;
    if (length < minLength) {
        length = minLength;
    }
    // avoid the too-big thumb
    if (offset + length > size) {
        offset = size - length;
    }

    drawThumb(canvas, r, offset, length, vertical);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.log4think.com/android-scrollbar-thumb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
