The distribution, processing and consumption of Android touch events, as well as the drag effect of images

Source: Internet
Author: User

There are only two main characters in touch event distribution: ViewGroup and view. The activity's touch event is actually called the ViewGroup touch event inside it, and can be treated directly as ViewGroup.

View within the ViewGroup, ViewGroup can also be in other viewgroup, this time the internal viewgroup as a view to analyze.

There are three related events in ViewGroup: Onintercepttouchevent, Dispatchtouchevent, Ontouchevent. There are only two related events for view: Dispatchtouchevent, Ontouchevent.

While Dispatchtouchevent is used to distribute touch events, Ontouchevent is used to handle touch events ()

The first step, the creation of touch events:

Touch event by the user touch the screen, the system will generate a Motionevent touch event object, which encapsulates the information used for this action, such as time, location coordinates.

The current touch event is then handed over to the current activity for processing.

The second step, the distribution and processing of touch events:

1, the system will touch the event to the activity processing, will call the activity of the Dispatchtouchevent method for the distribution of events, activity will first find the corresponding layout of the event

Father ViewGroup, handing the touch event to him.

2. The viewgroup will then call its own Dispatchtouchevent method to distribute the event:

There are two different situations:

The first is that the ViewGroup distribution event finds the corresponding child view for processing.

Give it to the child view to handle, child when the view gets the event, it also calls its own Dispatchtouchevent method, but the view distribution is to call the view itself ontouchevent or view settings

Ontouchlistener's Ontouch method handles events (it is worth mentioning that the Ontouch method has a higher priority than the Ontouchevent method, that is, the Ontouch method is executed first).

In both the Ontouchevent and Ontouch methods, it is necessary to return a Boolean type value, and if true, the touch event has been consumed, not passed down, there is a blocking touch to intercept the meaning of the event, the other child

View cannot respond to this touch event, and if False is returned, the touch event is not consumed and needs to be passed to other child view processing. If all sub-view does not consume this touch event

, and finally the touch event is handled by the ontouchevent of the activity.

A full touch event consists of down, move, and up.

What is worth noting here is that the activity distribution event is based on whether the view consumes an action-down touch event, and if there is no consumption, it will not be distributed to the touch event that the action is up and move.

The second is that the ViewGroup distribution event did not find the corresponding sub-view for processing.

At this time the event is handled by ViewGroup itself, it will call its own ontouchevent return false, indicating that the event did not find consumers, touch events are not consumed, move,up action does not need to be distributed further down,

Directly invokes the current activity's ontouchevent (EV) method for processing.

Only the view consumes an action-down touch event, and this view responds to the touch events of the other two actions.

After the Android event was distributed, let's talk about the implementation of the picture drag effect.

The first step, set a ontouchlistener for ImageView, and implement its Ontouch method

The second step, the implementation of the drag logic:

1, the finger press on the ImageView will have an initial position (Startx,starty)

2. Move your finger on the screen and move to a new location (Nowx,nowy)

3. Calculate the offset of the finger on the screen

Dx=nowx-startx

Dy=nowy-starty

4, recalculate the position of the picture, and control the range so that the picture will not exceed the border

int Left=miv_show.getleft () +dx; int top=miv_show.gettop () +dy; int right=miv_show.getright () +dx; int Buttom=miv_show.getbottom () +dy;
if (left<=0) {                        right+=-left;                        Left=0;                    }                    if (top<=0) {                        buttom+=-top;                        Top=0;} if (right>=mright) {left+=-(right-mright); right=mright;} if (buttom>=mbuttom {top+=-(buttom-mbuttom); buttom=Mbuttom;}           

5. Update the location of ImageView immediately by calling Imageview.layout (Left,top,right,bottom)

(left refers to the x-coordinate in the upper-right corner of the picture, top refers to the y-coordinate of the upper-left corner of the picture, and right refers to the x-coordinate of the bottom, bottom knows the y-coordinate of the bottom-bottom corner of the picture)

6. Re-initialize the start position of the finger (startx=nowx,starty=nowy)

7. When the finger leaves the picture, the position of the current picture is recorded, and the next ImageView loading is completed and echoed.

Here comes the question, when will the ImageView be loaded and finished? When an activity creates a call to the OnCreate method

ImageView is not loaded yet, if calling the Imageview.layout method in the OnCreate method is not working

, so we want to set up a listener, when the picture is loaded, callback the method of the listener, in this method call layout echo the location of the picture,

The code is as follows:

Miv_show.getviewtreeobserver (). Addongloballayoutlistener (NewOngloballayoutlistener () {@Override Public voidongloballayout () {System.out.println ("View load Complete"); intLastleft=sp.getint ("Lastleft", 0); intLasttop=sp.getint ("Lasttop", 0); intLastright=sp.getint ("Lastright", 0); intLastbuttom=sp.getint ("Lastbuttom", 0); if(! (lastleft==0&&lasttop==0&&lastright==0&&lastbuttom==0)){                    //Description Save Locationmiv_show.layout (Lastleft, Lasttop, Lastright, Lastbuttom); }            }        });

The code is implemented as follows:

Miv_show.setontouchlistener (NewOntouchlistener () {Private intStartX; Private intStarty; @Override Public BooleanOnTouch (View V, motionevent event) {intmright=rl.getright (); intMbuttom=Rl.getbottom (); Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:startX=(int) event.getrawx (); Starty=(int) Event.getrawy ();  Break;  CaseMotionevent.action_move:intNowx= (int) event.getrawx (); intNowy= (int) Event.getrawy (); intdx=nowx-StartX; intdy=nowy-Starty; intLeft=miv_show.getleft () +DX; intTop=miv_show.gettop () +dy; intRight=miv_show.getright () +DX; intButtom=miv_show.getbottom () +dy; if(left<=0) { Right+=-Left ; Left=0; }                    if(top<=0) {Buttom+=-top; Top=0; }                    if(right>=mright) { Left+=-(right-mright); Right=Mright; }                    if(buttom>=Mbuttom) {Top+=-(buttom-mbuttom); Buttom=Mbuttom;                                        } miv_show.layout (Left,top, right, buttom); StartX=nowx; Starty=Nowy;  Break;  Casemotionevent.action_up:intlastleft=Miv_show.getleft (); intlasttop=Miv_show.gettop (); intlastright=miv_show.getright (); intLastbuttom=Miv_show.getbottom (); Editor Editor=Sp.edit (); Editor.putint ("Lastleft", Lastleft); Editor.putint ("Lasttop", Lasttop); Editor.putint ("Lastright", Lastright); Editor.putint ("Lastbuttom", Lastbuttom);                    Editor.commit ();  Break; }                                //returns true to indicate that the event has been consumed and does not need to be passed down again//on behalf of all touch events are consumed here and no longer passed down                return true; }        });

The distribution, processing and consumption of Android touch events, as well as the drag effect of images

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.