bitmap - ANDROID - MapView -


can 1 please me few things regarding mapview im trying build. have renderer takes canvas , draws bitmap in it. have created view , in constructor create:

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(); 

in ondraw method have this:

super.ondraw(canvas); canvas.save(); canvas.translate(mposx, mposy); canvas.scale(mscalefactor, mscalefactor); drawable.draw(canvas); canvas.restore(); 

now implement pan , zoom (currently im working without saved tiles) here ontouch:

mscaledetector.ontouchevent(ev);

final int action = ev.getaction(); switch (action & 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 && 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() & motionevent.action_pointer_index_mask)              >> 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; 

here , problems have:

  1. when pan calculate distance in pixels can tell renderer when renders image again. how achieved? remembering coordinates on action_down , comparing new coordinates in action_up ?
  2. when pan complete (and image re-rendered) set canvas(image) original position. stays paned (because of canvas.translate?)
  3. how handle zoom gestures? code when doing zoom-out fingers image "dragged" towards top left corner ...

im new in android (and java) preciated.

if zoom code zooms canvas doesn't move correctly, suspect calling wrong canvas.scale() method. method using seems -

scale(float sx, float sy) 

and think need use -

scale(float sx, float sy, float px, float py) 

sx & sy x & y scale factors (i set them same value). px & py centre points zoom operation, e.g. mid point between motionevent.action_down , motionevent.action_pointer_down event coordinates.

in motionevent.action_pointer_down handler call -

midpoint(mid, event); 

mid defined -

private pointf mid = new pointf(); 

and midpoint function -

private void midpoint(pointf point, motionevent event)  {    float x = event.getx(0) + event.getx(1);    float y = event.gety(0) + event.gety(1);    point.set(x / 2, y / 2); } 

so in motionevent.action_move handler call -

canvas.scale(sx, sy, mid.x, mid.y); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -