e54c7410b173bdaf40f0779d83ceac1c52ab6735.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package eVVM.apk.helper;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Matrix;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.util.AttributeSet;
  8. import android.view.GestureDetector;
  9. import android.view.MotionEvent;
  10. import android.view.ScaleGestureDetector;
  11. import android.widget.FrameLayout;
  12. public class CommunityView extends FrameLayout {
  13. public Matrix matrix; // 缩放矩阵
  14. private int lastX;
  15. private int lastY;
  16. private int downX;
  17. private int downY;
  18. private boolean isScale; // true:正在缩放
  19. private boolean pointer; // 是否多个手指
  20. private int count = 0;//点击次数
  21. private long firstClick = 0;//第一次点击时间
  22. private long secondClick = 0;//第二次点击时间
  23. private float scale = 1.0f;
  24. private final float maxScale = 3.0f;
  25. private final float minScale = 1.0f;
  26. /**
  27. * 两次点击时间间隔,单位毫秒
  28. */
  29. private final int totalTime = 300;
  30. public CommunityView(@NonNull Context context) {
  31. this(context, null);
  32. }
  33. public CommunityView(@NonNull Context context, @Nullable AttributeSet attrs) {
  34. this(context, attrs, 0);
  35. }
  36. public CommunityView(@NonNull final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  37. super(context, attrs, defStyleAttr);
  38. matrix = new Matrix();
  39. }
  40. @Override protected void onDraw(Canvas canvas) {
  41. super.onDraw(canvas);
  42. canvas.concat(matrix);
  43. }
  44. @Override public boolean onTouchEvent(MotionEvent event) {
  45. int x = (int) event.getX();
  46. int y = (int) event.getY();
  47. gestureDetector.onTouchEvent(event);
  48. scaleGestureDetector.onTouchEvent(event);
  49. if (event.getPointerCount() > 1) {
  50. pointer = true;
  51. }else{
  52. int action = event.getAction() & MotionEvent.ACTION_MASK;
  53. switch (action) {
  54. case MotionEvent.ACTION_DOWN: {
  55. count++;
  56. if (1 == count) {
  57. firstClick = System.currentTimeMillis();//记录第一次点击时间
  58. } else if (2 == count) {
  59. secondClick = System.currentTimeMillis();//记录第二次点击时间
  60. if (secondClick - firstClick < totalTime) {//判断二次点击时间间隔是否在设定的间隔时间之内
  61. if (!isScale) {
  62. if (scale < 2) {
  63. float scaleFactor = 2/scale;
  64. scale = 2;
  65. matrix.postScale(scaleFactor, scaleFactor, x, y);
  66. invalidate();
  67. } else {
  68. float scaleFactor = 1/scale;
  69. scale = 1;
  70. matrix.postScale(scaleFactor, scaleFactor, x, y);
  71. invalidate();
  72. }
  73. }
  74. count = 0;
  75. firstClick = 0;
  76. } else {
  77. firstClick = secondClick;
  78. count = 1;
  79. }
  80. secondClick = 0;
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. int action = event.getAction();
  87. switch (action) {
  88. case MotionEvent.ACTION_DOWN:
  89. downX = x;
  90. downY = y;
  91. pointer = false;
  92. break;
  93. case MotionEvent.ACTION_MOVE:
  94. if (!isScale) {
  95. int dx = Math.abs(x - downX);
  96. int dy = Math.abs(y - downY);
  97. if (dx > 10 && dy > 10 && !pointer) {
  98. dx = x - lastX;
  99. dy = y - lastY;
  100. matrix.postTranslate(dx, dy);
  101. invalidate();
  102. }
  103. }
  104. break;
  105. case MotionEvent.ACTION_UP:
  106. break;
  107. }
  108. lastX = x;
  109. lastY = y;
  110. return true;
  111. }
  112. ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
  113. @Override public boolean onScale(ScaleGestureDetector detector) {
  114. float scaleFactor = detector.getScaleFactor();
  115. if(scale * scaleFactor >= minScale && scale * scaleFactor <= maxScale){
  116. scale *= scaleFactor;
  117. matrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());
  118. invalidate();
  119. }
  120. return true;
  121. }
  122. @Override public boolean onScaleBegin(ScaleGestureDetector detector) {
  123. isScale = true;
  124. return true;
  125. }
  126. @Override public void onScaleEnd(ScaleGestureDetector detector) {
  127. isScale = false;
  128. }
  129. });
  130. GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
  131. @Override public boolean onSingleTapConfirmed(MotionEvent e) {
  132. return super.onSingleTapConfirmed(e);
  133. }
  134. });
  135. }