android - Whenever I try to rotate imageview, it only rotates on axis -
so have been attempting create program can drag, zoom , rotate photo. big problem seem running whenever try rotate photo, rotates along corner, rather around center. means try rotate image, leaves fingers.
another big problem have every time touch 2 fingers, image resets being upright, instead of angle held when touched it.
@override protected void ondraw(canvas canvas) { canvas.save(); //if(mode==drag) canvas.translate(mposx, mposy); if (myscale.isinprogress()) { canvas.scale(mscalefactor, mscalefactor, myscale.getfocusx(), myscale.getfocusy()); } else{ canvas.scale(mscalefactor, mscalefactor, mlastgesturex, mlastgesturey); } if (myscale.isinprogress()) { canvas.rotate(degrees, myscale.getfocusx(), myscale.getfocusy()); } else{ canvas.rotate(degrees, mlastgesturex, mlastgesturey); } //canvas.setmatrix(matrix); //setimagematrix(matrix); super.ondraw(canvas); canvas.restore(); //canvas.drawbitmap(,matrix,new paint()); } @override public boolean ontouchevent(motionevent event) { super.ontouchevent(event); if(event.getpointercount()>1){ myscale.ontouchevent(event); } switch (event.getaction() & motionevent.action_mask){ case motionevent.action_down: { savedmatrix.set(matrix); final float x = event.getx(); final float y = event.gety(); mode=drag; // remember started mlasttouchx = x; mlasttouchy = y; mactivepointerid = event.getpointerid(0); lastevent = null; break; } case motionevent.action_pointer_down: { olddist = spacing(event); //savedmatrix.set(matrix); //midpoint(mid, event); log.d("touchresponse: ", "mode=zoom"); final float gx = myscale.getfocusx(); final float gy = myscale.getfocusy(); mlastgesturex=gx; mlastgesturey=gy; mode=zoom; lastevent = new float[4]; lastevent[0] = event.getx(0); lastevent[1] = event.getx(1); lastevent[2] = event.gety(0); lastevent[3] = event.gety(1); d = rotation(event); 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()&&mode==drag){ // move object float dx = x-mlasttouchx; float dy = y-mlasttouchy; mposx+=dx; mposy+=dy; // remember touch position next move event // invalidate request redraw invalidate(); } mlasttouchx = x; mlasttouchy = y; if(event.getpointercount()==2){ if (lastevent!=null){ newrot=rotation(event); degrees = newrot-d; } } invalidate(); break; } case motionevent.action_up: { } case motionevent.action_pointer_up: { // extract index of pointer left touch sensor\ mode=none; mode = none; lastevent = null; final int pointerindex = (event.getaction() & motionevent.action_pointer_index_mask) >> motionevent.action_pointer_index_shift; final int pointerid = event.getpointerid(pointerindex); if (pointerid == mactivepointerid) { // our active pointer going up. choose new // active pointer , adjust accordingly. final int newpointerindex = pointerindex == 0 ? 1 : 0; mlasttouchx = event.getx(newpointerindex); mlasttouchy = event.gety(newpointerindex); mactivepointerid = event.getpointerid(newpointerindex); } invalidate(); break; } } return true; } //this method ripped tutoriaql 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)); //matrix=temp; invalidate(); return true; } } private float rotation(motionevent event) { double delta_x = (event.getx(0) - event.getx(1)); double delta_y = (event.gety(0) - event.gety(1)); double radians = math.atan2(delta_y, delta_x); //if (constant.trace) log.d("rotation ~~~~~~~~~~~~~~~~~", delta_x+" ## "+delta_y+" ## "+radians+" ## " // +math.todegrees(radians)); log.d("rotation ~~~~~~~~~~~~~~~~~", delta_x+" ## "+delta_y+" ## "+radians+" ## " +math.todegrees(radians)); return (float) math.todegrees(radians); }
if want rotate around center, need translate center first. set new origin center of image, , rotate rotates around origin.
Comments
Post a Comment