Guest User

Untitled

a guest
May 31st, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package au.id.alexn;
  2.  
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.util.Log;
  6. import android.view.MotionEvent;
  7. import android.view.ScaleGestureDetector;
  8. import android.view.View;
  9. import android.widget.FrameLayout;
  10.  
  11. /**
  12. * Layout that provides pinch-zooming of content. This view should have exactly one child
  13. * view containing the content.
  14. */
  15. public class ZoomLayout extends FrameLayout implements ScaleGestureDetector.OnScaleGestureListener {
  16.  
  17. private enum Mode {
  18. NONE,
  19. DRAG,
  20. ZOOM
  21. }
  22.  
  23. private static final String TAG = "ZoomLayout";
  24. private static final float MIN_ZOOM = 1.0f;
  25. private static final float MAX_ZOOM = 4.0f;
  26.  
  27. private Mode mode = Mode.NONE;
  28. private float scale = 1.0f;
  29. private float lastScaleFactor = 0f;
  30.  
  31. // Where the finger first touches the screen
  32. private float startX = 0f;
  33. private float startY = 0f;
  34.  
  35. // How much to translate the canvas
  36. private float dx = 0f;
  37. private float dy = 0f;
  38. private float prevDx = 0f;
  39. private float prevDy = 0f;
  40.  
  41. public ZoomLayout(Context context) {
  42. super(context);
  43. init(context);
  44. }
  45.  
  46. public ZoomLayout(Context context, AttributeSet attrs) {
  47. super(context, attrs);
  48. init(context);
  49. }
  50.  
  51. public ZoomLayout(Context context, AttributeSet attrs, int defStyle) {
  52. super(context, attrs, defStyle);
  53. init(context);
  54. }
  55.  
  56. private void init(Context context) {
  57. final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context, this);
  58. this.setOnTouchListener(new View.OnTouchListener() {
  59. @Override
  60. public boolean onTouch(View view, MotionEvent motionEvent) {
  61. switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
  62. case MotionEvent.ACTION_DOWN:
  63. Log.i(TAG, "DOWN");
  64. if (scale > MIN_ZOOM) {
  65. mode = Mode.DRAG;
  66. startX = motionEvent.getX() - prevDx;
  67. startY = motionEvent.getY() - prevDy;
  68. }
  69. break;
  70. case MotionEvent.ACTION_MOVE:
  71. if (mode == Mode.DRAG) {
  72. dx = motionEvent.getX() - startX;
  73. dy = motionEvent.getY() - startY;
  74. }
  75. break;
  76. case MotionEvent.ACTION_POINTER_DOWN:
  77. mode = Mode.ZOOM;
  78. break;
  79. case MotionEvent.ACTION_POINTER_UP:
  80. mode = Mode.DRAG;
  81. break;
  82. case MotionEvent.ACTION_UP:
  83. Log.i(TAG, "UP");
  84. mode = Mode.NONE;
  85. prevDx = dx;
  86. prevDy = dy;
  87. break;
  88. }
  89. scaleDetector.onTouchEvent(motionEvent);
  90.  
  91. if ((mode == Mode.DRAG && scale >= MIN_ZOOM) || mode == Mode.ZOOM) {
  92. getParent().requestDisallowInterceptTouchEvent(true);
  93. float maxDx = (child().getWidth() - (child().getWidth() / scale)) / 2 * scale;
  94. float maxDy = (child().getHeight() - (child().getHeight() / scale))/ 2 * scale;
  95. dx = Math.min(Math.max(dx, -maxDx), maxDx);
  96. dy = Math.min(Math.max(dy, -maxDy), maxDy);
  97. Log.i(TAG, "Width: " + child().getWidth() + ", scale " + scale + ", dx " + dx
  98. + ", max " + maxDx);
  99. applyScaleAndTranslation();
  100. }
  101.  
  102. return true;
  103. }
  104. });
  105. }
  106.  
  107. // ScaleGestureDetector
  108.  
  109. @Override
  110. public boolean onScaleBegin(ScaleGestureDetector scaleDetector) {
  111. Log.i(TAG, "onScaleBegin");
  112. return true;
  113. }
  114.  
  115. @Override
  116. public boolean onScale(ScaleGestureDetector scaleDetector) {
  117. float scaleFactor = scaleDetector.getScaleFactor();
  118. Log.i(TAG, "onScale" + scaleFactor);
  119. if (lastScaleFactor == 0 || (Math.signum(scaleFactor) == Math.signum(lastScaleFactor))) {
  120. scale *= scaleFactor;
  121. scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
  122. lastScaleFactor = scaleFactor;
  123. } else {
  124. lastScaleFactor = 0;
  125. }
  126. return true;
  127. }
  128.  
  129. @Override
  130. public void onScaleEnd(ScaleGestureDetector scaleDetector) {
  131. Log.i(TAG, "onScaleEnd");
  132. }
  133.  
  134. private void applyScaleAndTranslation() {
  135. child().setScaleX(scale);
  136. child().setScaleY(scale);
  137. child().setTranslationX(dx);
  138. child().setTranslationY(dy);
  139. }
  140.  
  141. private View child() {
  142. return getChildAt(0);
  143. }
  144. }
Add Comment
Please, Sign In to add comment