• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.gallery3d.ui;
18 
19 import android.content.Context;
20 import android.graphics.Rect;
21 import android.util.TypedValue;
22 
23 public class ScrollBarView extends GLView {
24     @SuppressWarnings("unused")
25     private static final String TAG = "ScrollBarView";
26 
27     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 
37     private NinePatchTexture mScrollBarTexture;
38 
ScrollBarView(Context context, int gripHeight, int gripWidth)39     public ScrollBarView(Context context, int gripHeight, int gripWidth) {
40         TypedValue outValue = new TypedValue();
41         context.getTheme().resolveAttribute(
42                 android.R.attr.scrollbarThumbHorizontal, outValue, true);
43         mScrollBarTexture = new NinePatchTexture(
44                 context, outValue.resourceId);
45         mGripPosition = 0;
46         mGripWidth = 0;
47         mGivenGripWidth = gripWidth;
48         mGripHeight = gripHeight;
49     }
50 
51     @Override
onLayout( boolean changed, int left, int top, int right, int bottom)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".
setContentPosition(int position, int total)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 
86     @Override
render(GLCanvas canvas)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     }
94 }
95