blob: 82d4800ccd1b1d02715da8b1a903cd202dff9ea1 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
2 * Copyright (C) 2010 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 com.android.gallery3d.ui;
18
Owen Lina2fba682011-08-17 22:07:43 +080019import android.content.Context;
20import android.graphics.Rect;
Evan Millar1d9e17f2011-09-30 09:57:14 -070021import android.util.TypedValue;
Owen Lina2fba682011-08-17 22:07:43 +080022
23public class ScrollBarView extends GLView {
24 @SuppressWarnings("unused")
25 private static final String TAG = "ScrollBarView";
26
Owen Lina2fba682011-08-17 22:07:43 +080027 private int mBarHeight;
28
29 private int mGripHeight;
30 private int mGripPosition; // left side of the grip
31 private int mGripWidth; // zero if the grip is disabled
32 private int mGivenGripWidth;
33
34 private int mContentPosition;
35 private int mContentTotal;
36
Owen Lina2fba682011-08-17 22:07:43 +080037 private NinePatchTexture mScrollBarTexture;
38
39 public ScrollBarView(Context context, int gripHeight, int gripWidth) {
Evan Millar1d9e17f2011-09-30 09:57:14 -070040 TypedValue outValue = new TypedValue();
41 context.getTheme().resolveAttribute(
42 android.R.attr.scrollbarThumbHorizontal, outValue, true);
Owen Lina2fba682011-08-17 22:07:43 +080043 mScrollBarTexture = new NinePatchTexture(
Evan Millar1d9e17f2011-09-30 09:57:14 -070044 context, outValue.resourceId);
Owen Lina2fba682011-08-17 22:07:43 +080045 mGripPosition = 0;
46 mGripWidth = 0;
47 mGivenGripWidth = gripWidth;
48 mGripHeight = gripHeight;
49 }
50
Owen Lina2fba682011-08-17 22:07:43 +080051 @Override
52 protected void onLayout(
53 boolean changed, int left, int top, int right, int bottom) {
54 if (!changed) return;
55 mBarHeight = bottom - top;
56 }
57
58 // The content position is between 0 to "total". The current position is
59 // in "position".
60 public void setContentPosition(int position, int total) {
61 if (position == mContentPosition && total == mContentTotal) {
62 return;
63 }
64
65 invalidate();
66
67 mContentPosition = position;
68 mContentTotal = total;
69
70 // If the grip cannot move, don't draw it.
71 if (mContentTotal <= 0) {
72 mGripPosition = 0;
73 mGripWidth = 0;
74 return;
75 }
76
77 // Map from the content range to scroll bar range.
78 //
79 // mContentTotal --> getWidth() - mGripWidth
80 // mContentPosition --> mGripPosition
81 mGripWidth = mGivenGripWidth;
82 float r = (getWidth() - mGripWidth) / (float) mContentTotal;
83 mGripPosition = Math.round(r * mContentPosition);
84 }
85
Owen Lina2fba682011-08-17 22:07:43 +080086 @Override
87 protected void render(GLCanvas canvas) {
88 super.render(canvas);
89 if (mGripWidth == 0) return;
90 Rect b = bounds();
91 int y = (mBarHeight - mGripHeight) / 2;
92 mScrollBarTexture.draw(canvas, mGripPosition, y, mGripWidth, mGripHeight);
93 }
Owen Lina2fba682011-08-17 22:07:43 +080094}