Note that there are some explanatory texts on larger screens.

plurals
  1. POANDROID - MapView
    text
    copied!<p>Can some one please help me few things regarding mapview im trying to build. I have a renderer that takes canvas and draws a bitmap in it. I have created a view and in constructor i create:</p> <pre><code>bitmap = Bitmap.createBitmap(windowWidth, windowHeight, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); drawable = new BitmapDrawable(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); this.Render(); </code></pre> <p>In onDraw method i have this:</p> <pre><code>super.onDraw(canvas); canvas.save(); canvas.translate(mPosX, mPosY); canvas.scale(mScaleFactor, mScaleFactor); drawable.draw(canvas); canvas.restore(); </code></pre> <p>Now i would like to implement pan and zoom (currently im working without saved tiles) Here is my onTouch:</p> <p>mScaleDetector.onTouchEvent(ev);</p> <pre><code>final int action = ev.getAction(); switch (action &amp; MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { this.hasPanned = false; final float x = ev.getX(); final float y = ev.getY(); mLastTouchX = x; mLastTouchY = y; mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); if (!mScaleDetector.isInProgress()) { final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; mPosX += dx; mPosY += dy; invalidate(); } mLastTouchX = x; mLastTouchY = y; break; } case MotionEvent.ACTION_UP: { mActivePointerId = INVALID_POINTER_ID; if (this.mDownTouchX != this.mLastTouchX &amp;&amp; this.mDownTouchY != mLastTouchY) this.hasPanned = true; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = (ev.getAction() &amp; MotionEvent.ACTION_POINTER_INDEX_MASK) &gt;&gt; MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = ev.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = ev.getX(newPointerIndex); mLastTouchY = ev.getY(newPointerIndex); mActivePointerId = ev.getPointerId(newPointerIndex); } break; } } return true; </code></pre> <p>Here is what i would like and problems i have:</p> <ol> <li>When i pan i would like to calculate distance in pixels so i can tell my renderer when it renders the image again. How is this achieved? By remembering coordinates on ACTION_DOWN and comparing with new coordinates in ACTION_UP ?</li> <li>When pan is complete (and image is re-rendered) i would like to set the canvas(image) back to its original position. Currently it stays paned (because of canvas.translate?)</li> <li>How do i handle zoom gestures? With this code when doing a zoom-out with my fingers the image is "dragged" towards top left corner ...</li> </ol> <p>Im new in android (and java) so any help is preciated.</p>
 

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