PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/magnify/src/com/google/marvin/magnify/ZoomView.java

http://eyes-free.googlecode.com/
Java | 137 lines | 113 code | 24 blank | 0 comment | 20 complexity | 51283d34137ad29c928a50160d3fb503 MD5 | raw file
  1package com.google.marvin.magnify;
  2
  3import android.content.Context;
  4import android.content.res.Resources;
  5import android.graphics.Canvas;
  6import android.graphics.Paint;
  7import android.graphics.Rect;
  8import android.graphics.Paint.Style;
  9import android.util.AttributeSet;
 10import android.util.Log;
 11import android.view.MotionEvent;
 12import android.view.View;
 13
 14public class ZoomView extends View implements View.OnTouchListener,
 15    View.OnClickListener {
 16  private static final String TAG = "ZoomView";
 17  private static final int MIN_SIZE = 20;
 18  
 19  private OnZoomListener mOnZoomListener;
 20  private Paint mPaint;
 21  private Rect mRect;
 22  private Rect mLast;
 23  private int anchorX;
 24  private int anchorY;
 25  
 26  public interface OnZoomListener {
 27    public void onResize(View v, Rect r);
 28    public void onSelect(View v, Rect r);
 29  }
 30
 31  public ZoomView(Context context, AttributeSet attrs) {
 32    super(context, attrs);
 33    
 34    Resources resources = getResources();
 35    int color = resources.getColor(R.color.zoom_box);
 36    
 37    mPaint = new Paint();
 38    mPaint.setAntiAlias(true);
 39    mPaint.setStyle(Style.FILL);
 40    mPaint.setColor(color);
 41    
 42    mRect = null;
 43    
 44    setOnTouchListener(this);
 45  }
 46  
 47  @Override
 48  public boolean onTouch(View v, MotionEvent event) {
 49    int w = v.getWidth();
 50    int h = v.getHeight();
 51    int x = (int)event.getX();
 52    int y = (int)event.getY();
 53
 54    int action = event.getAction();
 55    switch (action) {
 56      case MotionEvent.ACTION_DOWN:
 57        Log.i(TAG, "Received onTouch event (action DOWN).");
 58        mLast = mRect;
 59        mRect = new Rect(x, y, x, y);
 60        anchorX = x;
 61        anchorY = y;
 62        setWithAspect(x, y);
 63        return false;
 64      case MotionEvent.ACTION_MOVE:
 65        setWithAspect(x, y);
 66        return true;
 67      case MotionEvent.ACTION_UP:
 68        Log.i(TAG, "Received onTouch event (action UP).");
 69        return fireOnZoom();
 70      default:
 71        Log.i(TAG, "Received onTouch event (action " + action + ").");
 72        return false;
 73    }
 74  }
 75  
 76  @Override
 77  protected void onDraw(Canvas canvas) {
 78    if (mRect != null
 79        && mRect.height() > MIN_SIZE
 80        && mRect.width() > MIN_SIZE) {
 81      canvas.drawRect(mRect, mPaint);
 82    }
 83  }
 84  
 85  private void setWithAspect(int r, int b) {
 86    int l = anchorX;
 87    int t = anchorY;
 88    
 89    if (r < l || b < t) {
 90      l = r;
 91      t = b;
 92      r = anchorX;
 93      b = anchorY;
 94    }
 95    
 96    int w = r - l;
 97    int h = b - t;
 98    
 99    
100    if (w != 0 && h != 0) {
101      float aspect = (w * getHeight()) / (float) (h * getWidth());
102      
103      if (aspect < 1) {
104        w /= aspect;
105      } else if (aspect > 1) {
106        h *= aspect;
107      }
108    }
109    
110    mRect.set(l, t, l + w, t + h);
111    postInvalidate();
112  }
113  
114  public void setOnZoomListener(OnZoomListener onZoomListener) {
115    mOnZoomListener = onZoomListener;
116  }
117  
118  private boolean fireOnZoom() {
119    if (mOnZoomListener != null
120        && mRect.width() > MIN_SIZE
121        && mRect.height() > MIN_SIZE) {
122      mOnZoomListener.onResize(this, mRect);
123      return true;
124    }
125    
126    Log.i(TAG, "Didn't consume ACTION_UP.");
127    
128    return false;
129  }
130
131  @Override
132  public void onClick(View v) {
133    if (mLast != null) {
134      mOnZoomListener.onSelect(this, mLast);
135    }
136  }
137}