Android expands View's Click Area

Source: Internet
Author: User

Sometimes, according to the visual map to make the effect, found that the click Area too small, not good click, the user experience is certainly not good. Enlarging the view will cause the entire visual image to become unattractive. So is there any way to expand the click Area without changing the view size?

The answer is YES!

Be able to solve the premise of this problem you have to have a certain understanding of the event distribution mechanism of View.

Below I will briefly introduce the view of the event distribution mechanism to facilitate understanding of the following solutions.

To clarify the entire mechanism, use the following view to illustrate the Click event distribution mechanism. is a framelayout (ViewGroup) with a ImageView (View) on the bread.

First, customize a myframelayout, inherit the Framelayout, and implement two click-related interfaces, the code is as follows:

 Public classMyframelayoutextendsFramelayoutImplementsOnclicklistener, Ontouchlistener {Private Static FinalString TAG = "Event";  PublicMyframelayout (Context context, AttributeSet attrs) {Super(context, attrs); LOG.D (TAG,"Myframelayout Init"); Setonclicklistener ( This); Setontouchlistener ( This); } @Override Public Booleandispatchtouchevent (Motionevent event) {log.d (TAG,"Myframelayout dispatchtouchevent" +event.getaction ()); return Super. Dispatchtouchevent (event); } @Override Public Booleanontouchevent (Motionevent event) {log.d (TAG,"Myframelayout ontouchevent" +event.getaction ()); return Super. Ontouchevent (event); } @Override Public voidOnClick (view view) {LOG.D (TAG,"Myframelayout OnClick"); } @Override Public BooleanOnTouch (view view, Motionevent event) {log.d (TAG,"Myframelayout OnTouch" +event.getaction ()); return true; } @Override Public Booleanonintercepttouchevent (motionevent ev) {LOG.D (TAG,"Myframelayout onintercepttouchevent" +ev.getaction ()); return Super. onintercepttouchevent (EV); }}

Next, for ImageView also do similar operations, the specific code is as follows:

 Public classMyimageviewextendsImageViewImplementsOnclicklistener, Ontouchlistener {Private Static FinalString TAG = "Event";  PublicMyimageview (Context context, AttributeSet attrs) {Super(context, attrs); LOG.D (TAG,"Myimageview Init"); Setonclicklistener ( This); Setontouchlistener ( This); } @Override Public Booleandispatchtouchevent (Motionevent event) {log.d (TAG,"Myimageview dispatchtouchevent" +event.getaction ()); return Super. Dispatchtouchevent (event); } @Override Public Booleanontouchevent (Motionevent event) {log.d (TAG,"Myimageview ontouchevent" +event.getaction ()); return Super. Ontouchevent (event); } @Override Public BooleanOnTouch (View arg0, motionevent arg1) {log.d (TAG,"Myimageview OnTouch" +arg1.getaction ()); return false; } @Override Public voidOnClick (View arg0) {log.d (TAG,"Myimageview OnClick"); }}

Here to illustrate is that only ViewGroup have onintercepttouchevent method, the ordinary View is not, it is not able to intercept the event.

At this time, if we click inside the ImageView, what kind of output? The results are as follows.

What if we click on the outer layer?

0,1,2 are representative of Action_down,action_up,action_move; A click action consists of a down, an up, and multiple MOVE operations.

Another look at the source code:

 public  boolean   dispatchtouchevent (motionevent ev) { boolean     consume = false  ;  if   (onintercepttouchevent (EV)) {consume    /span>= ontouchevent (EV);  else  {consume  = child.dispatchtouchevent (EV);  return   consume;}  

The above code makes it clear that for a viewgroup, when a click event is generated, it is first passed to it, and then dispatchtouchevent is called, if the ViewGroup Onintercepttouchevent returns true to indicate that it is intercepting the event and will be handed over to its ontouchevent for processing. If the ViewGroup onintercepttouchevent returns false, it is passed to the child element, and the dispatchtouchevent of the child element is called, so the loop repeats. This is consistent with the result of the above picture.

It is also stated that if the code is set to Ontouchlistener, then the OnTouch method is called first and then Ontouchevent is called. Onclicklistener is the lowest priority, so the OnClick is not called at the end.

Therefore, it can be seen from the second result graph that the ontouchevent and OnClick two methods are not called after OnTouch exists.

I believe that here, we have a certain understanding of the event distribution mechanism of the view.

Back to the beginning of the question, what is the way to expand the View of the click area?

Answer: Block the Click event in the parent view settings Ontouchlistener, determine whether the event is the corresponding child view, or the parent view, by judging the location of the click.

The specific implementation code is as follows:

 Public classTouchfactory {/**Expand Vertical Click area Size*/    Private Static Final intExt_v_size = 200;  Public StaticView.ontouchlistener Creattouchlistener () {return NewView.ontouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {if(Expendtouchsize (V, event)) {return true; }                return false;    }        }; }     Public Static Booleanexpendtouchsize (View root, motionevent event) {if(Rootinstanceofmyframelayout) {ImageView View=( (myframelayout) root). Getmyimageview (); if(View! =NULL&& view.getvisibility () = =view.visible) {Rect touchrect=NewRect ();                View.getglobalvisiblerect (Touchrect); intAction =event.getaction (); floatx =event.getrawx (); floaty =Event.getrawy (); if(y >= touchrect.top-ext_v_size) && (y <= touchrect.bottom +ext_v_size)) {                    if(x >=touchrect.left) {if(Action = =motionevent.action_up) {Toast.maketext (View.getcontext (),"Touch", Toast.length_short). Show (); }                        return true; }                }            }        }        return false; }}

Touchfactory encapsulates the Click event and determines whether or not to intercept the click event by judging the click area.

Below is Myframelayout of the concrete implementation. Because it is a custom view, the variable myimageview is bound to be empty, so it is assigned a value.

 Public classMyframelayoutextendsFramelayout {Private Static FinalString TAG = "Event"; PrivateMyimageview Myimageview;  PublicMyframelayout (Context context) { This(Context,NULL); }     PublicMyframelayout (Context context, AttributeSet attrs) { This(context, Attrs, 0); }     PublicMyframelayout (context context, AttributeSet attrs,intdef) {        Super(Context, attrs, Def);    Init (); }     Public voidinit () { This. Setontouchlistener (Touchfactory.creattouchlistener ()); }         PublicMyimageview Getmyimageview () {if(Myimageview = =NULL) {Myimageview=Findviewbyid (r.id.mimage); }        returnMyimageview; }}

Note: when the sub-view is set to Onclicklistener, the click Area is just the inside of the sub-view, it will consume this matter see, the parent view of the interception process is invalid, so once the interception to expand the area of the click, do not go to the sub view Set the click Callback to consume the Click event.

Android expands View's Click Area

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.