The following is an example of how to implement sliding, the main function of the example is to allow our custom view to move as the finger moves. The layout file is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" match_parent " android:layout_height= "Match_parent" > <com.scu.lly.dragviewtest.view.dragview android:layout_width= "100DP" Android : layout_height= "100DP"/></linearlayout>
Mode One: Layout methodwhen the view is drawn, the OnLayout () method is called to set the display position, so we can control the view's coordinates by modifying the view's left, top, right, and bottom four properties. To control the view sliding with your finger, you need to slide control in the Ontouchevent () event. The code is as follows:
public class Dragview extends view{private int mlastx; private int mlasty; Public Dragview (Context context) {super (context); Init (); } public Dragview (context context, AttributeSet Attrs) {Super (context, attrs); Init (); } public Dragview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Init (); } private void Init () {setbackgroundcolor (Color.Blue); } @Override public boolean ontouchevent (motionevent ev) {int x = (int) ev.getx (); int y = (int) ev.gety (); Switch (ev.getaction ()) {Case MotionEvent.ACTION_DOWN:mLastX = x; Mlasty = y; Break Case MotionEvent.ACTION_MOVE:int OffsetX = X-MLASTX; int OffsetY = Y-mlasty; Adjust layout four coordinates layout (GetLeft () + OffsetX, GetTop () + OffsetY, getRight () + OffsetX, GetbottOm () + OffsetY); Break } return true; }}
Way Two: Offsetleftandright () and Offsettopandbottom ()These two methods are in fact the layout of the above way of packaging, simplification, in layout, left and right two directions are added offsetx, top, bottom bottom Two directions are added offsety, in order to simplify the setting of four directions, Android provides offsetleftandright () instead of the left and right settings, using Offsettopandbottom () instead of the up and down directions. We only need to modify the section of the above code Action_move, as follows:
<span style= "White-space:pre" ></span>case motionevent.action_move: int offsetX = X-MLASTX; int offsetY = y-mlasty; Adjust layout of four coordinates //layout (getLeft () + OffsetX, GetTop () + OffsetY, getRight () + OffsetX, Getbottom () + OffsetY); Use shorthand form offsetleftandright (OffsetX); Offsettopandbottom (OffsetY); Break
Way Three: LayoutparamsLayoutparams preserves the layout parameters of a view, so we can change the position of the view by dynamically changing the layout parameters in the Layoutparams. Using the Getlayoutparams () method to get the layoutparams of the view, Layoutparams needed to set different types based on the type of the view's parent layout, for example, Our custom view is placed in the LinearLayout, so the linearlayout.layoutparams is obtained through Getlayoutparams (). Thus, the premise of obtaining layoutparams through Getlayoutparams () is that the view needs to have a parent layout. again, we only need to modify the action_move of the above code , as follows:
Case motionevent.action_move: int offsetX = X-MLASTX; int offsetY = y-mlasty; Linearlayout.layoutparams LP = (linearlayout.layoutparams) getlayoutparams (); Lp.leftmargin = GetLeft () + OffsetX; Lp.topmargin = GetTop () + OffsetY; SETLAYOUTPARAMS (LP); Break
As you can see, changing the position of a view by Layoutparams changes the margin property of the view, which is why this approach must have a parent layout, and the setting of the margin property only works if you have a parent layout.for changing the view position in this way, if we don't want to consider the type of the parent layout, you can also use Viewgroup.marginlayoutparams to set it, which is also more convenient. as follows:
Case motionevent.action_move: int offsetX = X-MLASTX; int offsetY = y-mlasty; Linearlayout.layoutparams LP = (linearlayout.layoutparams) getlayoutparams (); Viewgroup.marginlayoutparams LP = (viewgroup.marginlayoutparams) getlayoutparams (); Lp.leftmargin = GetLeft () + OffsetX; Lp.topmargin = GetTop () + OffsetY; SETLAYOUTPARAMS (LP); Break
The effect is the same.
Way Four: Scrollto and Scrollbyabout the Scrollto () and Scrollby () methods, this article "Scroller disclosure" is described in detail.
one thing to note about using the ScrollTo () and Scrollby () methods is that thescrollTo () and Scrollby () methods move the content of the view, that is, to move the contents of the view, If you use the Scrollto () and Scrollby () methods in ViewGroup, then all child view will be moved, and if used in view, the contents of the view will be moved . For example, Textview,content is its text, and Imageview,content is its drawable object.
So, in the example above, if we use this directly:Scrollby (offsetx,offsety);The view is not moving, but it is moving, but it is moving the contents of the view, and the content in our example is nothing. so, to make this view move, we should use the Scrollby or Scrollto method to move through the view's viewgroup. At the same time, the user two methods to move, notice at this time the coordinate direction and the usual is the opposite, specific in the "Scroller big secret" has explained. The code is as follows:
Case Motionevent.action_move: //int offsetX = x-mlastx; int offsetY = y-mlasty; At this point, the computed coordinates are the opposite of int offsetX = mlastx-x; int offsetY = mlasty-y; Let the viewgroup of the view be moved (view) getParent ()). Scrollby (offsetx,offsety); Break
Way Five: Scrollerby scroller this auxiliary class, with Scrollto and Scrollby can achieve some more advanced sliding effect, about the Scroller class specific introduction, also in this article has the detailed "Scroller big secret".
here, we just combine the above example to achieve a simple function, when we swipe over to lift the finger, the view automatically bounces back to the original position. The code is as follows:
public class Dragview extends view{private int mlastx; private int mlasty; Private Scroller Mscroller; Public Dragview (Context context) {super (context); Init (context); } public Dragview (context context, AttributeSet Attrs) {Super (context, attrs); Init (context); } public Dragview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Init (context); } private void Init (context context) {SetBackgroundColor (Color.Blue); Mscroller = new Scroller (context); } @Override public boolean ontouchevent (motionevent ev) {int x = (int) ev.getx (); int y = (int) ev.gety (); Switch (ev.getaction ()) {Case MotionEvent.ACTION_DOWN:mLastX = x; Mlasty = y; Break Case Motionevent.action_move://int OffsetX = X-MLASTX; int OffsetY = Y-mlasty; At this point, the computed coordinates are the opposite of int offsetX = Mlastx-x; int OffsetY = mlasty-y; Let the viewgroup of the view be moved (view) getParent ()). Scrollby (offsetx,offsety); Break Case MotionEvent.ACTION_UP:View ViewGroup = (View) getParent (); Mscroller.startscroll (Viewgroup.getscrollx (), viewgroup.getscrolly (),-VIEWGROUP.GETSCROLLX (),- Viewgroup.getscrolly ()); Remember to need invalidate invalidate (); Break } return true; @Override public void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {(View) getParent ()). ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ()); Remember that you need to constantly call invalidate to redraw invalidate (); } }}
The above five methods are commonly used sliding view method. There are two ways to control the movement of a view: property animation and use of Viewdraghelper, for these two methods, you can check the online information, not detailed introduction.
(The above is from the "Android Elite Biography" summary)
Several ways to implement sliding on Android