Guest User

Untitled

a guest
Mar 19th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. package com.dimon95me.photocollagejava.collage;
  2.  
  3. import android.view.MotionEvent;
  4. import android.view.View;
  5. import android.view.View.OnTouchListener;
  6.  
  7. public class MultiTouchListener implements OnTouchListener, View.OnClickListener {
  8.  
  9.     private static final int INVALID_POINTER_ID = -1;
  10.     public boolean isRotateEnabled = true;
  11.     public boolean isTranslateEnabled = true;
  12.     public boolean isScaleEnabled = true;
  13.     public float minimumScale = 0.5f;
  14.     public float maximumScale = 10.0f;
  15.     private int mActivePointerId = INVALID_POINTER_ID;
  16.     private float mPrevX;
  17.     private float mPrevY;
  18.     private ScaleGestureDetector mScaleGestureDetector;
  19.  
  20.     public MultiTouchListener() {
  21.         mScaleGestureDetector = new ScaleGestureDetector(new ScaleGestureListener());
  22.     }
  23.  
  24.     private static float adjustAngle(float degrees) {
  25.         if (degrees > 180.0f) {
  26.             degrees -= 360.0f;
  27.         } else if (degrees < -180.0f) {
  28.             degrees += 360.0f;
  29.         }
  30.  
  31.         return degrees;
  32.     }
  33.  
  34.     private static void move(View view, TransformInfo info) {
  35.         computeRenderOffset(view, info.pivotX, info.pivotY);
  36.         adjustTranslation(view, info.deltaX, info.deltaY);
  37.  
  38.         // Assume that scaling still maintains aspect ratio.
  39.         float scale = view.getScaleX() * info.deltaScale;
  40.         scale = Math.max(info.minimumScale, Math.min(info.maximumScale, scale));
  41.         view.setScaleX(scale);
  42.         view.setScaleY(scale);
  43.  
  44.         float rotation = adjustAngle(view.getRotation() + info.deltaAngle);
  45.         view.setRotation(rotation);
  46.     }
  47.  
  48.     private static void adjustTranslation(View view, float deltaX, float deltaY) {
  49.         float[] deltaVector = {deltaX, deltaY};
  50.         view.getMatrix().mapVectors(deltaVector);
  51.         view.setTranslationX(view.getTranslationX() + deltaVector[0]);
  52.         view.setTranslationY(view.getTranslationY() + deltaVector[1]);
  53.     }
  54.  
  55.     private static void computeRenderOffset(View view, float pivotX, float pivotY) {
  56.         if (view.getPivotX() == pivotX && view.getPivotY() == pivotY) {
  57.             return;
  58.         }
  59.  
  60.         float[] prevPoint = {0.0f, 0.0f};
  61.         view.getMatrix().mapPoints(prevPoint);
  62.  
  63.         view.setPivotX(pivotX);
  64.         view.setPivotY(pivotY);
  65.  
  66.         float[] currPoint = {0.0f, 0.0f};
  67.         view.getMatrix().mapPoints(currPoint);
  68.  
  69.         float offsetX = currPoint[0] - prevPoint[0];
  70.         float offsetY = currPoint[1] - prevPoint[1];
  71.  
  72.         view.setTranslationX(view.getTranslationX() - offsetX);
  73.         view.setTranslationY(view.getTranslationY() - offsetY);
  74.     }
  75.  
  76.     @Override
  77.     public boolean onTouch(View view, MotionEvent event) {
  78.         mScaleGestureDetector.onTouchEvent(view, event);
  79.  
  80.         if (!isTranslateEnabled) {
  81.             return true;
  82.         }
  83.  
  84.         int action = event.getAction();
  85.         switch (action & event.getActionMasked()) {
  86.             case MotionEvent.ACTION_DOWN: {
  87.                 mPrevX = event.getX();
  88.                 mPrevY = event.getY();
  89.  
  90.                 // Save the ID of this pointer.
  91.                 mActivePointerId = event.getPointerId(0);
  92.                 break;
  93.             }
  94.  
  95.             case MotionEvent.ACTION_MOVE: {
  96.                 // Find the index of the active pointer and fetch its position.
  97.                 int pointerIndex = event.findPointerIndex(mActivePointerId);
  98.                 if (pointerIndex != -1) {
  99.                     float currX = event.getX(pointerIndex);
  100.                     float currY = event.getY(pointerIndex);
  101.  
  102.                     // Only move if the ScaleGestureDetector isn't processing a
  103.                     // gesture.
  104.                     if (!mScaleGestureDetector.isInProgress()) {
  105.                         adjustTranslation(view, currX - mPrevX, currY - mPrevY);
  106.                     }
  107.                 }
  108.  
  109.                 break;
  110.             }
  111.  
  112.             case MotionEvent.ACTION_CANCEL:
  113.                 mActivePointerId = INVALID_POINTER_ID;
  114.                 break;
  115.  
  116.             case MotionEvent.ACTION_UP:
  117.                 mActivePointerId = INVALID_POINTER_ID;
  118.                 break;
  119.  
  120.             case MotionEvent.ACTION_POINTER_UP: {
  121.                 // Extract the index of the pointer that left the touch sensor.
  122.                 int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  123.                 int pointerId = event.getPointerId(pointerIndex);
  124.                 if (pointerId == mActivePointerId) {
  125.                     // This was our active pointer going up. Choose a new
  126.                     // active pointer and adjust accordingly.
  127.                     int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  128.                     mPrevX = event.getX(newPointerIndex);
  129.                     mPrevY = event.getY(newPointerIndex);
  130.                     mActivePointerId = event.getPointerId(newPointerIndex);
  131.                 }
  132.  
  133.                 break;
  134.             }
  135.         }
  136.  
  137.         return true;
  138.     }
  139.  
  140.     @Override
  141.     public void onClick(View v) {
  142.         System.out.println("fhsdlhflasdhfksdl");
  143.     }
  144.  
  145.     private class ScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  146.  
  147.         private float mPivotX;
  148.         private float mPivotY;
  149.         private Vector2D mPrevSpanVector = new Vector2D();
  150.  
  151.         @Override
  152.         public boolean onScaleBegin(View view, ScaleGestureDetector detector) {
  153.             mPivotX = detector.getFocusX();
  154.             mPivotY = detector.getFocusY();
  155.             mPrevSpanVector.set(detector.getCurrentSpanVector());
  156.             return true;
  157.         }
  158.  
  159.         @Override
  160.         public boolean onScale(View view, ScaleGestureDetector detector) {
  161.             TransformInfo info = new TransformInfo();
  162.             info.deltaScale = isScaleEnabled ? detector.getScaleFactor() : 1.0f;
  163.             info.deltaAngle = isRotateEnabled ? Vector2D.getAngle(mPrevSpanVector, detector.getCurrentSpanVector()) : 0.0f;
  164.             info.deltaX = isTranslateEnabled ? detector.getFocusX() - mPivotX : 0.0f;
  165.             info.deltaY = isTranslateEnabled ? detector.getFocusY() - mPivotY : 0.0f;
  166.             info.pivotX = mPivotX;
  167.             info.pivotY = mPivotY;
  168.             info.minimumScale = minimumScale;
  169.             info.maximumScale = maximumScale;
  170.  
  171.             move(view, info);
  172.             return false;
  173.         }
  174.     }
  175.  
  176.     private class TransformInfo {
  177.  
  178.         public float deltaX;
  179.         public float deltaY;
  180.         public float deltaScale;
  181.         public float deltaAngle;
  182.         public float pivotX;
  183.         public float pivotY;
  184.         public float minimumScale;
  185.         public float maximumScale;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment