ScrollBarDrawable.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.graphics.Canvas;
20import android.graphics.ColorFilter;
21import android.graphics.PixelFormat;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24
25/**
26 * This is only used by View for displaying its scroll bars.  It should probably
27 * be moved in to the view package since it is used in that lower-level layer.
28 * For now, we'll hide it so it can be cleaned up later.
29 * {@hide}
30 */
31public class ScrollBarDrawable extends Drawable {
32    private Drawable mVerticalTrack;
33    private Drawable mHorizontalTrack;
34    private Drawable mVerticalThumb;
35    private Drawable mHorizontalThumb;
36    private int mRange;
37    private int mOffset;
38    private int mExtent;
39    private boolean mVertical;
40    private boolean mChanged;
41    private boolean mRangeChanged;
42    private final Rect mTempBounds = new Rect();
43    private boolean mAlwaysDrawHorizontalTrack;
44    private boolean mAlwaysDrawVerticalTrack;
45
46    public ScrollBarDrawable() {
47    }
48
49    /**
50     * Indicate whether the horizontal scrollbar track should always be drawn regardless of the
51     * extent. Defaults to false.
52     *
53     * @param alwaysDrawTrack Set to true if the track should always be drawn
54     */
55    public void setAlwaysDrawHorizontalTrack(boolean alwaysDrawTrack) {
56        mAlwaysDrawHorizontalTrack = alwaysDrawTrack;
57    }
58
59    /**
60     * Indicate whether the vertical scrollbar track should always be drawn regardless of the
61     * extent. Defaults to false.
62     *
63     * @param alwaysDrawTrack Set to true if the track should always be drawn
64     */
65    public void setAlwaysDrawVerticalTrack(boolean alwaysDrawTrack) {
66        mAlwaysDrawVerticalTrack = alwaysDrawTrack;
67    }
68
69    /**
70     * Indicates whether the vertical scrollbar track should always be drawn regardless of the
71     * extent.
72     */
73    public boolean getAlwaysDrawVerticalTrack() {
74        return mAlwaysDrawVerticalTrack;
75    }
76
77    /**
78     * Indicates whether the horizontal scrollbar track should always be drawn regardless of the
79     * extent.
80     */
81    public boolean getAlwaysDrawHorizontalTrack() {
82        return mAlwaysDrawHorizontalTrack;
83    }
84
85    public void setParameters(int range, int offset, int extent, boolean vertical) {
86        if (mVertical != vertical) {
87            mChanged = true;
88        }
89
90        if (mRange != range || mOffset != offset || mExtent != extent) {
91            mRangeChanged = true;
92        }
93
94        mRange = range;
95        mOffset = offset;
96        mExtent = extent;
97        mVertical = vertical;
98    }
99
100    @Override
101    public void draw(Canvas canvas) {
102        final boolean vertical = mVertical;
103        final int extent = mExtent;
104        final int range = mRange;
105
106        boolean drawTrack = true;
107        boolean drawThumb = true;
108        if (extent <= 0 || range <= extent) {
109            drawTrack = vertical ? mAlwaysDrawVerticalTrack : mAlwaysDrawHorizontalTrack;
110            drawThumb = false;
111        }
112
113        Rect r = getBounds();
114        if (canvas.quickReject(r.left, r.top, r.right, r.bottom,
115                Canvas.EdgeType.AA)) {
116            return;
117        }
118        if (drawTrack) {
119            drawTrack(canvas, r, vertical);
120        }
121
122        if (drawThumb) {
123            int size = vertical ? r.height() : r.width();
124            int thickness = vertical ? r.width() : r.height();
125            int length = Math.round((float) size * extent / range);
126            int offset = Math.round((float) (size - length) * mOffset / (range - extent));
127
128            // avoid the tiny thumb
129            int minLength = thickness * 2;
130            if (length < minLength) {
131                length = minLength;
132            }
133            // avoid the too-big thumb
134            if (offset + length > size) {
135                offset = size - length;
136            }
137
138            drawThumb(canvas, r, offset, length, vertical);
139        }
140    }
141
142    @Override
143    protected void onBoundsChange(Rect bounds) {
144        super.onBoundsChange(bounds);
145        mChanged = true;
146    }
147
148    protected void drawTrack(Canvas canvas, Rect bounds, boolean vertical) {
149        Drawable track;
150        if (vertical) {
151            track = mVerticalTrack;
152        } else {
153            track = mHorizontalTrack;
154        }
155        if (mChanged) {
156            track.setBounds(bounds);
157        }
158        track.draw(canvas);
159    }
160
161    protected void drawThumb(Canvas canvas, Rect bounds, int offset, int length, boolean vertical) {
162        final Rect thumbRect = mTempBounds;
163        final boolean changed = mRangeChanged || mChanged;
164        if (changed) {
165            if (vertical) {
166                thumbRect.set(bounds.left,  bounds.top + offset,
167                        bounds.right, bounds.top + offset + length);
168            } else {
169                thumbRect.set(bounds.left + offset, bounds.top,
170                        bounds.left + offset + length, bounds.bottom);
171            }
172        }
173
174        if (vertical) {
175            final Drawable thumb = mVerticalThumb;
176            if (changed) thumb.setBounds(thumbRect);
177            thumb.draw(canvas);
178        } else {
179            final Drawable thumb = mHorizontalThumb;
180            if (changed) thumb.setBounds(thumbRect);
181            thumb.draw(canvas);
182        }
183    }
184
185    public void setVerticalThumbDrawable(Drawable thumb) {
186        if (thumb != null) {
187            mVerticalThumb = thumb;
188        }
189    }
190
191    public void setVerticalTrackDrawable(Drawable track) {
192        mVerticalTrack = track;
193    }
194
195    public void setHorizontalThumbDrawable(Drawable thumb) {
196        if (thumb != null) {
197            mHorizontalThumb = thumb;
198        }
199    }
200
201    public void setHorizontalTrackDrawable(Drawable track) {
202        mHorizontalTrack = track;
203    }
204
205    public int getSize(boolean vertical) {
206        if (vertical) {
207            return (mVerticalTrack != null ?
208                    mVerticalTrack : mVerticalThumb).getIntrinsicWidth();
209        } else {
210            return (mHorizontalTrack != null ?
211                    mHorizontalTrack : mHorizontalThumb).getIntrinsicHeight();
212        }
213    }
214
215    @Override
216    public void setAlpha(int alpha) {
217        if (mVerticalTrack != null) {
218            mVerticalTrack.setAlpha(alpha);
219        }
220        mVerticalThumb.setAlpha(alpha);
221        if (mHorizontalTrack != null) {
222            mHorizontalTrack.setAlpha(alpha);
223        }
224        mHorizontalThumb.setAlpha(alpha);
225    }
226
227    @Override
228    public void setColorFilter(ColorFilter cf) {
229        if (mVerticalTrack != null) {
230            mVerticalTrack.setColorFilter(cf);
231        }
232        mVerticalThumb.setColorFilter(cf);
233        if (mHorizontalTrack != null) {
234            mHorizontalTrack.setColorFilter(cf);
235        }
236        mHorizontalThumb.setColorFilter(cf);
237    }
238
239    @Override
240    public int getOpacity() {
241        return PixelFormat.TRANSLUCENT;
242    }
243
244    @Override
245    public String toString() {
246        return "ScrollBarDrawable: range=" + mRange + " offset=" + mOffset +
247               " extent=" + mExtent + (mVertical ? " V" : " H");
248    }
249}
250
251
252