【问题标题】:How calc X, Y coordinates to simulate touch on screen?如何计算 X、Y 坐标来模拟屏幕触摸?
【发布时间】:2018-08-20 06:10:09
【问题描述】:

在我的智能手机android远程协助项目中,我正在尝试基于Image组件(Delphi)内的鼠标坐标模拟屏幕触摸(使用AccessibilityService),这就是我接收屏幕的地方远程设备。

Image 组件位于 ScrollBox 组件内

  • Image 组件是:对齐:alNone,AutoSize:True
  • ScrollBox 是:对齐:alClient,AutoScroll:True,AutoSize: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
}

但触摸发生在非常遥远的地方。

如何解决这个问题?

【问题讨论】:

  • 请注意,这些操作不会改变 X 和 Y:X := (X * Image1.Width) div Image1.Width;。期望的结果是什么?什么坐标?
  • @MBo,我想要那个,例如:当我点击 WhatsApp 图标时,智能手机上的触摸也会出现在 whatsapp 图标上。这是希望的结果。
  • 那么,没有人知道如何计算这些坐标以在我用鼠标指向TImage组件的同一位置触摸智能手机屏幕上(上面的Delphi应用程序截图)?

标签: android delphi delphi-10-seattle accessibilityservice


【解决方案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;

【讨论】:

    猜你喜欢
    • 2015-12-28
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2017-03-04
    相关资源
    最近更新 更多