| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package eVVM.apk.helper;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.util.AttributeSet;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.ScaleGestureDetector;
- import android.widget.FrameLayout;
- public class CommunityView extends FrameLayout {
- public Matrix matrix; // 缩放矩阵
- private int lastX;
- private int lastY;
- private int downX;
- private int downY;
- private boolean isScale; // true:正在缩放
- private boolean pointer; // 是否多个手指
- private int count = 0;//点击次数
- private long firstClick = 0;//第一次点击时间
- private long secondClick = 0;//第二次点击时间
- private float scale = 1.0f;
- private final float maxScale = 3.0f;
- private final float minScale = 1.0f;
- /**
- * 两次点击时间间隔,单位毫秒
- */
- private final int totalTime = 300;
- public CommunityView(@NonNull Context context) {
- this(context, null);
- }
- public CommunityView(@NonNull Context context, @Nullable AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public CommunityView(@NonNull final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- matrix = new Matrix();
- }
- @Override protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.concat(matrix);
- }
- @Override public boolean onTouchEvent(MotionEvent event) {
- int x = (int) event.getX();
- int y = (int) event.getY();
- gestureDetector.onTouchEvent(event);
- scaleGestureDetector.onTouchEvent(event);
- if (event.getPointerCount() > 1) {
- pointer = true;
- }else{
- int action = event.getAction() & MotionEvent.ACTION_MASK;
- switch (action) {
- case MotionEvent.ACTION_DOWN: {
- count++;
- if (1 == count) {
- firstClick = System.currentTimeMillis();//记录第一次点击时间
- } else if (2 == count) {
- secondClick = System.currentTimeMillis();//记录第二次点击时间
- if (secondClick - firstClick < totalTime) {//判断二次点击时间间隔是否在设定的间隔时间之内
- if (!isScale) {
- if (scale < 2) {
- float scaleFactor = 2/scale;
- scale = 2;
- matrix.postScale(scaleFactor, scaleFactor, x, y);
- invalidate();
- } else {
- float scaleFactor = 1/scale;
- scale = 1;
- matrix.postScale(scaleFactor, scaleFactor, x, y);
- invalidate();
- }
- }
- count = 0;
- firstClick = 0;
- } else {
- firstClick = secondClick;
- count = 1;
- }
- secondClick = 0;
- }
- break;
- }
- }
- }
- int action = event.getAction();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- downX = x;
- downY = y;
- pointer = false;
- break;
- case MotionEvent.ACTION_MOVE:
- if (!isScale) {
- int dx = Math.abs(x - downX);
- int dy = Math.abs(y - downY);
- if (dx > 10 && dy > 10 && !pointer) {
- dx = x - lastX;
- dy = y - lastY;
- matrix.postTranslate(dx, dy);
- invalidate();
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- break;
- }
- lastX = x;
- lastY = y;
- return true;
- }
- ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
- @Override public boolean onScale(ScaleGestureDetector detector) {
- float scaleFactor = detector.getScaleFactor();
- if(scale * scaleFactor >= minScale && scale * scaleFactor <= maxScale){
- scale *= scaleFactor;
- matrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());
- invalidate();
- }
- return true;
- }
- @Override public boolean onScaleBegin(ScaleGestureDetector detector) {
- isScale = true;
- return true;
- }
- @Override public void onScaleEnd(ScaleGestureDetector detector) {
- isScale = false;
- }
- });
- GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
- @Override public boolean onSingleTapConfirmed(MotionEvent e) {
- return super.onSingleTapConfirmed(e);
- }
- });
- }
|