Android Draggable Controls Ontouchevent

Source: Internet
Author: User
<span id="Label3"></p><p><p>first, attach a link to the content of the article reproduced:</p></p><p><p></p></p><p><p>Learning Android Draggable Events first requires a detailed knowledge structure of the Android screen and TouchEvent Parameters.</p></p><p><p>1. Android coordinate system</p></p><p><p></p></p><p><p><span style="font-size: 14pt;"><strong>first, make clear the coordinate system in android: the upper left corner of the screen is the coordinate system Origin (0,0), the origin point to the right is the x-axis positive direction, the origin of the downward extension is the y-axis positive Direction. </strong></span><br><strong><span style="font-size: 16px;">1. View coordinates</span></strong><br>It is important to note that the coordinates of the view are relative to the parent container, including: getTop (), getbottom (), getLeft (), getRight (). Take GetTop as an example, the function source code is:<br>/**<br>* Top position of this view relative to its parent.<br>* Relative to the top position of the parent control, in pixels, that is, the distance from the head to the parent control<br>* @return The top of this view, in Pixels.<br>*/<br>@ViewDebug. Capturedviewproperty<br>Public final int GetTop () {<br>Return mtop;<br>}</p></p><p><p></p></p><p><p>Figure 1:<br><span></span><br><br><br>Figure 2:<br><span></span><br><br>1. Left position of view View.getleft ()<br>2. Right position of view View.getright ()<br>3, the top position of the view View.gettop ();<br>4, The view bottom position View.getbottom ();<br>5, View width view.getwidth ();<br>6. View Height View.getheight ()<br><strong><span style="font-size: 16px;">2. Motionevent class getrowx () and GetX ()</span></strong><br>1, event.getrowx (): The x-coordinate of the touch point relative to the screen origin<br>2, Event.getx (): The x-coordinate of the touch point relative to the origin of its component<br><span></span><br><br><br>second, there needs to be a clear grasp of the ontouchevent</p></p><p><p>For example, if you need to respond to action_up, then the Action_down event processing needs to return True.</p></p><p><p>Attach the source code and Explain:</p></p><p><p>1, first need to get the screen width length. Records the position at which the down point is Pressed.</p></p><p><p>2. In the move event, the moving position is calculated continuously and bounds checked, then the new coordinate position is set using the layout Function. Use Postinvalidate to Refresh.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.example.hellotouchevent;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Activity;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.util.DisplayMetrics;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.Menu;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.MotionEvent;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View.OnTouchListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.RelativeLayout;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.Toast;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Activity<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">ontouchlistener{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Relativelayout mrlfather; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Relativelayout mrlmezi; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Displaymetrics dm; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">lastx, lasty; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Initview (); DM</span></span>=<span style="color: #000000;"><span style="color: #000000;">getresources (). getdisplaymetrics (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScreenWidth =<span style="color: #000000;"><span style="color: #000000;">dm.widthpixels; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScreenHeight = dm.heightpixels-50<span style="color: #000000;"><span style="color: #000000;">; Setlistener (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Initview () {mrlfather</span></span>=<span style="color: #000000;"><span style="color: #000000;">(relativelayout) Findviewbyid (r.id.rl_father); Mrlmezi</span></span>=<span style="color: #000000;"><span style="color: #000000;">(relativelayout) Findviewbyid (r.id.rl_mezi); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setlistener () {mrlfather.setontouchlistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Mrlmezi.setontouchlistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Oncreateoptionsmenu (menu Menu) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Inflate the menu; this adds items to the action bar if it is Present.</span></span><span style="color: #000000;"><span style="color: #000000;">getmenuinflater (). Inflate (r.menu.main, menu); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">onTouch (View v, motionevent event) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>EA =<span style="color: #000000;"><span style="color: #000000;">event.getaction (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScreenWidth =<span style="color: #000000;"><span style="color: #000000;">dm.widthpixels; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScreenHeight =<span style="color: #000000;"><span style="color: #000000;">dm.heightpixels; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.rl_father:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">R.id.rl_mezi:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(ea) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">MotionEvent.ACTION_DOWN:lastX</span></span>= (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Event.getrawx ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the original x-coordinate of the Touch event touch Location</span></span>Lasty = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) Event.getrawy (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">motionevent.action_move:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>DX = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Event.getrawx ()-<span style="color: #000000;"><span style="color: #000000;">lastx; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>DY = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Event.getrawy ()-<span style="color: #000000;"><span style="color: #000000;">lasty; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>L = V.getleft () +<span style="color: #000000;"><span style="color: #000000;">dx; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>b = V.getbottom () +<span style="color: #000000;"><span style="color: #000000;">dy; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>r = V.getright () +<span style="color: #000000;"><span style="color: #000000;">dx; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>t = v.gettop () +<span style="color: #000000;"><span style="color: #000000;">dy; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">following determines whether the move exceeds the screen</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(l < 0<span style="color: #000000;"><span style="color: #000000;">) {l</span></span>= 0<span style="color: #000000;"><span style="color: #000000;">; R</span></span>= L +<span style="color: #000000;"><span style="color: #000000;">V.getwidth (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(t < 0<span style="color: #000000;"><span style="color: #000000;">) {t</span></span>= 0<span style="color: #000000;"><span style="color: #000000;">; b</span></span>= T +<span style="color: #000000;"><span style="color: #000000;">V.getheight (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(r ><span style="color: #000000;"><span style="color: #000000;">Screenwidth) {r</span></span>=<span style="color: #000000;"><span style="color: #000000;">screenwidth; L</span></span>= r-<span style="color: #000000;"><span style="color: #000000;">V.getwidth (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(b ><span style="color: #000000;"><span style="color: #000000;">Screenheight) {b</span></span>=<span style="color: #000000;"><span style="color: #000000;">screenheight; T</span></span>= b-<span style="color: #000000;"><span style="color: #000000;">V.getheight (); } v.layout (l, t, r, b); Lastx</span></span>= (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) event.getrawx (); Lasty</span></span>= (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) Event.getrawy (); V.postinvalidate (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">motionevent.action_up:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;"> : </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; }}</span></span></pre><p><p></p></p><p><p>Layout function</p></p><pre><pre>Btn1.layout (+, +, +<span style="color: #000000;">); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">0,0 on top left of container</span> <span style="color: #008000;">//</span> <span style="color: #008000;">the distance from the left of the container to the left of the control</span> <span style="color: #008000;">//</span> <span style="color: #008000;">the distance from the top of the container to the top of the T control</span> <span style="color: #008000;">//</span> <span style="color: #008000;">the distance from the left of the container to the right of the R control</span> <span style="color: #008000;">//</span> <span style="color: #008000;">the distance from the top of the container at the bottom of the B control</span></pre></pre><p><p></p></p><p><p>SOURCE Download:</p></p><p><p>The next blog highlights custom Controls.</p></p><p><p></p></p><p><p>Android Draggable Controls Ontouchevent</p></p></span>

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.