如何计算X,Y坐标以模拟屏幕上的触摸?

时间:2018-08-20 06:10:09

标签: android delphi delphi-10-seattle accessibilityservice

在我的智能手机android远程增强项目中,我正在尝试根据Image组件(Delphi)内的鼠标坐标模拟屏幕上的触摸(使用AccessibilityService)。接收远程设备的屏幕。

enter image description here

  

Image组件位于ScrollBox组件内部

  • Image组件是:对齐: alNone ,自动调整大小: True
  • ScrollBox是:对齐: alClient ,自动滚动: True ,自动大小: False

Image组件的重新命名代码:

procedure TForm2.Checkbox1Click(Sender: TObject);
begin
  if Checkbox1.Checked then
  begin
    Image1.AutoSize := false;
    Image1.Stretch := true;
    Image1.Align := alClient;
  end
  else
  begin
    Image1.AutoSize := true;
    Image1.Stretch := false;
    Image1.Align := alNone;
  end;
end;

这是我发送坐标的方式:

procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin

  X := (X * Image1.Width) div Image1.Width;
  Y := (Y * Image1.Height) div Image1.Height;

  SS1.Socket.Connections[Index].SendText('touch' + IntToStr(X) +
    '<|>' + IntToStr(Y) + #13#10);
end;

然后我在Java代码(android)上尝试过此操作:

String xline;

if (xline.contains("touch")) {

    String coordinates = xline.replace("touch", "");

    String[] tokens = coordinates.split(Pattern.quote("<|>"));

    float x = parseFloat(tokens[0]);
    float y = parseFloat(tokens[1]);

    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    int screenHeight = getResources().getDisplayMetrics().heightPixels;

    x = (x * screenWidth) / screenWidth;
    y = (y * screenHeight) / screenHeight;

    // touch on screen with x and y
}

但是触摸发生在很远的地方。

如何解决?

1 个答案:

答案 0 :(得分:1)

找到了解决方案,引用为this answer

procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Index, XTouch, YTouch, RXCoord, RYCoord: Integer;
  List: TStrings;
  RScreen: String;
begin
  Index := Form1.ListView1.ItemIndex;
  if Index = -1 then
    Exit;

  List := TStringList.Create;
  RScreen := Form1.ListView1.Selected.SubItems[6];

  try
    ExtractStrings(['x'], [], PChar(RScreen), List); // Ex: my smartphone is 1920x1080
    RYCoord := StrToInt(List[0]); // 1920 (height)
    RXCoord := StrToInt(List[1]); // 1080 (width)
  finally
    List.Free;
  end;

  XTouch := Round((X / Image1.Width) * RXCoord);
  YTouch := Round((Y / Image1.Height) * RYCoord);

  Form1.SS1.Socket.Connections[Index].SendText('touch' + IntToStr(XTouch)
    + '<|>' + IntToStr(YTouch) + #13#10);
end;
相关问题