Take a custom ImageView example:
/*** Can be dragged ImageView* Created by admin on 2017/2/21. */public class Floatingimageview extends imageview{public Floatingimageview (context context) {super (context); } public Floatingimageview (context context, AttributeSet Attrs) {Super (context, attrs); } public Floatingimageview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defsty LEATTR); } @TargetApi (Build.version_codes.LOLLIPOP) Public Floatingimageview (context context, AttributeSet attrs, int defstyleattr, int defstyleres) {Super (conte XT, Attrs, Defstyleattr, defstyleres); } int StartX; int starty; int left; int top; int[] temp = new int[]{0, 0}; @Override public boolean ontouchevent (Motionevent event) {Boolean ismove = false; int x = (int) event.getrawx (); int y = (int) Event.getrawy (); Switch (event.getaction ()) {case Motionevent.Action_down://Touch down so check if the StartX = x; Starty = y; TEMP[0] = (int) event.getx (); TEMP[1] = Y-gettop (); Break Case Motionevent.Action_move://Touch drag with the ball left = x-temp[0]; top = y-temp[1]; if (left < 0) {//The control of the Ieft border does not exceed the leftmost = 0; } layout (left, top, left + getwidth (), Top + getheight ());//free drag to break; Case Motionevent.action_up: if (Math.Abs (X-STARTX) > 2 | | Math.Abs (Y-starty) > 2) {//To determine whether to move, then a certain range is not a move, to resolve the triggering event conflict//will drag the last position to set down, otherwise the page refresh after rendering the button will automatically return to the original position Note The parent container relativelayout.layoutparams LP = (relativelayout.layoutparams) getlayoutparams (); Lp.setmargins (left, top,0,0); SETLAYOUTPARAMS (LP); OK is drag ismove = true; } break; } return Ismove? True:super.onTouchEvent (event); }}
Android customization can drag view, interface rendering refresh will not automatically return to the start position