android - How to create an endeless / moveable Layout? "endless paper"-Metapher -
how create endless / movable layout, endless/movable in directions? want develop app create diagrams, , if diagram gets big want movable in directions.
i hope understandable.
with regards
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_draw); _root = (viewgroup) findviewbyid(r.id.draw); _view = new imageview(this); _view.setimageresource(r.drawable.greenbutton); bitmapdrawable bd=(bitmapdrawable) this.getresources().getdrawable(r.drawable.greenbutton); int height=bd.getbitmap().getheight(); int width=bd.getbitmap().getwidth(); /*myscrollview myv = new myscrollview(this); myv.setlayoutparams(_root.getlayoutparams());*/ relativelayout.layoutparams layoutparams=new relativelayout.layoutparams(height,width); _view.setlayoutparams(layoutparams); _view.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { final int x = (int) event.getrawx(); final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: relativelayout.layoutparams lparams = (relativelayout.layoutparams) v.getlayoutparams(); _xdelta = x - lparams.leftmargin; _ydelta = y - lparams.topmargin; break; case motionevent.action_up: break; case motionevent.action_pointer_down: break; case motionevent.action_pointer_up: break; case motionevent.action_move: relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) v.getlayoutparams(); layoutparams.leftmargin = x - _xdelta; layoutparams.topmargin = y - _ydelta; layoutparams.rightmargin = -250; layoutparams.bottommargin = -250; v.setlayoutparams(layoutparams); break; } _root.invalidate(); return true; } }); //myv.addview(_view); _root.addview(_view); <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".draw"> <relativelayout android:id="@+id/draw" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/layout_grid"> </relativelayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical"> <gridview android:id="@+id/mostusedbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numcolumns="2" android:layout_weight="1"> </gridview> <gridview android:id="@+id/elementbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numcolumns="2" android:layout_weight="1"></gridview> <gridview android:id="@+id/relbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numcolumns="2" android:layout_weight="1"></gridview> </linearlayout> </linearlayout>
create custom view scrolls (do manually) , keep in 2 variables let's scrollx , scrolly (assume point @ top left point of screen). draw on top offset of scrollx , scrolly.
here code:
public class myscrollview extends view { private int scrollx, scrolly; private int pinpointx, pinpointy; public myscrollview(context context) { super(context); //initialize scroll @ top left edge of "paper" scrollx = 0; scrolly = 0; } public void ontouchevent(motionevent e) { switch (e.getaction()) { case motionevent.action_down: //mark touch point pinpointx = (int) e.getx(); pinpointy = (int) e.gety(); break; case motionevent.action_move: //scroll view scrollx += (int) e.getx() -pinpointx; scrolly += (int) e.gety() -pinpointy; //mark new point pinpointx = (int) e.getx(); pinpointy = (int) e.gety(); break; } } @override public void ondraw(canvas canvas) { super.ondraw(canvas); //draw here offset of scrollx, scrolly } } sorry may have made mistakes (mainly android's class names) wrote code on go, if u see minor mistake make sure correct it.
note code allow scroll "infinitely" @ least long scrollx , scrolly remain on int limits (from -2,147,483,648 2,147,483,647) can keep since nobody scroll far, i'd recommend applying limits make app more "perfect".
also i'm not sure think code may have issue if scroll negative side (leftwards , upwards - when scrollx and/or scrolly negative) may wrong.
if want include more views (like layout ask) make viewgroup don't have experience using that. showed logic follow it's attach code like.
Comments
Post a Comment