/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

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