Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2012 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.filtershow.ui;
     18 
     19 import android.graphics.Canvas;
     20 import android.graphics.Paint;
     21 import android.view.MotionEvent;
     22 
     23 public class SliderController {
     24     private static final String LOGTAG = "SliderController";
     25 
     26     private float mCenterX;
     27     private float mCenterY;
     28     private float mCurrentX;
     29     private float mCurrentY;
     30     private int mValue = 100;
     31     int mOriginalValue = 0;
     32 
     33     private int mWidth = 0;
     34     private int mHeight = 0;
     35 
     36     private String mToast = null;
     37 
     38     private final Paint mPaint = new Paint();
     39 
     40     private SliderListener mListener = null;
     41 
     42     private MODES mMode = MODES.NONE;
     43     private static int mTextSize = 128;
     44 
     45     private enum MODES {
     46         NONE, DOWN, UP, MOVE
     47     }
     48 
     49     public void onDraw(Canvas canvas) {
     50         if (mMode == MODES.NONE || mMode == MODES.UP) {
     51             return;
     52         }
     53     }
     54 
     55     public void drawToast(Canvas canvas) {
     56         if (mToast != null) {
     57             canvas.save();
     58             mPaint.setTextSize(mTextSize);
     59             float textWidth = mPaint.measureText(mToast);
     60             int toastX = (int) ((getWidth() - textWidth) / 2.0f);
     61             int toastY = (int) (getHeight() / 3.0f);
     62 
     63             mPaint.setARGB(255, 0, 0, 0);
     64             canvas.drawText(mToast, toastX - 2, toastY - 2, mPaint);
     65             canvas.drawText(mToast, toastX - 2, toastY, mPaint);
     66             canvas.drawText(mToast, toastX, toastY - 2, mPaint);
     67             canvas.drawText(mToast, toastX + 2, toastY + 2, mPaint);
     68             canvas.drawText(mToast, toastX + 2, toastY, mPaint);
     69             canvas.drawText(mToast, toastX, toastY + 2, mPaint);
     70             mPaint.setARGB(255, 255, 255, 255);
     71             canvas.drawText(mToast, toastX, toastY, mPaint);
     72             canvas.restore();
     73         }
     74     }
     75 
     76     protected int computeValue() {
     77         int delta = (int) (100 * (getCurrentX() - getCenterX()) / getWidth());
     78         int value = mOriginalValue + delta;
     79         if (value < -100) {
     80             value = -100;
     81         } else if (value > 100) {
     82             value = 100;
     83         }
     84         setValue(value);
     85         mToast = "" + value;
     86         return value;
     87     }
     88 
     89     public void setValue(int value) {
     90         mValue = value;
     91     }
     92 
     93     public int getWidth() {
     94         return mWidth;
     95     }
     96 
     97     public int getHeight() {
     98         return mHeight;
     99     }
    100 
    101     public void setWidth(int value) {
    102         mWidth = value;
    103     }
    104 
    105     public void setHeight(int value) {
    106         mHeight = value;
    107     }
    108 
    109     public float getCurrentX() {
    110         return mCurrentX;
    111     }
    112 
    113     public float getCurrentY() {
    114         return mCurrentY;
    115     }
    116 
    117     public float getCenterX() {
    118         return mCenterX;
    119     }
    120 
    121     public float getCenterY() {
    122         return mCenterY;
    123     }
    124 
    125     public void setActionDown(float x, float y) {
    126         mCenterX = x;
    127         mCenterY = y;
    128         mCurrentX = x;
    129         mCurrentY = y;
    130         mMode = MODES.DOWN;
    131         if (mListener != null) {
    132             mListener.onTouchDown(x, y);
    133         }
    134     }
    135 
    136     public void setActionMove(float x, float y) {
    137         mCurrentX = x;
    138         mCurrentY = y;
    139         mMode = MODES.MOVE;
    140         computeValue();
    141         if (mListener != null) {
    142             mListener.onNewValue(mValue);
    143         }
    144     }
    145 
    146     public void setActionUp() {
    147         mMode = MODES.UP;
    148         mOriginalValue = computeValue();
    149         if (mListener != null) {
    150             mListener.onTouchUp();
    151         }
    152     }
    153 
    154     public void setNoAction() {
    155         mMode = MODES.NONE;
    156     }
    157 
    158     public void setListener(SliderListener listener) {
    159         mListener = listener;
    160     }
    161 
    162     public boolean onTouchEvent(MotionEvent event) {
    163         setNoAction();
    164         switch (event.getActionMasked()) {
    165             case (MotionEvent.ACTION_DOWN): {
    166                 setActionDown(event.getX(), event.getY());
    167                 break;
    168             }
    169             case (MotionEvent.ACTION_UP): {
    170                 setActionUp();
    171                 break;
    172             }
    173             case (MotionEvent.ACTION_CANCEL): {
    174                 setActionUp();
    175                 break;
    176             }
    177             case (MotionEvent.ACTION_MOVE): {
    178                 setActionMove(event.getX(), event.getY());
    179                 break;
    180             }
    181         }
    182         return true;
    183     }
    184 
    185     public void reset() {
    186         mOriginalValue = 0;
    187     }
    188 
    189 }
    190