android - Custom View does not move with gestures -
i implemented dragging , scaling code offered on android developer website found here
the problem running in regards ability move view once has been added layout. image scalable through gesture not movable.
this view being added framelayout within fragment if helps @ all.
has ran similar problem implementing android example in custom view? or can tell me missing cannot move views adding.
public class customimageview extends view { private static final int invalid_pointer_id = 0; drawable _drawable; // private static readonly int invalidpointerid = -1; // private int _activepointerid = invalidpointerid; private float _posx; private float _posy; private float mscalefactor = 1.0f; private int mactivepointerid = invalid_pointer_id; // gesture listeners private scalegesturedetector mscaledetector; private float mlasttouchx; private float mlasttouchy; private float mposy; private float mposx; public customimageview(context context, int resourceid) { super(context, null, 0); _drawable = getresources().getdrawable(resourceid); _drawable.setbounds(0, 0, 200, 200); mscaledetector = new scalegesturedetector(context, new scalelistener()); // todo auto-generated constructor stub } protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.save(); canvas.translate(_posx, _posy); canvas.scale(mscalefactor, mscalefactor); _drawable.draw(canvas); canvas.restore(); } @override public boolean ontouchevent(motionevent ev) { // let scalegesturedetector inspect events. mscaledetector.ontouchevent(ev); final int action = motioneventcompat.getactionmasked(ev); switch (action) { case motionevent.action_down: { final int pointerindex = motioneventcompat.getactionindex(ev); final float x = motioneventcompat.getx(ev, pointerindex); final float y = motioneventcompat.gety(ev, pointerindex); // remember started (for dragging) mlasttouchx = x; mlasttouchy = y; // save id of pointer (for dragging) mactivepointerid = motioneventcompat.getpointerid(ev, 0); break; } case motionevent.action_move: { // find index of active pointer , fetch position final int pointerindex = motioneventcompat.findpointerindex(ev, mactivepointerid); final float x = motioneventcompat.getx(ev, pointerindex); final float y = motioneventcompat.gety(ev, pointerindex); // calculate distance moved final float dx = x - mlasttouchx; final float dy = y - mlasttouchy; mposx += dx; mposy += dy; invalidate(); // remember touch position next move event 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 = motioneventcompat.getactionindex(ev); final int pointerid = motioneventcompat.getpointerid(ev, pointerindex); if (pointerid == mactivepointerid) { // our active pointer going up. choose new // active pointer , adjust accordingly. final int newpointerindex = pointerindex == 0 ? 1 : 0; mlasttouchx = motioneventcompat.getx(ev, newpointerindex); mlasttouchy = motioneventcompat.gety(ev, newpointerindex); mactivepointerid = motioneventcompat.getpointerid(ev, 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; } }
}
here xml, root_layout adding views framelayout
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <relativelayout android:layout_width="fill_parent" android:layout_height="@dimen/ab_height" android:background="@color/red" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <imagebutton android:id="@+id/btn_back" android:layout_width="@dimen/width" android:layout_height="fill_parent" android:background="@color/red" android:contentdescription="@string/desc" android:src="@drawable/ic_navigation_back" /> <linearlayout android:layout_width="@dimen/dim_1" android:layout_height="fill_parent" android:background="@color/red" > </linearlayout> <textview android:id="@+id/text_name" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:ellipsize="end" android:gravity="center" android:singleline="true" android:tag="bold" android:text="pixagram" android:textcolor="@color/white" android:textsize="@dimen/tex_size_xxxlarge" android:textstyle="bold" /> <linearlayout android:layout_width="@dimen/dim_1" android:layout_height="fill_parent" android:background="@color/red" > </linearlayout> <imagebutton android:id="@+id/btn_accept" android:layout_width="@dimen/width" android:layout_height="fill_parent" android:background="@color/red" android:contentdescription="@string/desc" android:src="@drawable/ic_navigation_accept" /> </linearlayout> </relativelayout> <linearlayout android:layout_width="fill_parent" android:layout_height="@dimen/dim_1" android:background="@color/red" > </linearlayout> <framelayout android:id="@+id/root_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <imageview android:id="@+id/imageviewedit" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/abc_ab_solid_dark_holo" /> </framelayout> <linearlayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="@color/red" android:orientation="horizontal" > <horizontalscrollview android:id="@+id/horizontalscrollview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="bottom" android:gravity="bottom" android:background="@color/transparent" > <linearlayout android:id="@+id/linearlayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:background="@color/transparent" > <imagebutton android:id="@+id/imagebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ic_clear" /> <imagebutton android:id="@+id/imagebutton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_tab_selected_pressed_holo" /> <imagebutton android:id="@+id/imagebutton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ic_clear" /> <imagebutton android:id="@+id/imagebutton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_tab_selected_pressed_holo" /> <imagebutton android:id="@+id/imagebutton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ic_clear" /> <imagebutton android:id="@+id/imagebutton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_tab_selected_pressed_holo" /> <imagebutton android:id="@+id/imagebutton7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ic_clear" /> <imagebutton android:id="@+id/imagebutton8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_tab_selected_pressed_holo" /> <imagebutton android:id="@+id/imagebutton9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ic_clear" /> <imagebutton android:id="@+id/imagebutton10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_tab_selected_pressed_holo" /> </linearlayout> </horizontalscrollview> </linearlayout> </linearlayout>
this seems show action_move not getting called
09-23 23:14:46.310: i/viewrootimpl(24235): viewroot's touch event : touch down 09-23 23:14:46.350: i/viewrootimpl(24235): viewroot's touch event : touch 09-23 23:14:47.300: i/viewrootimpl(24235): viewroot's touch event : touch down 09-23 23:14:47.790: i/viewrootimpl(24235): viewroot's touch event : touch 09-23 23:14:48.000: i/viewrootimpl(24235): viewroot's touch event : touch down 09-23 23:14:48.030: i/viewrootimpl(24235): viewroot's touch event :261 09-23 23:14:48.670: i/viewrootimpl(24235): viewroot's touch event :6 09-23 23:14:48.710: i/viewrootimpl(24235): viewroot's touch event : touch 09-23 23:14:48.980: i/viewrootimpl(24235): viewroot's touch event : touch down 09-23 23:14:49.320: i/viewrootimpl(24235): viewroot's touch event : touch
although haven't dug through of code, looks in ondraw()
method, translating _posx
, _posy
, don't change these anywhere in gesture handling. try using mposx
, mposy
in ondraw()
instead.
Comments
Post a Comment