Guest User

Untitled

a guest
Jan 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package com.example.poweruser.fcemsandroid;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Matrix;
  5. import android.graphics.PointF;
  6. import android.graphics.drawable.Drawable;
  7. import android.util.AttributeSet;
  8. import android.util.Log;
  9. import android.view.MotionEvent;
  10. import android.view.ScaleGestureDetector;
  11. import android.view.View;
  12. import android.widget.ImageView;
  13.  
  14.  
  15. public class TouchImageView extends ImageView {
  16. Matrix matrix;
  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.  
  22. int mode = NONE;
  23.  
  24. // Remember some things for zooming
  25. PointF last = new PointF();
  26. PointF start = new PointF();
  27. float minScale = 1f;
  28. float maxScale = 3f;
  29. float[] m;
  30. int viewWidth, viewHeight;
  31.  
  32. static final int CLICK = 3;
  33.  
  34. float saveScale = 1f;
  35.  
  36. protected float origWidth, origHeight;
  37.  
  38. int oldMeasuredWidth, oldMeasuredHeight;
  39.  
  40. ScaleGestureDetector mScaleDetector;
  41.  
  42. Context context;
  43.  
  44. public TouchImageView() {}
  45.  
  46. public TouchImageView(Context context) {
  47. super(context);
  48. sharedConstructing(context);
  49. }
  50.  
  51. public TouchImageView(Context context, AttributeSet attrs) {
  52. super(context, attrs);
  53. sharedConstructing(context);
  54. }
  55.  
  56. private void sharedConstructing(Context context) {
  57.  
  58. super.setClickable(true);
  59.  
  60. this.context = context;
  61.  
  62. mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
  63.  
  64. matrix = new Matrix();
  65.  
  66. m = new float[9];
  67.  
  68. setImageMatrix(matrix);
  69.  
  70. setScaleType(ScaleType.MATRIX);
  71.  
  72. setOnTouchListener(new OnTouchListener() {
  73.  
  74. @Override
  75. public boolean onTouch(View v, MotionEvent event) {
  76.  
  77. mScaleDetector.onTouchEvent(event);
  78.  
  79. PointF curr = new PointF(event.getX(), event.getY());
  80.  
  81. switch (event.getAction()) {
  82.  
  83. case MotionEvent.ACTION_DOWN:
  84.  
  85. last.set(curr);
  86.  
  87. start.set(last);
  88.  
  89. mode = DRAG;
  90.  
  91. break;
  92.  
  93. case MotionEvent.ACTION_MOVE:
  94.  
  95. if (mode == DRAG) {
  96.  
  97. float deltaX = curr.x - last.x;
  98.  
  99. float deltaY = curr.y - last.y;
  100.  
  101. float fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale);
  102.  
  103. float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale);
  104.  
  105. matrix.postTranslate(fixTransX, fixTransY);
  106.  
  107. fixTrans();
  108.  
  109. last.set(curr.x, curr.y);
  110.  
  111. }
  112.  
  113. break;
  114.  
  115. case MotionEvent.ACTION_UP:
  116.  
  117. mode = NONE;
  118.  
  119. int xDiff = (int) Math.abs(curr.x - start.x);
  120.  
  121. int yDiff = (int) Math.abs(curr.y - start.y);
  122.  
  123. if (xDiff < CLICK && yDiff < CLICK)
  124.  
  125. performClick();
  126.  
  127. break;
  128.  
  129. case MotionEvent.ACTION_POINTER_UP:
  130.  
  131. mode = NONE;
  132.  
  133. break;
  134.  
  135. }
  136.  
  137. setImageMatrix(matrix);
  138.  
  139. invalidate();
  140.  
  141. return true; // indicate event was handled
  142.  
  143. }
  144.  
  145. });
  146. }
  147.  
  148. public void setMaxZoom(float x) {
  149.  
  150. maxScale = x;
  151.  
  152. }
  153.  
  154. private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  155.  
  156. @Override
  157. public boolean onScaleBegin(ScaleGestureDetector detector) {
  158.  
  159. mode = ZOOM;
  160.  
  161. return true;
  162.  
  163. }
  164.  
  165. @Override
  166. public boolean onScale(ScaleGestureDetector detector) {
  167.  
  168. float mScaleFactor = detector.getScaleFactor();
  169.  
  170. float origScale = saveScale;
  171.  
  172. saveScale *= mScaleFactor;
  173.  
  174. if (saveScale > maxScale) {
  175.  
  176. saveScale = maxScale;
  177.  
  178. mScaleFactor = maxScale / origScale;
  179.  
  180. } else if (saveScale < minScale) {
  181.  
  182. saveScale = minScale;
  183.  
  184. mScaleFactor = minScale / origScale;
  185.  
  186. }
  187.  
  188. if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight)
  189.  
  190. matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2, viewHeight / 2);
  191.  
  192. else
  193.  
  194. matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
  195.  
  196. fixTrans();
  197.  
  198. return true;
  199.  
  200. }
  201.  
  202. }
  203.  
  204. void fixTrans() {
  205.  
  206. matrix.getValues(m);
  207.  
  208. float transX = m[Matrix.MTRANS_X];
  209.  
  210. float transY = m[Matrix.MTRANS_Y];
  211.  
  212. float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
  213.  
  214. float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale);
  215.  
  216. if (fixTransX != 0 || fixTransY != 0)
  217.  
  218. matrix.postTranslate(fixTransX, fixTransY);
  219.  
  220. }
  221.  
  222.  
  223.  
  224. float getFixTrans(float trans, float viewSize, float contentSize) {
  225.  
  226. float minTrans, maxTrans;
  227.  
  228. if (contentSize <= viewSize) {
  229.  
  230. minTrans = 0;
  231.  
  232. maxTrans = viewSize - contentSize;
  233.  
  234. } else {
  235.  
  236. minTrans = viewSize - contentSize;
  237.  
  238. maxTrans = 0;
  239.  
  240. }
  241.  
  242. if (trans < minTrans)
  243.  
  244. return -trans + minTrans;
  245.  
  246. if (trans > maxTrans)
  247.  
  248. return -trans + maxTrans;
  249.  
  250. return 0;
  251.  
  252. }
  253.  
  254. float getFixDragTrans(float delta, float viewSize, float contentSize) {
  255.  
  256. if (contentSize <= viewSize) {
  257.  
  258. return 0;
  259.  
  260. }
  261.  
  262. return delta;
  263.  
  264. }
  265.  
  266. @Override
  267. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  268.  
  269. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  270.  
  271. viewWidth = MeasureSpec.getSize(widthMeasureSpec);
  272.  
  273. viewHeight = MeasureSpec.getSize(heightMeasureSpec);
  274.  
  275. //
  276. // Rescales image on rotation
  277. //
  278. if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
  279.  
  280. || viewWidth == 0 || viewHeight == 0)
  281.  
  282. return;
  283.  
  284. oldMeasuredHeight = viewHeight;
  285.  
  286. oldMeasuredWidth = viewWidth;
  287.  
  288. if (saveScale == 1) {
  289.  
  290. //Fit to screen.
  291.  
  292. float scale;
  293.  
  294. Drawable drawable = getDrawable();
  295.  
  296. if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0)
  297.  
  298. return;
  299.  
  300. int bmWidth = drawable.getIntrinsicWidth();
  301.  
  302. int bmHeight = drawable.getIntrinsicHeight();
  303.  
  304. Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);
  305.  
  306. float scaleX = (float) viewWidth / (float) bmWidth;
  307.  
  308. float scaleY = (float) viewHeight / (float) bmHeight;
  309.  
  310. scale = Math.min(scaleX, scaleY);
  311.  
  312. matrix.setScale(scale, scale);
  313.  
  314. // Center the image
  315.  
  316. float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight);
  317.  
  318. float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth);
  319.  
  320. redundantYSpace /= (float) 2;
  321.  
  322. redundantXSpace /= (float) 2;
  323.  
  324. matrix.postTranslate(redundantXSpace, redundantYSpace);
  325.  
  326. origWidth = viewWidth - 2 * redundantXSpace;
  327.  
  328. origHeight = viewHeight - 2 * redundantYSpace;
  329.  
  330. setImageMatrix(matrix);
  331.  
  332. }
  333.  
  334. fixTrans();
  335.  
  336. }
Add Comment
Please, Sign In to add comment