About Ontouchevent (Motionevent event) events monitoring in Android custom view

Source: Internet
Author: User


Make a custom ViewGroup today, add the child control dynamically by AddView, in order to save trouble, rewrite the public boolean ontouchevent (Motionevent event) {} method directly in the parent control to listen to which button is currently touching. It's a bit of a problem, so write it down.

First, the Click Effect is only Motionevent.action_down, this returns the return to true;

And then the difference between GetX () and GETRAWX (), this post has been written to

Http://www.cnblogs.com/foura/articles/2016881.html

The summary is:GetX () is the x-coordinate of the widget relative to its upper-left corner, and Getrawx () is the x-coordinate value relative to the upper-left corner of the screen (note: The upper-left corner of the screen is the upper-left corner of the phone screen, regardless of whether the activity is titlebar or full screen ), GetY (), Getrawy () The same reason

The previously set rectangle was always

Rects.add (New Rect ());

Getchildat (i). Getglobalvisiblerect (Rects.get (i)); but there was something wrong with the coordinates.

Have to use Rects.add directly (new Rect (V.getleft (), V.gettop (), V.getright (), V.getbottom ()));

Use the child view's position in the parent view to draw the rectangle, and then compare the Getx () method of the parent view.

The difference between these methods of looking at dizziness, mainly refer to this article, to distinguish which is relative to the screen, which is relative to their own

http://blog.csdn.net/centralperk/article/details/7949900

Especially this one.GetLeft , GetTop, Getbottom, GetRight, this group is the help to get the coordinates of the child view relative to its parent view.

Here casually put a monitoring method, of course, to improve to use, solve a little problem

Private arraylist<rect> rects = new arraylist<rect> ();p Rivate int  eventX = 0;private int eventY = 0; @Overridepublic  boolean  Ontouchevent (motionevent event)  {// todo auto-generated method stub//log.e ("",   "_____" +event.getaction ()); if (Event.getaction ()  == motionevent.action_down) {eventx =   (int)  event.getx ();eventy =  (int)  event.gety ();} Determine which item the current user clicked on if  (Event.getaction ()  == motionevent.action_up)  {int upX =  ( int)  event.getx ();int upy =  (int)  event.gety (); if (Math.Abs (UPX-EVENTX) <50& &math.abs (upy-eventy) <50) {for  (Int i = 0,len=getchildcount (); i <  len; i++)  {if (Rects.size ()  < len) {view v = getchildat (i); rects.add (New rect (V.getleft (), V.gettop (), V.getright (), V.getbottom ()));} &nBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.E ("",  +upX+ "+" +event.getRawX () + "__" +upy+ "+" +event.getrawy () + "___" +rects.get (i)), if (Rects.get (i). Contains (upx, upy)) {LOG.E ("",  "i__" +i); Getchildat (i). SetSelected (True); Else{getchildat (i). SetSelected (False);}}} Return true;}


This article is from the "Fly to learn Android" blog, please be sure to keep this source http://qq445493481.blog.51cto.com/9545543/1716388

About Ontouchevent (Motionevent event) events monitoring in Android custom view

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.