image - Android Zoomview center -
i tried create zoom view image scaled center. however, if change initial position of image , pinch image moves lower right corner of window. reference used pinch zoom view of android developer , changed parameters. have ideas fix that?
package android.oli.com.fitnessapp; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect; import android.graphics.drawable.drawable; import android.util.attributeset; import android.view.motionevent; import android.view.scalegesturedetector; import android.view.view; public class zoomview extends view{ private drawable micon; private float mposx; private float mposy; private float mlasttouchx; private float mlasttouchy; private scalegesturedetector mscaledetector; private float mscalefactor = 1.f; private static final int invalid_pointer_id = -1; // ‘active pointer’ 1 moving our object. private int mactivepointerid = invalid_pointer_id; context context; public zoomview(context context) { super(context); micon = context.getresources().getdrawable(r.drawable.image_1); this.context = context; init(); } public zoomview(context context, attributeset attrs) { super(context, attrs); micon = context.getresources().getdrawable(r.drawable.image_1); this.context = context; init(); } public zoomview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); micon = context.getresources().getdrawable(r.drawable.image_1); this.context = context; init(); } public void init(){ micon.setbounds(0, 0, micon.getintrinsicwidth(), micon.getintrinsicheight()); mscaledetector = new scalegesturedetector(context, new scalelistener()); } @override public void ondraw(canvas canvas) { super.ondraw(canvas); canvas.save(); canvas.translate(mposx - ((micon.getintrinsicwidth()) * mscalefactor) / 2, mposy - ((micon.getintrinsicwidth()) * mscalefactor) / 2); canvas.scale(mscalefactor, mscalefactor); micon.draw(canvas); //rectmid.set(0, 0, canvas.getwidth(), canvas.getheight()/2); //canvas.drawrect(rectmid, blue); canvas.restore(); } @override public boolean ontouchevent(motionevent ev) { // let scalegesturedetector inspect events. mscaledetector.ontouchevent(ev); final int action = ev.getaction(); switch (action & motionevent.action_mask) { case motionevent.action_down: { 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); // move if scalegesturedetector isn't processing gesture. 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; 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) { // our active pointer going up. choose new // active pointer , adjust accordingly. final int newpointerindex = pointerindex == 0 ? 1 : 0; mlasttouchx = ev.getx(newpointerindex); mlasttouchy = ev.gety(newpointerindex); mactivepointerid = ev.getpointerid(newpointerindex); } break; } } return true; } 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, 5.0f)); invalidate(); return true; } } }
xml:
<android.oli.com.fitnessapp.zoomview android:layout_width="match_parent" android:layout_height="match_parent" />
ok got :d
import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect; import android.graphics.drawable.drawable; import android.util.attributeset; import android.util.displaymetrics; import android.view.motionevent; import android.view.scalegesturedetector; import android.view.view; public class zoomview extends view{ private drawable micon; private float mposx; private float mposy; private float mlasttouchx; private float mlasttouchy; private scalegesturedetector mscaledetector; private float mscalefactor = 1.f; private static final int invalid_pointer_id = -1; // ‘active pointer’ 1 moving our object. private int mactivepointerid = invalid_pointer_id; context context; int height, width; public zoomview(context context) { super(context); micon = context.getresources().getdrawable(r.drawable.image_1); this.context = context; init(); } public zoomview(context context, attributeset attrs) { super(context, attrs); micon = context.getresources().getdrawable(r.drawable.image_1); this.context = context; init(); } public zoomview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); micon = context.getresources().getdrawable(r.drawable.image_1); this.context = context; init(); } public void init(){ displaymetrics displaymetrics = new displaymetrics(); ((activity) getcontext()).getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); height = displaymetrics.heightpixels; width = displaymetrics.widthpixels; micon.setbounds(0, 0, 0 + micon.getintrinsicwidth(), 0 + micon.getintrinsicheight()); mscaledetector = new scalegesturedetector(context, new scalelistener()); } @override public void ondraw(canvas canvas) { super.ondraw(canvas); canvas.save(); canvas.translate(width/2 + mposx - (micon.getintrinsicwidth() * mscalefactor) / 2, height/2 + mposy - (micon.getintrinsicwidth() * mscalefactor) / 2); canvas.scale(mscalefactor, mscalefactor); micon.draw(canvas); //rectmid.set(0, 0, canvas.getwidth(), canvas.getheight()/2); //canvas.drawrect(rectmid, blue); canvas.restore(); } @override public boolean ontouchevent(motionevent ev) { // let scalegesturedetector inspect events. mscaledetector.ontouchevent(ev); final int action = ev.getaction(); switch (action & motionevent.action_mask) { case motionevent.action_down: { 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); // move if scalegesturedetector isn't processing gesture. 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; 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) { // our active pointer going up. choose new // active pointer , adjust accordingly. final int newpointerindex = pointerindex == 0 ? 1 : 0; mlasttouchx = ev.getx(newpointerindex); mlasttouchy = ev.gety(newpointerindex); mactivepointerid = ev.getpointerid(newpointerindex); } break; } } return true; } 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, 5.0f)); invalidate(); return true; } } }
Comments
Post a Comment