canvas - Android custom width adjustable slider -
i'm trying make custom slider, can dragged , width can changed.
i made relative layout view that has frame layout 2 images (left , right) , text in middle. when frame layout dragged animate new position of view. have problem adjust width of view when left or right image dragged. tried changes layout parameter re-centers whole view , doesn't extend dragged side.
xml layout
<relativelayout android:id="@+id/test_layout" android:layout_width="match_parent" android:layout_height="40dp" android:background="@color/coloraccent" android:orientation="horizontal"> <framelayout android:id="@+id/frame" android:layout_width="80dp" android:layout_height="40dp" android:layout_centerinparent="true" android:layout_marginbottom="5dp" android:layout_margintop="5dp" android:animatelayoutchanges="true" android:background="@drawable/test_background"> <imageview android:id="@+id/left" android:layout_width="10dp" android:layout_height="match_parent" android:background="@drawable/test_background" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="false" android:text="test" android:textcolor="#7a1ea1" /> <imageview android:id="@+id/right" android:layout_width="10dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="@drawable/test_background" /> </framelayout> </relativelayout>
binding.frame.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view view, motionevent event) { switch (event.getaction()) { case motionevent.action_down: dx = view.getx() - event.getrawx(); dy = view.gety() - event.getrawy(); break; case motionevent.action_move: float x = event.getrawx() + dx; if (x > binding.testlayout.getright()) x = binding.testlayout.getright(); else if (x < binding.testlayout.getleft()) x = binding.testlayout.getleft(); view.animate() .x(x) .setduration(0) .start(); break; default: return false; } return true; } });
or should rather use canvas this?
Comments
Post a Comment