Note that there are some explanatory texts on larger screens.

plurals
  1. POSet zoom ratio of a displayed image in an android canvas
    primarykey
    data
    text
    <p>I am trying to write an android app that lets me draw graphics on top of an image then scale and zoom the image with the graphics staying over the same place on the image that have been drawn on top of it while changing the graphics in real time. </p> <p>However I have been having a lot of issues actually getting it to zoom in while maintaining the center of the image. I have written code where I have a thread that updates the image. Updates are passed in using a class that I created called "PendingUpdate" through an ArrayBlockingQueue. This update contains a desired zoom level which is supposed to be the ratio of the image pixels to the canvas pixels and an image center. However the following code makes it pan while I am zooming which confuses me.</p> <pre><code>//Scale the image canvas.scale(pendingUpdate.getZoom(), pendingUpdate.getZoom()); //Translate the image double updateCx = pendingUpdate.getCenter().getX(); double updateCy = pendingUpdate.getCenter().getY(); double halfCanvasWidthInImagePixels = pendingUpdate.getZoom()*(canvas.getWidth()/2); double halfCanvasHeightInImagePixels = pendingUpdate.getZoom()*(canvas.getHeight()/2); double imageTranslateX = updateCx - halfCanvasWidthInImagePixels; double imageTranslateY = updateCy - halfCanvasHeightInImagePixels; canvas.translate(-(float)imageTranslateX, -(float)imageTranslateY); canvas.drawBitmap(pendingUpdate.getImage(), matrix, new Paint()); </code></pre> <p>Thank you for the help!</p> <p>Edit: here is the full function, I can also post PendingUpdate if this helps, however its just a data class.</p> <pre><code> private void doDraw(Canvas canvas, PendingUpdate pendingUpdate) { int iWidth = pendingUpdate.getImage().getWidth(); int iHeight = pendingUpdate.getImage().getHeight(); Matrix matrix = new Matrix(); canvas.drawColor(Color.BLACK); //TODO: add scrolling functionality to this if(pendingUpdate.getZoom()&gt;0) { //Scale the image canvas.scale(pendingUpdate.getZoom(), pendingUpdate.getZoom()); //Translate the image double updateCx = pendingUpdate.getCenter().getX(); double updateCy = pendingUpdate.getCenter().getY(); double halfCanvasWidthInImagePixels = pendingUpdate.getZoom()*(canvas.getWidth()/2); double halfCanvasHeightInImagePixels = pendingUpdate.getZoom()*(canvas.getHeight()/2); double imageTranslateX = updateCx - halfCanvasWidthInImagePixels; double imageTranslateY = updateCy - halfCanvasHeightInImagePixels; canvas.translate(-(float)imageTranslateX, -(float)imageTranslateY); canvas.drawBitmap(pendingUpdate.getImage(), matrix, new Paint()); }else { //matrix.postTranslate(canvas.getWidth()-iWidth/2, canvas.getWidth()-iHeight/2); canvas.drawBitmap(pendingUpdate.getImage(), (canvas.getWidth()-iWidth)/2, (canvas.getHeight()-iHeight)/2, null); } //TODO: draw other stuff on canvas here such as current location } </code></pre> <p>edit 2: This is how I finally got it to work, it was simply a matter of scaling it before translating it.</p> <pre><code> private void doDraw(Canvas canvas, PendingUpdate pendingUpdate) { int iWidth = pendingUpdate.getImage().getWidth(); int iHeight = pendingUpdate.getImage().getHeight(); canvas.drawColor(Color.BLACK); //TODO: add scrolling functionality to this if(pendingUpdate.getZoom()&gt;0) { //Scale the image canvas.save(); double updateCx = pendingUpdate.getCenter().getX(); double updateCy = pendingUpdate.getCenter().getY(); double halfCanvasWidthInImagePixels = (canvas.getWidth()/2); double halfCanvasHeightInImagePixels = (canvas.getHeight()/2); double imageTranslateX = updateCx - halfCanvasWidthInImagePixels; double imageTranslateY = updateCy - halfCanvasHeightInImagePixels; //canvas.scale(pendingUpdate.getZoom(), pendingUpdate.getZoom(), (float)pendingUpdate.getCenter().getX(), (float)pendingUpdate.getCenter().getY()); canvas.scale(pendingUpdate.getZoom(), pendingUpdate.getZoom(), canvas.getWidth()/2, canvas.getHeight()/2); canvas.translate(-(float)imageTranslateX, -(float)imageTranslateY); canvas.drawBitmap(pendingUpdate.getImage(), 0, 0, null); canvas.restore(); }else { //TODO: update this so it displays image scaled to screen and updates current zoom somehow canvas.drawBitmap(pendingUpdate.getImage(), (canvas.getWidth()-iWidth)/2, (canvas.getHeight()-iHeight)/2, null); } //TODO: draw other stuff on canvas here such as current location } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload