Android customization can drag view, interface rendering refresh will not automatically return to the start position

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.