Flick
package io.appium.android.bootstrap.handler;import com.android.uiautomator.core.UiDevice;import io.appium.android.bootstrap.*;import io.appium.android.bootstrap.exceptions.InvalidCoordinatesException;import io.appium.android.bootstrap.utils.Point;import org.json.JSONException;import java.util.Hashtable;/** * This handler is used to flick elements in the Android UI. * * Based on the element Id, flick that element. * */public class Flick extends CommandHandler { private Point calculateEndPoint(final Point start, final Integer xSpeed, final Integer ySpeed) { final UiDevice d = UiDevice.getInstance(); final Point end = new Point(); final double speedRatio = (double) xSpeed / ySpeed; double xOff; double yOff; final double value = Math.min(d.getDisplayHeight(), d.getDisplayWidth()); if (speedRatio < 1) { yOff = value / 4; xOff = value / 4 * speedRatio; } else { xOff = value / 4; yOff = value / 4 / speedRatio; } xOff = Integer.signum(xSpeed) * xOff; yOff = Integer.signum(ySpeed) * yOff; end.x = start.x + xOff; end.y = start.y + yOff; return end; } /* * @param command The {@link AndroidCommand} used for this handler. * * @return {@link AndroidCommandResult} * * @throws JSONException * * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. * bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { Point start = new Point(0.5, 0.5); Point end = new Point(); Double steps; final Hashtable<String, Object> params = command.params(); final UiDevice d = UiDevice.getInstance(); if (command.isElementCommand()) { AndroidElement el; try { el = command.getElement(); start = el.getAbsolutePosition(start); final Integer xoffset = (Integer) params.get("xoffset"); final Integer yoffset = (Integer) params.get("yoffset"); final Integer speed = (Integer) params.get("speed"); steps = 1250.0 / speed + 1; end.x = start.x + xoffset; end.y = start.y + yoffset; } catch (final Exception e) { return getErrorResult(e.getMessage()); } } else { try { final Integer xSpeed = (Integer) params.get("xSpeed"); final Integer ySpeed = (Integer) params.get("ySpeed"); final Double speed = Math.min(1250.0, Math.sqrt(xSpeed * xSpeed + ySpeed * ySpeed)); steps = 1250.0 / speed + 1; start = getDeviceAbsPos(start); end = calculateEndPoint(start, xSpeed, ySpeed); } catch (final InvalidCoordinatesException e) { return getErrorResult(e.getMessage()); } } steps = Math.abs(steps); Logger.debug("Flicking from " + start.toString() + " to " + end.toString() + " with steps: " + steps.intValue()); final boolean res = d.swipe(start.x.intValue(), start.y.intValue(), end.x.intValue(), end.y.intValue(), steps.intValue()); if (res) { return getSuccessResult(res); } else { return getErrorResult("Flick did not complete successfully"); } }}
The Code steps are similar to swipe and finally call the uidevice. Swipe method. Let's take a look at the difference. First, it is divided into controls and coordinates for analysis:
Widget
First, the center of the control is used as the starting coordinate, and then the displacement data is obtained based on the number of degrees xoffset and yoffset provided. The speed limit number is used to calculate the step.
steps = 1250.0 / speed + 1;end.x = start.x + xoffset;end.y = start.y + yoffset;
The starting coordinate plus the displacement is the ending coordinate. This steps setting is still a bit confusing. I think this 1250 is the maximum displacement, and speed represents the path of each step. Use 1250/speed to get the number of steps used to the end point, and add the initial value to get the value of steps. The start point coordinates, end point coordinates, and step values are all set.
Coordinates
Strictly speaking, it cannot be said that the coordinates should be regarded as the coordinate displacement, because the number of measures passed in is actually the speed xspeed and yspeed of the coordinate system. The X axis moves the xspeed distance, and the Y axis moves the yspeed coordinate. Then obtain the coordinate value and steps value.
It performs a ratio of 1250 and the square of the displacement, and obtains the minimum value to calculate steps. Start Coordinate Position: coordinates of the center of the screen. Then call end = calculateendpoint (START, xspeed, yspeed) to obtain the coordinates of the end point.
private Point calculateEndPoint(final Point start, final Integer xSpeed, final Integer ySpeed) { final UiDevice d = UiDevice.getInstance(); final Point end = new Point(); final double speedRatio = (double) xSpeed / ySpeed; double xOff; double yOff; final double value = Math.min(d.getDisplayHeight(), d.getDisplayWidth()); if (speedRatio < 1) { yOff = value / 4; xOff = value / 4 * speedRatio; } else { xOff = value / 4; yOff = value / 4 / speedRatio; } xOff = Integer.signum(xSpeed) * xOff; yOff = Integer.signum(ySpeed) * yOff; end.x = start.x + xOff; end.y = start.y + yOff; return end; }
First, calculate the displacement ratio speedratio (X's displacement/y's displacement), then obtain the screen width and the smallest number of high school copies to the value. assume that speedratio is <1, and the moving distance of X is 1/4 of the value. the Y coordinate is the same as that of X. Therefore, the coordinates of the ending point and the starting point obtained through the above calculation should be the diagonal line of the positive square.
The call to uidevice. Swipe is the same as that in swipe. Nothing special
Summary
Especially want to know What 1250 represents. Otherwise, I still don't think I understand the meaning of this method. Ah