Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid image transformation with matrix, translate touch coordinates back
    primarykey
    data
    text
    <p>I'm building a "navigation type" app for android.</p> <p>For the navigation part I'm building an Activity where the user can move and zoom the map (which is a bitmap) using touch events, and also the map rotate around the center of the screen using the compass.</p> <p>I'm using Matrix to scale, transpose and rotate the image, and than I draw it to the canvas.</p> <p>Here is the code called on loading of the view, to center the image in the screen:</p> <pre><code> image = new Matrix(); image.setScale(zoom, zoom); image_center = new PointF(bmp.getWidth() / 2, bmp.getHeight() / 2); float centerScaledWidth = image_center.x * zoom; float centerScaledHeigth = image_center.y * zoom; image.postTranslate( screen_center.x - centerScaledWidth, screen_center.y - centerScaledHeigth); </code></pre> <p>The rotation of the image is doing using the <em>postRotate</em> method.</p> <p>Then in the <em>onDraw()</em> method I only call</p> <pre><code> canvas.drawBitmap(bmp, image, drawPaint); </code></pre> <p>The problem is that, when the user touch the screen, I want to get the point touched on the image, but apparently I can't get the correct position. I tried to invert the <strong>image</strong> matrix and translate the touched points, it isn't working.</p> <p>Do somebody know how to translate the point coordinates?</p> <p><strong>EDIT</strong></p> <p>I'm using this code for traslation. <em>dx</em> and <em>dy</em> are translation values get from the <em>onTouch</em> listener. *new_center* is an array of float values in this form {x0, y0, x1, y1...}</p> <pre><code> Matrix translated = new Matrix(); Matrix inverted = new Matrix(); translated.set(image); translated.postTranslate(dx, dy); translated.invert(inverted); inverted.mapPoints(new_center); translated.mapPoints(new_center); Log.i("new_center", new_center[0]+" "+new_center[1]); </code></pre> <p>Actually I tried using as *new_center = {0,0}*:</p> <p>appling only the <em>translated</em> matrix, I get as espected the distance between the (0,0) point of the bmp and the (0,0) point of the screen, but it seems to not take account of the rotation.</p> <p>Appling the <em>inverted</em> matrix to the points I get those results, moving the image in every possible way.</p> <pre><code> 12-26 13:26:08.481: I/new_center(11537): 1.9073486E-6 -1.4901161E-7 12-26 13:26:08.581: I/new_center(11537): 0.0 -3.874302E-7 12-26 13:26:08.631: I/new_center(11537): 1.9073486E-6 1.2516975E-6 12-26 13:26:08.781: I/new_center(11537): -1.9073486E-6 -5.364418E-7 12-26 13:26:08.951: I/new_center(11537): 0.0 2.682209E-7 12-26 13:26:09.093: I/new_center(11537): 0.0 7.003546E-7 </code></pre> <p>Instead I was especting the coordinates translated on the image.</p> <p>Is it correct my line of thoughts?</p>
    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.
 

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