Guest User

Untitled

a guest
May 20th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1.  
  2. import android.content.Context;
  3. import android.graphics.Matrix;
  4. import android.graphics.PointF;
  5. import android.graphics.drawable.Drawable;
  6. import android.util.AttributeSet;
  7. import android.util.Log;
  8. import android.view.MotionEvent;
  9. import android.view.ScaleGestureDetector;
  10. import android.view.View;
  11. import android.widget.ImageView;
  12.  
  13. public class TouchImageView extends ImageView {
  14.  
  15. Matrix matrix;
  16.  
  17. // We can be in one of these 3 states
  18. static final int NONE = 0;
  19. static final int DRAG = 1;
  20. static final int ZOOM = 2;
  21. int mode = NONE;
  22.  
  23. // Remember some things for zooming
  24. PointF last = new PointF();
  25. PointF start = new PointF();
  26. float minScale = 1f;
  27. float maxScale = 3f;
  28. float[] m;
  29.  
  30. int viewWidth, viewHeight;
  31. static final int CLICK = 3;
  32. float saveScale = 1f;
  33. protected float origWidth, origHeight;
  34. int oldMeasuredWidth, oldMeasuredHeight;
  35.  
  36. ScaleGestureDetector mScaleDetector;
  37.  
  38. Context context;
  39.  
  40. public TouchImageView(Context context) {
  41. super(context);
  42. sharedConstructing(context);
  43. }
  44.  
  45. public TouchImageView(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. sharedConstructing(context);
  48. }
  49.  
  50. private void sharedConstructing(Context context) {
  51. super.setClickable(true);
  52. this.context = context;
  53. mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
  54. matrix = new Matrix();
  55. m = new float[9];
  56. setImageMatrix(matrix);
  57. setScaleType(ScaleType.MATRIX);
  58.  
  59. setOnTouchListener(new OnTouchListener() {
  60.  
  61. @Override
  62. public boolean onTouch(View v, MotionEvent event) {
  63. mScaleDetector.onTouchEvent(event);
  64. PointF curr = new PointF(event.getX(), event.getY());
  65.  
  66. switch (event.getAction()) {
  67. case MotionEvent.ACTION_DOWN:
  68. last.set(curr);
  69. start.set(last);
  70. mode = DRAG;
  71. break;
  72.  
  73. case MotionEvent.ACTION_MOVE:
  74. if (mode == DRAG) {
  75. float deltaX = curr.x - last.x;
  76. float deltaY = curr.y - last.y;
  77. float fixTransX = getFixDragTrans(deltaX, viewWidth,
  78. origWidth * saveScale);
  79. float fixTransY = getFixDragTrans(deltaY, viewHeight,
  80. origHeight * saveScale);
  81. matrix.postTranslate(fixTransX, fixTransY);
  82. fixTrans();
  83. last.set(curr.x, curr.y);
  84. }
  85. break;
  86.  
  87. case MotionEvent.ACTION_UP:
  88. mode = NONE;
  89. int xDiff = (int) Math.abs(curr.x - start.x);
  90. int yDiff = (int) Math.abs(curr.y - start.y);
  91. if (xDiff < CLICK && yDiff < CLICK)
  92. performClick();
  93. break;
  94.  
  95. case MotionEvent.ACTION_POINTER_UP:
  96. mode = NONE;
  97. break;
  98. }
  99.  
  100. setImageMatrix(matrix);
  101. invalidate();
  102. return true; // indicate event was handled
  103. }
  104.  
  105. });
  106. }
  107.  
  108. public void setMaxZoom(float x) {
  109. maxScale = x;
  110. }
  111.  
  112. private class ScaleListener extends
  113. ScaleGestureDetector.SimpleOnScaleGestureListener {
  114. @Override
  115. public boolean onScaleBegin(ScaleGestureDetector detector) {
  116. mode = ZOOM;
  117. return true;
  118. }
  119.  
  120. @Override
  121. public boolean onScale(ScaleGestureDetector detector) {
  122. float mScaleFactor = detector.getScaleFactor();
  123. float origScale = saveScale;
  124. saveScale *= mScaleFactor;
  125. if (saveScale > maxScale) {
  126. saveScale = maxScale;
  127. mScaleFactor = maxScale / origScale;
  128. } else if (saveScale < minScale) {
  129. saveScale = minScale;
  130. mScaleFactor = minScale / origScale;
  131. }
  132.  
  133. if (origWidth * saveScale <= viewWidth
  134. || origHeight * saveScale <= viewHeight)
  135. matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2,
  136. viewHeight / 2);
  137. else
  138. matrix.postScale(mScaleFactor, mScaleFactor,
  139. detector.getFocusX(), detector.getFocusY());
  140.  
  141. fixTrans();
  142. return true;
  143. }
  144. }
  145.  
  146. void fixTrans() {
  147. matrix.getValues(m);
  148. float transX = m[Matrix.MTRANS_X];
  149. float transY = m[Matrix.MTRANS_Y];
  150.  
  151. float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
  152. float fixTransY = getFixTrans(transY, viewHeight, origHeight
  153. * saveScale);
  154.  
  155. if (fixTransX != 0 || fixTransY != 0)
  156. matrix.postTranslate(fixTransX, fixTransY);
  157. }
  158.  
  159. float getFixTrans(float trans, float viewSize, float contentSize) {
  160. float minTrans, maxTrans;
  161.  
  162. if (contentSize <= viewSize) {
  163. minTrans = 0;
  164. maxTrans = viewSize - contentSize;
  165. } else {
  166. minTrans = viewSize - contentSize;
  167. maxTrans = 0;
  168. }
  169.  
  170. if (trans < minTrans)
  171. return -trans + minTrans;
  172. if (trans > maxTrans)
  173. return -trans + maxTrans;
  174. return 0;
  175. }
  176.  
  177. float getFixDragTrans(float delta, float viewSize, float contentSize) {
  178. if (contentSize <= viewSize) {
  179. return 0;
  180. }
  181. return delta;
  182. }
  183.  
  184. @Override
  185. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  186. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  187. viewWidth = MeasureSpec.getSize(widthMeasureSpec);
  188. viewHeight = MeasureSpec.getSize(heightMeasureSpec);
  189.  
  190. //
  191. // Rescales image on rotation
  192. //
  193. if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
  194. || viewWidth == 0 || viewHeight == 0)
  195. return;
  196. oldMeasuredHeight = viewHeight;
  197. oldMeasuredWidth = viewWidth;
  198.  
  199. if (saveScale == 1) {
  200. // Fit to screen.
  201. float scale;
  202.  
  203. Drawable drawable = getDrawable();
  204. if (drawable == null || drawable.getIntrinsicWidth() == 0
  205. || drawable.getIntrinsicHeight() == 0)
  206. return;
  207. int bmWidth = drawable.getIntrinsicWidth();
  208. int bmHeight = drawable.getIntrinsicHeight();
  209.  
  210. Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);
  211.  
  212. float scaleX = (float) viewWidth / (float) bmWidth;
  213. float scaleY = (float) viewHeight / (float) bmHeight;
  214. scale = Math.min(scaleX, scaleY);
  215. matrix.setScale(scale, scale);
  216.  
  217. // Center the image
  218. float redundantYSpace = (float) viewHeight
  219. - (scale * (float) bmHeight);
  220. float redundantXSpace = (float) viewWidth
  221. - (scale * (float) bmWidth);
  222. redundantYSpace /= (float) 2;
  223. redundantXSpace /= (float) 2;
  224.  
  225. matrix.postTranslate(redundantXSpace, redundantYSpace);
  226.  
  227. origWidth = viewWidth - 2 * redundantXSpace;
  228. origHeight = viewHeight - 2 * redundantYSpace;
  229. setImageMatrix(matrix);
  230. }
  231. fixTrans();
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment