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;
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:
Let's say this is the image that I am wanting to pan.
This is the image after panning to the left.
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
add a comment |
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:
Let's say this is the image that I am wanting to pan.
This is the image after panning to the left.
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
add a comment |
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:
Let's say this is the image that I am wanting to pan.
This is the image after panning to the left.
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
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:
Let's say this is the image that I am wanting to pan.
This is the image after panning to the left.
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
java android canvas zoom panning
asked Mar 26 at 1:28
SeanSean
821 silver badge10 bronze badges
821 silver badge10 bronze badges
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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