/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

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