Home | History | Annotate | Download | only in ui
      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 com.android.gallery3d.R;
     20 
     21 import android.content.Context;
     22 import android.graphics.Rect;
     23 import android.util.TypedValue;
     24 
     25 public class ScrollBarView extends GLView {
     26     @SuppressWarnings("unused")
     27     private static final String TAG = "ScrollBarView";
     28 
     29     public interface Listener {
     30         void onScrollBarPositionChanged(int position);
     31     }
     32 
     33     private int mBarHeight;
     34 
     35     private int mGripHeight;
     36     private int mGripPosition;  // left side of the grip
     37     private int mGripWidth;     // zero if the grip is disabled
     38     private int mGivenGripWidth;
     39 
     40     private int mContentPosition;
     41     private int mContentTotal;
     42 
     43     private Listener mListener;
     44     private NinePatchTexture mScrollBarTexture;
     45 
     46     public ScrollBarView(Context context, int gripHeight, int gripWidth) {
     47         TypedValue outValue = new TypedValue();
     48         context.getTheme().resolveAttribute(
     49                 android.R.attr.scrollbarThumbHorizontal, outValue, true);
     50         mScrollBarTexture = new NinePatchTexture(
     51                 context, outValue.resourceId);
     52         mGripPosition = 0;
     53         mGripWidth = 0;
     54         mGivenGripWidth = gripWidth;
     55         mGripHeight = gripHeight;
     56     }
     57 
     58     public void setListener(Listener listener) {
     59         mListener = listener;
     60     }
     61 
     62     @Override
     63     protected void onLayout(
     64             boolean changed, int left, int top, int right, int bottom) {
     65         if (!changed) return;
     66         mBarHeight = bottom - top;
     67     }
     68 
     69     // The content position is between 0 to "total". The current position is
     70     // in "position".
     71     public void setContentPosition(int position, int total) {
     72         if (position == mContentPosition && total == mContentTotal) {
     73             return;
     74         }
     75 
     76         invalidate();
     77 
     78         mContentPosition = position;
     79         mContentTotal = total;
     80 
     81         // If the grip cannot move, don't draw it.
     82         if (mContentTotal <= 0) {
     83             mGripPosition = 0;
     84             mGripWidth = 0;
     85             return;
     86         }
     87 
     88         // Map from the content range to scroll bar range.
     89         //
     90         // mContentTotal --> getWidth() - mGripWidth
     91         // mContentPosition --> mGripPosition
     92         mGripWidth = mGivenGripWidth;
     93         float r = (getWidth() - mGripWidth) / (float) mContentTotal;
     94         mGripPosition = Math.round(r * mContentPosition);
     95     }
     96 
     97     private void notifyContentPositionFromGrip() {
     98         if (mContentTotal <= 0) return;
     99         float r = (getWidth() - mGripWidth) / (float) mContentTotal;
    100         int newContentPosition = Math.round(mGripPosition / r);
    101         mListener.onScrollBarPositionChanged(newContentPosition);
    102     }
    103 
    104     @Override
    105     protected void render(GLCanvas canvas) {
    106         super.render(canvas);
    107         if (mGripWidth == 0) return;
    108         Rect b = bounds();
    109         int y = (mBarHeight - mGripHeight) / 2;
    110         mScrollBarTexture.draw(canvas, mGripPosition, y, mGripWidth, mGripHeight);
    111     }
    112 
    113     // The onTouch() handler is disabled because now we don't want the user
    114     // to drag the bar (it's an indicator only).
    115     /*
    116     @Override
    117     protected boolean onTouch(MotionEvent event) {
    118         switch (event.getAction()) {
    119             case MotionEvent.ACTION_DOWN: {
    120                 int x = (int) event.getX();
    121                 return (x >= mGripPosition && x < mGripPosition + mGripWidth);
    122             }
    123             case MotionEvent.ACTION_MOVE: {
    124                 // Adjust x by mGripWidth / 2 so the center of the grip
    125                 // matches the touch position.
    126                 int x = (int) event.getX() - mGripWidth / 2;
    127                 x = Utils.clamp(x, 0, getWidth() - mGripWidth);
    128                 if (mGripPosition != x) {
    129                     mGripPosition = x;
    130                     notifyContentPositionFromGrip();
    131                     invalidate();
    132                 }
    133                 break;
    134             }
    135         }
    136         return true;
    137     }
    138     */
    139 }
    140