Android code to simulate user clicks, swipe and other operations

Source: Internet
Author: User

/**
* Simulate user click
*
* @param view to trigger the action
* @param x x -axis offset relative to the upper-left corner of the view to manipulate
* @param y offset from the upper left-hand corner of the view to manipulate
*/
private static void Analoguserclick (view view, float x, float y) {
if (view = = null) {
Return
}
LOGUTIL.E (Tag_point, "simulating click Operation:p->" + x + "," + y);
Long downtime = Systemclock.uptimemillis ();//Analog Press down time

Long eventtime = downtime;//event Occurrence time

Motionevent downevent = Motionevent.obtain (Downtime, eventtime,
Action_down, x, y, 0);
View.ontouchevent (downevent);

Eventtime = eventtime + 90;//leave screen time

Motionevent upevent = Motionevent.obtain (Downtime, eventtime,
Motionevent.action_up, x, y, 0);
View.ontouchevent (upevent);

Recycling Events
Downevent.recycle ();
Upevent.recycle ();
}

/**
* Simulate user swipe operation
*
* @param view to trigger the action
* @param type of analog operation: uniform sliding, fast sliding
* @param the starting point of the p1x slide x coordinate
* @param p1y The starting point y-coordinate of the slide
* @param p2x the end of the slide x coordinate
* @param p2y the end of the slide y-coordinate
*/
private static void Analoguserscroll (view view, final int type, final float p1x, final float p1y, final float p2x, final f Loat p2y) {
LOGUTIL.E (Tag_point, "simulating the sliding screen operation:p1->" + p1x + "," + p1y + ";p 2->" + p2x + "," + p2y);
if (view = = null) {
Return
}
Long downtime = Systemclock.uptimemillis ();//Analog Press down time

Long eventtime = downtime;

float PX = p1x;
float PY = p1y;
int speed = 0;//Fast Slide
float Touchtime = 116;//number of touch events occurred while simulating sliding

The average distance to move each event
float Perx = (p2x-p1x)/touchtime;
float Pery = (p2y-p1y)/touchtime;

Boolean isreversal = Perx < 0 | | Pery < 0;//to determine whether to reverse: swipe your finger from bottom to top, or swipe your finger from right to left
Boolean Ishandy = Math.Abs (Pery) > Math.Abs (Perx);//Decide whether to swipe left or right or swipe up or down

if (type = = User_touch_type_1) {//Accelerated swipe
Touchtime = 10;//If it is a quick swipe, the touch event that occurs is less than even sliding
Speed = Isreversal? -20:20;//reverse movement, the coordinates are decremented each time
}

Impersonate the user by pressing
Motionevent downevent = Motionevent.obtain (Downtime, eventtime,
Action_down, PX, PY, 0);
View.ontouchevent (downevent);

Simulating events during a move
list<motionevent> moveevents = new arraylist<> ();
Boolean isskip = false;
for (int i = 0; i < touchtime; i++) {

PX + = (Perx + speed);
PY + = (Pery + speed);
if ((Isreversal && PX < P2X) | | (!isreversal && PX > P2X)) {
PX = P2X;
Isskip =!ishandy;
}

if ((Isreversal && PY < p2y) | | (!isreversal && PY > P2Y)) {
PY = P2Y;
Isskip = Ishandy;
}
Eventtime + = 20.0f;//Event time is increasing
Motionevent moveevent = getmoveevent (Downtime, eventtime, PX, PY);
Moveevents.add (moveevent);
View.ontouchevent (moveevent);
if (type = = User_touch_type_1) {//Accelerated swipe
Speed + = (isreversal -70:70);
}
if (Isskip) {
Break
}
}

Analog Finger off screen
Motionevent upevent = Motionevent.obtain (Downtime, eventtime,
Motionevent.action_up, PX, PY, 0);
View.ontouchevent (upevent);

Reclaim Touch Events
Downevent.recycle ();
for (int i = 0; i < moveevents.size (); i++) {
Moveevents.get (i). Recycle ();
}
Upevent.recycle ();
}

Android code to simulate user clicks, swipe and other operations

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.