customizing Draggable TextView

Source: Internet
Author: User
Tags gety

Write a custom drag control to understand some of the points of knowledge:

1.view.getleft () View.gettop () view.getright () View.getbottom ()

These four methods are methods of the view class: The function is to get the view left edge, the right edge to the Y axis distance, the top edge, the bottom edge to the x-axis distance (the axis is the upper left corner of the parent layout is the coordinate system), you can simply consider the upper left corner of the view and the lower right corner of the coordinates (

Extension: View.getx (), View.gety () is the position of the view in the parent layout, and also the upper-left coordinate of the view, where view.getx () = View.getleft () view.gety () =view.gettop ()

2.motionevent.getx (), Motionevent.gety () method

These two methods are to get the distance between the finger and the horizontal and vertical direction of the upper left corner of the view under touch conditions.

Extensions: MOTIONEVENT.GETRAWX () and Motionevent.getrawy () are obtained under touch conditions, where the finger is relative to the upper-left corner of the phone's screen in horizontal and vertical directions, and is usually used to determine whether the sliding distance is valid.

The 3.Layout (int left,int top,int Right,int bottom) method is used to control the location of the view.

This method is the key to make the view move, to correctly calculate the value of the left top right bottom, usually with the value of the previous state (1 of the four methods) plus in the horizontal or vertical direction to move the distance can be


With a general understanding of this series of methods, you can simply write a draggable custom control with the following code:


Package Com.example.android_dragbutton;import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.motionevent;import Android.widget.textview;public class DragTV extends TextView {Private float startx;//down event occurs when the finger is relative to the x-axis of the upper-left corner of the view when the private float starty;//down event occurs, the distance of the finger relative to the y-axis of the view's upper left private float EndX; When the move event occurs, the finger is relative to the x-axis of the upper-left corner of the view private float Endy; When the move event occurs, the finger is relative to the y-axis of the upper-left corner of the view, the private int. Dragtv the distance from the left edge relative to the parent control is private int top; Dragtv the distance from the upper edge relative to the parent control is private int right; Dragtv the right edge relative to the parent control's distance private int bottom; Dragtv the distance of the bottom edge relative to the parent control is private int hor; In the case of touch, the distance the finger moves in the x-axis is private int ver; In the case of touch, the finger moves in the y direction from public dragtv (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);  /Todo auto-generated constructor stub}public DRAGTV (context context, AttributeSet Attrs) {This (context, attrs, 0);//TODO Auto-generated Constructor Stub}public DRAGTV (context context) {This (context, null);//TODO auto-generated Constructor stub} @Overridepublic boolean Ontouchevent (Motionevent event) {//TODO auto-generated method Stubswitch ( Event.getaction ()) {Case motionevent.action_down://the moment the finger has just touched the screen, the distance between the finger and the horizontal and vertical direction of the upper left corner of the view: StartX Startystartx = Event.getx (); starty = Event.gety (); break;case motionevent.action_move://finger stays on the screen or moves, the finger is relative to the horizontal and vertical distance of the upper-left corner of the view: EndX Endyendx = Event.getx (); Endy = Event.gety ();//Gets the position of the view at this moment. left = GetLeft (), top = GetTop (), right = GetRight (), bottom = Getbottom ();//horizontal distance of the finger movement hor = (int) (ENDX-STARTX);//Vertical Finger movement Distance ver = (int) (endy-starty);//When the finger moves in a horizontal or vertical direction, reset the view position (layout method) if (hor! = 0 | | ver! = 0) {layout (left + hor, top + ver, right + hor, bottom + ver);} Break;case Motionevent.action_up:break;default:break;} return true;}}
demo:http://download.csdn.net/detail/laoziyueguo3/8456849

customizing Draggable TextView

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.