android - When Zooming an ImageView the image scales to the corner instead to center -


so i'm running error image zoom, instead of zooming midpoint of 2 fingers, zoom corner of original picture. creates unnatural user interface.

@override protected void ondraw(canvas canvas) {      canvas.save();     canvas.translate(mposx, mposy);     canvas.scale(mscalefactor, mscalefactor);     super.ondraw(canvas);     canvas.restore(); } 

with ontouchevent, myscale.ontouch send program scalelistener scaling information

@override public boolean ontouchevent(motionevent event) {     super.ontouchevent(event);     midpoint(mid,event);     myscale.ontouchevent(event);     switch (event.getaction() & motionevent.action_mask){             case motionevent.action_down: {                 final float x = event.getx();                 final float y = event.gety();                 mode=drag;                 // remember started                 mlasttouchx = x;                 mlasttouchy = y;                 mactivepointerid = event.getpointerid(0);                 break;             }         case motionevent.action_pointer_down: {            final float gx = myscale.getfocusx();             final float gy = myscale.getfocusy();             mlastgesturex=gx;             mlastgesturey=gy;             mode=zoom;             break;         }               case motionevent.action_move: {                 final int pointerindex = event.findpointerindex(mactivepointerid);                 final float x = event.getx(pointerindex);                 final float y = event.gety(pointerindex);                  // calculate distance moved              if(!myscale.isinprogress()){                     // move object                      final float dx = x - mlasttouchx;                     final float dy = y - mlasttouchy;                     mposx += dx;                     mposy += dy;                      // remember touch position next move event                       // invalidate request redraw                     invalidate();              }                 mlasttouchx = x;                 mlasttouchy = y;                  break;              }  have deleted action , action pointer because have nothing problems              }      invalidate();     return true; }       private float spacing(motionevent event) {     float x = event.getx(0) - event.getx(1);     float y = event.gety(0) - event.gety(1);     return floatmath.sqrt(x * x + y * y); }     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); }  private class scalelistener extends scalegesturedetector.simpleonscalegesturelistener {     @override     public boolean onscale(scalegesturedetector detector) {          mscalefactor *= detector.getscalefactor();          // don't let object small or large.         mscalefactor = math.max(0.1f, math.min(mscalefactor, 10.0f));         float currentdist=detector.getcurrentspan();         float last=detector.getpreviousspan();         float ratio = currentdist/last;          mscalefactor = math.max(0.1f, math.min(mscalefactor, 5.0f));          matrix temp = new matrix();         temp.postscale(ratio, ratio, mid.x,mid.y);         temp.preconcat(matrix);          matrix=temp;          invalidate();         return true;     } } 

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

http://developer.android.com/reference/android/graphics/canvas.html#scale%28float,%20float,%20float,%20float%29


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -