PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://eyes-free.googlecode.com/
Java | 161 lines | 97 code | 22 blank | 42 comment | 2 complexity | 867497baa6951bf00cd4a2b94d838d4a MD5 | raw file
  1package com.google.marvin.magnify;
  2
  3import android.content.Context;
  4import android.graphics.Bitmap;
  5import android.graphics.Canvas;
  6import android.graphics.ColorMatrix;
  7import android.graphics.ColorMatrixColorFilter;
  8import android.graphics.Matrix;
  9import android.graphics.Paint;
 10import android.graphics.RectF;
 11import android.graphics.Matrix.ScaleToFit;
 12import android.util.AttributeSet;
 13import android.view.GestureDetector;
 14import android.view.MotionEvent;
 15import android.view.View;
 16import android.view.GestureDetector.SimpleOnGestureListener;
 17
 18public class ContrastView extends View {
 19  @SuppressWarnings("unused")
 20  private static final String TAG = "ContrastView";
 21  
 22  private Paint mPaint = new Paint();
 23  private Bitmap mBitmap;
 24  private Matrix mMatrix;
 25  private RectF mBounds;
 26  private RectF mSource;
 27  
 28  private SimpleOnGestureListener mSimple;
 29  private GestureDetector mGesture;
 30  private OnTouchListener onTouch;
 31  
 32  public ContrastView(Context context, AttributeSet attrs) {
 33    super(context, attrs);
 34    
 35    mBounds = new RectF(0, 0, 0, 0);
 36    mSource = new RectF(0, 0, 0, 0);
 37    mMatrix = new Matrix();
 38    
 39    mSimple = new SimpleOnGestureListener() {
 40      @Override
 41      public boolean onSingleTapConfirmed(MotionEvent e) {
 42        performZoom((int) e.getX(), (int) e.getY(), 2);
 43        
 44        return false;
 45      }
 46
 47      @Override
 48      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
 49          float velocityY) {
 50        return false;
 51      }
 52
 53      @Override
 54      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
 55          float distanceY) {
 56        performDrag((int) distanceX, (int) distanceY);
 57        
 58        return true;
 59      }
 60    };
 61    
 62    mGesture = new GestureDetector(context, mSimple);
 63    
 64    onTouch = new OnTouchListener() {
 65      @Override
 66      public boolean onTouch(View v, MotionEvent event) {
 67        mGesture.onTouchEvent(event);
 68        
 69        return true;
 70      }
 71    };
 72    
 73    setOnTouchListener(onTouch);
 74  }
 75  
 76  public void performZoom(int centerX, int centerY, float zoom) {
 77    /*
 78    int w = mSource.width();
 79    int h = mSource.height();
 80    int x = mSource.left + w * centerX / getWidth();
 81    int y = mSource.top + h * centerY / getHeight();
 82    
 83    zoom *= 2;
 84    
 85    mSource.left = x - (int)(w / zoom);
 86    mSource.top = y - (int)(h / zoom);
 87    mSource.right = x + (int)(w / zoom);
 88    mSource.bottom = y + (int)(h / zoom);
 89    
 90    Log.i(TAG, "Zoomed in to " + mSource.left + "," + mSource.top + "," + mSource.right + "," + mSource.bottom);
 91    */
 92    
 93    mMatrix.postScale(zoom, zoom, centerX, centerY);
 94    postInvalidate();
 95  }
 96  
 97  public void performDrag(int deltaX, int deltaY) {
 98    /*
 99    int w = mSource.width();
100    int h = mSource.height();
101    int x = mSource.left + w * deltaX / getWidth();
102    int y = mSource.top + h * deltaY / getHeight();
103    
104    int nl = mSource.left + deltaX;
105    int nr = mSource.right + deltaX;
106    int nt = mSource.top + deltaY;
107    int nb = mSource.bottom + deltaY;
108
109    if (nl < 0) {
110      deltaX = 0 - mSource.left;
111    } else if (nr > mBitmap.getWidth()) {
112      deltaX = mBitmap.getWidth() - mSource.right;
113    }
114  
115    if (nt < 0) {
116      deltaY = 0 - mSource.top;
117    } else if (nb > mBitmap.getHeight()) {
118      deltaY = mBitmap.getHeight() - mSource.bottom;
119    }
120    
121    mSource.offset(deltaX, deltaY);
122    
123    Log.i(TAG, "Dragged to " + mSource.left + "," + mSource.top + "," + mSource.right + "," + mSource.bottom);
124    */
125    mMatrix.postTranslate(-deltaX, -deltaY);
126    postInvalidate();
127  }
128  
129  @Override
130  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
131    mBounds.set(0, 0, w, h);
132    mMatrix.setRectToRect(mSource, mBounds, ScaleToFit.CENTER);
133  }
134
135  public void setBitmap(Bitmap bitmap) {
136    mSource.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
137    mMatrix.setRectToRect(mSource, mBounds, ScaleToFit.CENTER);
138    mBitmap = bitmap;
139    postInvalidate();
140  }
141
142  public void setContrast(float contrast) {
143    float scale = contrast + 1.f;
144    float translate = (-.5f * scale + .5f) * 255.f;
145    float[] array = new float[] {
146        scale, 0, 0, 0, translate,
147        0, scale, 0, 0, translate,
148        0, 0, scale, 0, translate,
149        0, 0, 0, 1, 0};
150    ColorMatrix matrix = new ColorMatrix(array);
151    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
152    mPaint.setColorFilter(filter);
153  }
154
155  @Override
156  protected void onDraw(Canvas canvas) {
157    if (mBitmap != null) {
158      canvas.drawBitmap(mBitmap, mMatrix, mPaint);
159    }
160  }
161}