Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. package com.example.wolfgang.maps;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Paint;
  9. import android.util.AttributeSet;
  10. import android.view.Display;
  11. import android.view.MotionEvent;
  12. import android.view.ScaleGestureDetector;
  13. import android.view.WindowManager;
  14. import android.widget.ImageView;
  15.  
  16. //http://vivin.net/2011/12/04/implementing-pinch-zoom-and-pandrag-in-an-android-view-on-the-canvas/8/
  17.  
  18. public class TransformationImgView extends ImageView
  19. {
  20. Paint paint = new Paint();
  21. Bitmap b;
  22.  
  23. //These two constants specify the minimum and maximum zoom
  24. private static float MIN_ZOOM = 1f;
  25. private static float MAX_ZOOM = 5f;
  26.  
  27. private float scaleFactor = 1.f;
  28. private ScaleGestureDetector detector;
  29.  
  30. //These constants specify the mode that we’re in
  31. private static int NONE = 0;
  32. private static int DRAG = 1;
  33. private static int ZOOM = 2;
  34.  
  35. private int mode;
  36.  
  37. //These two variables keep track of the X and Y coordinate of the finger when it first
  38. //touches the screen
  39. private float startX = 0f;
  40. private float startY = 0f;
  41.  
  42. //These two variables keep track of the amount we need to translate the canvas along the X
  43. //and the Y coordinate
  44. private float translateX = 0f;
  45. private float translateY = 0f;
  46.  
  47.  
  48.  
  49. //These two variables keep track of the amount we translated the X and Y coordinates, the last time we
  50. //panned.
  51. private float previousTranslateX = 0f;
  52. private float previousTranslateY = 0f;
  53.  
  54. private boolean dragged = false;
  55. private float displayWidth;
  56. private float displayHeight;
  57. Display display;
  58. public TransformationImgView(Context context, AttributeSet attrs)
  59. {
  60. super(context, attrs);
  61. paint.setColor(Color.BLACK);
  62. b = BitmapFactory.decodeResource(getResources(), R.drawable.mapklein);
  63. detector = new ScaleGestureDetector(getContext(), new ScaleListener());
  64.  
  65. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  66. display = wm.getDefaultDisplay();
  67.  
  68. displayWidth = this.getWidth();
  69. displayHeight = this.getHeight();
  70. }
  71.  
  72. @Override
  73. public boolean onTouchEvent(MotionEvent event)
  74. {
  75. switch (event.getAction() & MotionEvent.ACTION_MASK)
  76. {
  77. case MotionEvent.ACTION_DOWN:
  78. mode = DRAG;
  79.  
  80. //We assign the current X and Y coordinate of the finger to startX and startY minus the previously translated
  81. //amount for each coordinates This works even when we are translating the first time because the initial
  82. //values for these two variables is zero.
  83. startX = event.getX() - previousTranslateX;
  84. startY = event.getY() - previousTranslateY;
  85. break;
  86.  
  87. case MotionEvent.ACTION_MOVE:
  88. if(mode == DRAG)
  89. {
  90. translateX = event.getX() - startX;
  91. translateY = event.getY() - startY;
  92.  
  93. //We cannot use startX and startY directly because we have adjusted their values using the previous translation values.
  94. //This is why we need to add those values to startX and startY so that we can get the actual coordinates of the finger.
  95. double distance = Math.sqrt(Math.pow(event.getX() - (startX + previousTranslateX), 2) +
  96. Math.pow(event.getY() - (startY + previousTranslateY), 2));
  97.  
  98. if (distance > 0) {
  99. dragged = true;
  100. }
  101. }
  102.  
  103. break;
  104.  
  105. case MotionEvent.ACTION_POINTER_DOWN:
  106. mode = ZOOM;
  107. break;
  108.  
  109. case MotionEvent.ACTION_UP:
  110. mode = NONE;
  111. dragged = false;
  112.  
  113. //All fingers went up, so let’s save the value of translateX and translateY into previousTranslateX and
  114. //previousTranslate
  115. previousTranslateX = translateX;
  116. previousTranslateY = translateY;
  117. break;
  118.  
  119. case MotionEvent.ACTION_POINTER_UP:
  120. mode = NONE;
  121.  
  122. //This is not strictly necessary; we save the value of translateX and translateY into previousTranslateX
  123. //and previousTranslateY when the second finger goes up
  124. previousTranslateX = translateX;
  125. previousTranslateY = translateY;
  126. break;
  127. }
  128.  
  129. detector.onTouchEvent(event);
  130.  
  131. //We redraw the canvas only in the following cases:
  132. //
  133. // o The mode is ZOOM
  134. // OR
  135. // o The mode is DRAG and the scale factor is not equal to 1 (meaning we have zoomed) and dragged is
  136. // set to true (meaning the finger has actually moved)
  137. //if ((mode == DRAG && scaleFactor != 1f && dragged) || mode == ZOOM)
  138. if ((mode == DRAG && dragged) || mode == ZOOM)
  139. {
  140. invalidate();
  141. }
  142.  
  143. return true;
  144. }
  145.  
  146. @Override
  147. public void onDraw(Canvas canvas) {
  148.  
  149. canvas.save();
  150.  
  151. //We need to divide by the scale factor here, otherwise we end up with excessive panning based on our zoom level
  152. //because the translation amount also gets scaled according to how much we’ve zoomed into the canvas.
  153.  
  154.  
  155. canvas.scale(scaleFactor, scaleFactor);
  156. if ((translateX * -1) < 0)
  157. {
  158. translateX = 0;
  159. } else if (((translateX * -1)) > (b.getWidth() - (this.getWidth() / scaleFactor))*scaleFactor)
  160. {
  161. translateX = (-(b.getWidth() - (this.getWidth() / (scaleFactor))))*scaleFactor;
  162. }
  163.  
  164. if ((translateY * -1) < 0)
  165. {
  166. translateY = 0;
  167. } else if (((translateY * -1)) > (b.getHeight() - (this.getHeight() / scaleFactor))*scaleFactor)
  168. {
  169. translateY = (-(b.getWidth() - (this.getHeight() / (scaleFactor))))*scaleFactor;
  170. }
  171.  
  172. canvas.translate(translateX /scaleFactor, translateY /scaleFactor);
  173.  
  174.  
  175.  
  176. //We’re going to scale the X and Y coordinates by the same amount
  177.  
  178. super.onDraw(canvas);
  179.  
  180. canvas.drawBitmap(b, 0,0, null);
  181.  
  182. canvas.restore();
  183. }
  184.  
  185. private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
  186. {
  187. @Override
  188. public boolean onScale(ScaleGestureDetector detector)
  189. {
  190. scaleFactor *= detector.getScaleFactor();
  191. scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
  192. return true;
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement