Android Studio how to continue panning a canvas from a previously panned stateHow to save an Android Activity state using save instance state?How do I call one constructor from another in Java?How to get an enum value from a string value in Java?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I display an alert dialog on Android?How to clear the canvas for redrawingHow do I add a library project to Android Studio?Android Studio: Add jar as library?Undo/Redo not working in android Canvas

What is this little owl-like bird?

Sharing shapefile collection

This one's for Matthew:

OR-backed serious games

Is a request to book a business flight ticket for a graduate student an unreasonable one?

Are there any balance issues in allowing two half-feats to be taken without the Ability Score Increase instead of a feat?

Does throwing a penny at a train stop the train?

Single word for "refusing to move to next activity unless present one is completed."

Why is the air gap between the stator and rotor on a motor kept as small as it is?

Why didn't Thanos kill all the Dwarves on Nidavellir?

How were Martello towers supposed to work?

Why do people keep referring to Leia as Princess Leia, even after the destruction of Alderaan?

Why does wrapping aluminium foil around my food help it keep warm, even though aluminium is a good conductor?

Why doesn't sea level show seasonality?

Misspelling my name on my mathematical publications

What is the measurable difference between dry basil and fresh?

What happens to unproductive professors?

What is a "shilicashe?"

Swapping "Good" and "Bad"

When I press the space bar it deletes the letters after it

When an electron changes its spin, or any other intrinsic property, is it still the same electron?

Received a dinner invitation through my employer's email, is it ok to attend?

What does the phrase "head down the rat's hole" mean here?

Credit score and financing new car



Android Studio how to continue panning a canvas from a previously panned state


How to save an Android Activity state using save instance state?How do I call one constructor from another in Java?How to get an enum value from a string value in Java?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I display an alert dialog on Android?How to clear the canvas for redrawingHow do I add a library project to Android Studio?Android Studio: Add jar as library?Undo/Redo not working in android Canvas






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am having a bit of trouble trying to fix a bug in the zoom and panning operation of my canvas. Currently the zooming and panning functionality does work in that the canvas can be zoomed into or panned across, however the issue is that when the canvas is in a panned state (the image has been panned across) and the canvas invalidates (when the screen is touched again e.g. to continue panning) it snaps back to its original co-ordinates rather than continuing to pan from the previous state. To illustrate:



enter image description here



Let's say this is the image that I am wanting to pan.



enter image description here



This is the image after panning to the left.



enter image description here



This is the result of when I tap the screen again to continue panning to the left, as can be seen it snaps back to the middle whereas I want it to stay at the panning state it was previously so that panning can continue from that point. Below is my code:



private final static float mMinZoom = 1.f;
private final static float mMaxZoom = 3.f;
private float mScaleFactor = 1.f;
private ScaleGestureDetector mScaleGestureDetector;
private final static int NONE = 0;
private final static int PAN = 1;
private final static int ZOOM = 1;
private int mEventState;
private float mStartX = 0;
private float mStartY = 0;
private float mTranslateX = 0;
private float mTranslateY = 0;

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
@Override
public boolean onScale(ScaleGestureDetector detector)
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(mMinZoom, Math.min(mMaxZoom,mScaleFactor));
//invalidate();
//requestLayout();
return super.onScale(detector);



public DrawGraphTest(Context context)
super(context);
init();


public DrawGraphTest(final Context context, final AttributeSet attrs)
super(context, attrs);
init();
mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());


@Override
public boolean onTouchEvent(MotionEvent event)
switch(event.getAction() & MotionEvent.ACTION_MASK)
case MotionEvent.ACTION_DOWN:
mEventState = PAN;
mStartX = event.getX();
mStartY = event.getY();
Log.d("coordinates", String.valueOf(event.getX() / mScaleFactor));
break;
case MotionEvent.ACTION_UP:
mEventState = NONE;
break;
case MotionEvent.ACTION_MOVE:
mTranslateX = event.getX() - mStartX;
mTranslateY = event.getY() - mStartY;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mEventState = ZOOM;
break;

mScaleGestureDetector.onTouchEvent(event);
if((mEventState == PAN && mScaleFactor != mMinZoom)

private void init()
//Various paints and such


@Override
protected void onDraw(Canvas canvas)
super.onDraw(canvas);

canvas.save();
canvas.scale(mScaleFactor,mScaleFactor);
canvas.translate(mTranslateX / mScaleFactor, mTranslateY /mScaleFactor);

//Draw various stuff to canvas
//.....
canvas.restore();



Any help with this would be greatly appreciated as I am very new to using Android Studio, cheers!










share|improve this question




























    0















    I am having a bit of trouble trying to fix a bug in the zoom and panning operation of my canvas. Currently the zooming and panning functionality does work in that the canvas can be zoomed into or panned across, however the issue is that when the canvas is in a panned state (the image has been panned across) and the canvas invalidates (when the screen is touched again e.g. to continue panning) it snaps back to its original co-ordinates rather than continuing to pan from the previous state. To illustrate:



    enter image description here



    Let's say this is the image that I am wanting to pan.



    enter image description here



    This is the image after panning to the left.



    enter image description here



    This is the result of when I tap the screen again to continue panning to the left, as can be seen it snaps back to the middle whereas I want it to stay at the panning state it was previously so that panning can continue from that point. Below is my code:



    private final static float mMinZoom = 1.f;
    private final static float mMaxZoom = 3.f;
    private float mScaleFactor = 1.f;
    private ScaleGestureDetector mScaleGestureDetector;
    private final static int NONE = 0;
    private final static int PAN = 1;
    private final static int ZOOM = 1;
    private int mEventState;
    private float mStartX = 0;
    private float mStartY = 0;
    private float mTranslateX = 0;
    private float mTranslateY = 0;

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
    @Override
    public boolean onScale(ScaleGestureDetector detector)
    mScaleFactor *= detector.getScaleFactor();
    mScaleFactor = Math.max(mMinZoom, Math.min(mMaxZoom,mScaleFactor));
    //invalidate();
    //requestLayout();
    return super.onScale(detector);



    public DrawGraphTest(Context context)
    super(context);
    init();


    public DrawGraphTest(final Context context, final AttributeSet attrs)
    super(context, attrs);
    init();
    mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());


    @Override
    public boolean onTouchEvent(MotionEvent event)
    switch(event.getAction() & MotionEvent.ACTION_MASK)
    case MotionEvent.ACTION_DOWN:
    mEventState = PAN;
    mStartX = event.getX();
    mStartY = event.getY();
    Log.d("coordinates", String.valueOf(event.getX() / mScaleFactor));
    break;
    case MotionEvent.ACTION_UP:
    mEventState = NONE;
    break;
    case MotionEvent.ACTION_MOVE:
    mTranslateX = event.getX() - mStartX;
    mTranslateY = event.getY() - mStartY;
    break;
    case MotionEvent.ACTION_POINTER_DOWN:
    mEventState = ZOOM;
    break;

    mScaleGestureDetector.onTouchEvent(event);
    if((mEventState == PAN && mScaleFactor != mMinZoom)

    private void init()
    //Various paints and such


    @Override
    protected void onDraw(Canvas canvas)
    super.onDraw(canvas);

    canvas.save();
    canvas.scale(mScaleFactor,mScaleFactor);
    canvas.translate(mTranslateX / mScaleFactor, mTranslateY /mScaleFactor);

    //Draw various stuff to canvas
    //.....
    canvas.restore();



    Any help with this would be greatly appreciated as I am very new to using Android Studio, cheers!










    share|improve this question
























      0












      0








      0








      I am having a bit of trouble trying to fix a bug in the zoom and panning operation of my canvas. Currently the zooming and panning functionality does work in that the canvas can be zoomed into or panned across, however the issue is that when the canvas is in a panned state (the image has been panned across) and the canvas invalidates (when the screen is touched again e.g. to continue panning) it snaps back to its original co-ordinates rather than continuing to pan from the previous state. To illustrate:



      enter image description here



      Let's say this is the image that I am wanting to pan.



      enter image description here



      This is the image after panning to the left.



      enter image description here



      This is the result of when I tap the screen again to continue panning to the left, as can be seen it snaps back to the middle whereas I want it to stay at the panning state it was previously so that panning can continue from that point. Below is my code:



      private final static float mMinZoom = 1.f;
      private final static float mMaxZoom = 3.f;
      private float mScaleFactor = 1.f;
      private ScaleGestureDetector mScaleGestureDetector;
      private final static int NONE = 0;
      private final static int PAN = 1;
      private final static int ZOOM = 1;
      private int mEventState;
      private float mStartX = 0;
      private float mStartY = 0;
      private float mTranslateX = 0;
      private float mTranslateY = 0;

      private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
      @Override
      public boolean onScale(ScaleGestureDetector detector)
      mScaleFactor *= detector.getScaleFactor();
      mScaleFactor = Math.max(mMinZoom, Math.min(mMaxZoom,mScaleFactor));
      //invalidate();
      //requestLayout();
      return super.onScale(detector);



      public DrawGraphTest(Context context)
      super(context);
      init();


      public DrawGraphTest(final Context context, final AttributeSet attrs)
      super(context, attrs);
      init();
      mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());


      @Override
      public boolean onTouchEvent(MotionEvent event)
      switch(event.getAction() & MotionEvent.ACTION_MASK)
      case MotionEvent.ACTION_DOWN:
      mEventState = PAN;
      mStartX = event.getX();
      mStartY = event.getY();
      Log.d("coordinates", String.valueOf(event.getX() / mScaleFactor));
      break;
      case MotionEvent.ACTION_UP:
      mEventState = NONE;
      break;
      case MotionEvent.ACTION_MOVE:
      mTranslateX = event.getX() - mStartX;
      mTranslateY = event.getY() - mStartY;
      break;
      case MotionEvent.ACTION_POINTER_DOWN:
      mEventState = ZOOM;
      break;

      mScaleGestureDetector.onTouchEvent(event);
      if((mEventState == PAN && mScaleFactor != mMinZoom)

      private void init()
      //Various paints and such


      @Override
      protected void onDraw(Canvas canvas)
      super.onDraw(canvas);

      canvas.save();
      canvas.scale(mScaleFactor,mScaleFactor);
      canvas.translate(mTranslateX / mScaleFactor, mTranslateY /mScaleFactor);

      //Draw various stuff to canvas
      //.....
      canvas.restore();



      Any help with this would be greatly appreciated as I am very new to using Android Studio, cheers!










      share|improve this question














      I am having a bit of trouble trying to fix a bug in the zoom and panning operation of my canvas. Currently the zooming and panning functionality does work in that the canvas can be zoomed into or panned across, however the issue is that when the canvas is in a panned state (the image has been panned across) and the canvas invalidates (when the screen is touched again e.g. to continue panning) it snaps back to its original co-ordinates rather than continuing to pan from the previous state. To illustrate:



      enter image description here



      Let's say this is the image that I am wanting to pan.



      enter image description here



      This is the image after panning to the left.



      enter image description here



      This is the result of when I tap the screen again to continue panning to the left, as can be seen it snaps back to the middle whereas I want it to stay at the panning state it was previously so that panning can continue from that point. Below is my code:



      private final static float mMinZoom = 1.f;
      private final static float mMaxZoom = 3.f;
      private float mScaleFactor = 1.f;
      private ScaleGestureDetector mScaleGestureDetector;
      private final static int NONE = 0;
      private final static int PAN = 1;
      private final static int ZOOM = 1;
      private int mEventState;
      private float mStartX = 0;
      private float mStartY = 0;
      private float mTranslateX = 0;
      private float mTranslateY = 0;

      private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
      @Override
      public boolean onScale(ScaleGestureDetector detector)
      mScaleFactor *= detector.getScaleFactor();
      mScaleFactor = Math.max(mMinZoom, Math.min(mMaxZoom,mScaleFactor));
      //invalidate();
      //requestLayout();
      return super.onScale(detector);



      public DrawGraphTest(Context context)
      super(context);
      init();


      public DrawGraphTest(final Context context, final AttributeSet attrs)
      super(context, attrs);
      init();
      mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());


      @Override
      public boolean onTouchEvent(MotionEvent event)
      switch(event.getAction() & MotionEvent.ACTION_MASK)
      case MotionEvent.ACTION_DOWN:
      mEventState = PAN;
      mStartX = event.getX();
      mStartY = event.getY();
      Log.d("coordinates", String.valueOf(event.getX() / mScaleFactor));
      break;
      case MotionEvent.ACTION_UP:
      mEventState = NONE;
      break;
      case MotionEvent.ACTION_MOVE:
      mTranslateX = event.getX() - mStartX;
      mTranslateY = event.getY() - mStartY;
      break;
      case MotionEvent.ACTION_POINTER_DOWN:
      mEventState = ZOOM;
      break;

      mScaleGestureDetector.onTouchEvent(event);
      if((mEventState == PAN && mScaleFactor != mMinZoom)

      private void init()
      //Various paints and such


      @Override
      protected void onDraw(Canvas canvas)
      super.onDraw(canvas);

      canvas.save();
      canvas.scale(mScaleFactor,mScaleFactor);
      canvas.translate(mTranslateX / mScaleFactor, mTranslateY /mScaleFactor);

      //Draw various stuff to canvas
      //.....
      canvas.restore();



      Any help with this would be greatly appreciated as I am very new to using Android Studio, cheers!







      java android canvas zoom panning






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 1:28









      SeanSean

      821 silver badge10 bronze badges




      821 silver badge10 bronze badges






















          0






          active

          oldest

          votes










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348614%2fandroid-studio-how-to-continue-panning-a-canvas-from-a-previously-panned-state%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes




          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348614%2fandroid-studio-how-to-continue-panning-a-canvas-from-a-previously-panned-state%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript