Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:37:38+00:00 2026-05-27T01:37:38+00:00

Please see each section below for a description of my problem described in three

  • 0

Please see each section below for a description of my problem described in three separate ways. Hopefully should help people to answer.

Problem: How do you find a pair of coordinate expressed in canvas/userspace when you only have the coordinate expressed in terms of a zoomed image, given the original scale point & scale factor?

Problem in practice:

I’m currently trying to replicate the zoom functionality used in apps such as the gallery / maps, when you can pinch to zoom/zoom out with the zoom moving towards the midpoint of the pinch.

On down I save the centre point of the zoom (which is in X,Y coordinates based on the current screen). I then have this function act when a “scale” gesture is detected:

class ImageScaleGestureDetector extends SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        if(mScaleAllowed) 
            mCustomImageView.scale(detector.getScaleFactor(), mCenterX, mCenterY);
        return true;
    }
}   

The scale function of the CustomImageView look like this:

public boolean scale(float scaleFactor, float focusX, float focusY) {
    mScaleFactor *= scaleFactor;

    // Don't let the object get too small or too large.
    mScaleFactor = Math.max(MINIMUM_SCALE_VALUE, Math.min(mScaleFactor, 5.0f));

    mCenterScaleX = focusX;
    mCenterScaleY = focusY;

    invalidate();

    return true;
}

The drawing of the scaled image is achieved by overriding the onDraw method which scales the canvas around the centre ands draw’s the image to it.

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.translate(mCenterScaleX, mCenterScaleY);
    canvas.scale(mScaleFactor, mScaleFactor);
    canvas.translate(-mCenterScaleX, -mCenterScaleY);
    mIcon.draw(canvas);
    canvas.restore();
}

This all works fine when scaling from ScaleFactor 1, this is because the initial mCenterX and mCenterY are coordinates which are based on the device screen. 10, 10 on the device is 10, 10 on the canvas.

After you have already zoomed however, then next time you click position 10, 10 it will no longer correspond to 10, 10 in the canvas because of the scaling & transforming that has already been performed.

Problem in abstraction:

The image below is an example of a zoom operation around centre point A. Each box represents the position and size of the view when at that scale factor (1, 2, 3, 4, 5).

Example

In the example if you scaled by a factor of 2 around A then you clicked on position B, the X, Y reported as B would be based on the screen position – not on the position relative to 0,0 of the initial canvas.

I need to find a way of getting the absolute position of B.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:37:39+00:00Added an answer on May 27, 2026 at 1:37 am

    So, after redrawing the problem I’ve found the solution I was looking for. It’s gone through a few iteration’s but here’s how I worked it out:

    Solution Description

    B – Point, Center of the scale operation

    A1, A2, A3 – Points, equal in user-space but different in canvas-space.

    You know the values for Bx and By because they are always constant no matter what the scale factor (You know this value in both canvas-space and in user-space).

    You know Ax & Ay in user-space so you can find the distance between Ax to Bx and Ay to By. This measurement is in user-space, to convert it to a canvas-space measurement simply divide it by the scale factor. (Once converted to canvas-space, you can see these lines in red, orange and yellow).

    As point B is constant, the distance between it and the edges are constant (These are represented by Blue Lines). This value is equal in user-space and canvas-space.

    You know the width of the Canvas in canvas-space so by subtracting these two canvas space measurements (Ax to Bx and Bx to Edge) from the total width you are left with the coordinates for point A in canvas-space:

    public float[] getAbsolutePosition(float Ax, float Ay) {
        
        float fromAxToBxInCanvasSpace = (mCenterScaleX - Ax) / mScaleFactor;
        float fromBxToCanvasEdge = mCanvasWidth - Bx;
        float x = mCanvasWidth - fromAxToBxInCanvasSpace - fromBxToCanvasEdge;
        
        float fromAyToByInCanvasSpace = (mCenterScaleY - Ay) / mScaleFactor;
        float fromByToCanvasEdge = mCanvasHeight - By;
        float y = mCanvasHeight - fromAyToByInCanvasSpace - fromByToCanvasEdge;
        
        return new float[] { x, y };
    }
    

    The above code and image describe when you’re clicking to the top left of the original centre. I used the same logic to find A no matter which quadrant it was located in and refactored to the following:

    public float[] getAbsolutePosition(float Ax, float Ay) {
    
        float x = getAbsolutePosition(mBx, Ax);
        float y = getAbsolutePosition(mBy, Ay); 
    
        return new float[] { x, y };
    }
    
    private float getAbsolutePosition(float oldCenter, float newCenter, float mScaleFactor) {
        if(newCenter > oldCenter) {
            return oldCenter + ((newCenter - oldCenter) / mScaleFactor);
        } else {
            return oldCenter - ((oldCenter - newCenter) / mScaleFactor);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display some text below each grid image. Please see my code
I am having problem with Object for..each loop Please see Snap. I am getting
Please see code below. The destructors are never called. Anyone know why and how
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
everyone. Please see example below. I'd like to supply a string to 'schedule_action' method
I have an Adorner which adornes a Border (please see screenshot below). The MouseDown
I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int,
Please see my code below. ifstream myLibFile (libs//%s , line); // Compile failed here
I'm trying to create a dynamic ItemizedOverylay (please see the code below) on Google
UPDATE: Please see the updates below, as I'm adding more information to the bottom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.