Vote utilisateur: 0 / 5

Etoiles inactivesEtoiles inactivesEtoiles inactivesEtoiles inactivesEtoiles inactives

Bismillah,

lien vers google play

CODE SNIPET : zooming and dragging in a custom view.

C'est la classe que j'utilise dans mon code android studio pour zoomer et déplacer le dessin. Le zoom se fait à l'endroit du toucher.

PS : cette classe n'est pas de moi !

 

public class FingerMotionTracker {

 View mView;

 public FingerMotionTracker(Context context, View v,
          float minScaleFactor,
          float maxScaleFactor,
          boolean allowDragHorizontal,
          boolean allowDragVertical,
          boolean centerAndScaleOneOnDoubleTap) {
  mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
  mGestureDetector = new GestureDetector(context, new GestureListener());
  mView = v;

  mMinScaleFactor = minScaleFactor;
  mMaxScaleFactor = maxScaleFactor;

  mAllowDragHorizontal = allowDragHorizontal;
  mAllowDragVertical = allowDragVertical;
  mCenterAndScaleOneOnDoubleTap = centerAndScaleOneOnDoubleTap;

  reset();
 }

 boolean mAllowDragHorizontal;
 boolean mAllowDragVertical;
 boolean mCenterAndScaleOneOnDoubleTap;

 float mMinScaleFactor;
 float mMaxScaleFactor;

 public VelocityTracker mVelocityTracker;
 public int mVelocityTrackerCnt = 0;
 public GestureDetector mGestureDetector;
 public ScaleGestureDetector mScaleDetector;
 public float mScaleFactor;

 //These two variables keep track of the amount we need to translate the canvas along the X
 //and the Y coordinate
 public float mTranslateX;
 public float mTranslateY;

 public float mPreviousScaleFactor;

 void reset() {

  mScaleFactor = 1.f;

  mTranslateX = 0f;
  mTranslateY = 0f;
 }

 public boolean onTouchEvent(MotionEvent event) {

  mScaleDetector.onTouchEvent(event);
  mGestureDetector.onTouchEvent(event);

  return true;
 }

 void correctTranslateX() {
  if (mScaleFactor < 1f) {
   if (mTranslateX < 0) {
    mTranslateX = 0;
   }
   if (mTranslateX > -1 * mView.getMeasuredWidth() * (mScaleFactor - 1)) {
    mTranslateX = -1 * mView.getMeasuredWidth() * (mScaleFactor - 1);
   }
  } else if (mScaleFactor > 1f) {
   if (mTranslateX > 0) {
    mTranslateX = 0;
   }
   if (mTranslateX <= -1 * mView.getMeasuredWidth() * (mScaleFactor - 1)) {
    mTranslateX = -1 * mView.getMeasuredWidth() * (mScaleFactor - 1);
   }
  } else {
   mTranslateX = 0;

  }
 }

 void correctTranslateY() {
  if (mScaleFactor < 1f) {
   if (mTranslateY < 0) {
    mTranslateY = 0;
   }
   if (mTranslateY > -1 * mView.getMeasuredHeight() * (mScaleFactor - 1)) {
    mTranslateY = -1 * mView.getMeasuredHeight() * (mScaleFactor - 1);
   }
  } else if (mScaleFactor > 1f) {
   if (mTranslateY > 0) {
    mTranslateY = 0;
   }
   if (mTranslateY <= -1 * mView.getMeasuredHeight() * (mScaleFactor - 1)) {
    mTranslateY = -1 * mView.getMeasuredHeight() * (mScaleFactor - 1);
   }
  } else {
   mTranslateY = 0;

  }
 }

 private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  @Override
  public boolean onScale(ScaleGestureDetector detector) {
   mPreviousScaleFactor = mScaleFactor;

   float newScaleFactor = mScaleFactor * detector.getScaleFactor();

   // Don't let the object get too small or too large.
   newScaleFactor = Math.max(mMinScaleFactor, Math.min(newScaleFactor, mMaxScaleFactor));

   if (Math.abs(mPreviousScaleFactor - newScaleFactor) < 0.000000001) {
    return super.onScale(detector);
   }

   mScaleFactor = newScaleFactor;

   // This is to move X/Y with zooming so that the zoom center is where the 2
   // fingers are and not on the left/top of the screen
   mTranslateX = mTranslateX - (mScaleFactor * detector.getFocusX() - mPreviousScaleFactor *  detector.getFocusX());
   mTranslateY = mTranslateY - (mScaleFactor * detector.getFocusY() - mPreviousScaleFactor *  detector.getFocusY());

   correctTranslateX();
   correctTranslateY();
   mView.invalidate();
   return true;
  }
 }

 private class GestureListener extends GestureDetector.SimpleOnGestureListener {


  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

     // System.out.println("onFling");
     // System.out.println("- xvel:" + velocityX);
     // System.out.println("- e1.getX():" + e1.getX());
     // System.out.println("- e2.getX():" + e2.getX());


     if (mAllowDragHorizontal) {
      mTranslateX = mTranslateX - Math.abs(velocityX) / 1000f * (e1.getX() - e2.getX());
     }
     if (mAllowDragVertical) {
      mTranslateY = mTranslateY - Math.abs(velocityY) / 1000f * (e1.getY() - e2.getY());
     }

     correctTranslateX();
     correctTranslateY();

     mView.invalidate();

     // System.out.println("mVelocityTracker.obtain");
     mVelocityTracker = VelocityTracker.obtain();
     ++mVelocityTrackerCnt;
     mVelocityTracker.addMovement(e1);
     mVelocityTracker.addMovement(e2);

   return true;
  }

  @Override
  public boolean onDoubleTap(MotionEvent event) {

   if (mCenterAndScaleOneOnDoubleTap) {
    reset();
    mView.invalidate();
    return true;
   } else {
    return false;
   }
  }

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2,
        float distanceX, float distanceY) {

   // System.out.println("onScroll distanceX " + distanceX);

   if (mAllowDragHorizontal) {
    mTranslateX -= distanceX;
   }
   if (mAllowDragVertical) {
    mTranslateY -= distanceY;
   }

   correctTranslateX();
   correctTranslateY();

   mView.invalidate();

   return true;
  }
 }
}

 

 

Vous pouvez poster des commentaires pour signaler des bugs, des erreurs, améliorations, témoignages, ...