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

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현