Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.graphics.Rect; |
Evan Millar | 1d9e17f | 2011-09-30 09:57:14 -0700 | [diff] [blame] | 21 | import android.util.TypedValue; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 22 | |
| 23 | public class ScrollBarView extends GLView { |
| 24 | @SuppressWarnings("unused") |
| 25 | private static final String TAG = "ScrollBarView"; |
| 26 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 37 | private NinePatchTexture mScrollBarTexture; |
| 38 | |
| 39 | public ScrollBarView(Context context, int gripHeight, int gripWidth) { |
Evan Millar | 1d9e17f | 2011-09-30 09:57:14 -0700 | [diff] [blame] | 40 | TypedValue outValue = new TypedValue(); |
| 41 | context.getTheme().resolveAttribute( |
| 42 | android.R.attr.scrollbarThumbHorizontal, outValue, true); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 43 | mScrollBarTexture = new NinePatchTexture( |
Evan Millar | 1d9e17f | 2011-09-30 09:57:14 -0700 | [diff] [blame] | 44 | context, outValue.resourceId); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 45 | mGripPosition = 0; |
| 46 | mGripWidth = 0; |
| 47 | mGivenGripWidth = gripWidth; |
| 48 | mGripHeight = gripHeight; |
| 49 | } |
| 50 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 51 | @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 Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 86 | @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 Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 94 | } |