如何计算 X、Y 坐标来模拟屏幕上的触摸?
How calc X, Y coordinates to simulate touch on screen?
在我的智能手机远程协助项目 android 中,我正在尝试根据 Image
组件内的鼠标坐标模拟屏幕上的触摸(使用 )(Delphi), 那是我接收远程设备屏幕的地方。
Image
component is inside a ScrollBox
component
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
}
但是触摸发生在很远的地方。
如何解决这个问题?
找到解决方案,参考为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;
在我的智能手机远程协助项目 android 中,我正在尝试根据 Image
组件内的鼠标坐标模拟屏幕上的触摸(使用
Image
component is inside aScrollBox
component
Image
组件是:对齐:alNone,自动调整大小:TrueScrollBox
是:对齐: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
}
但是触摸发生在很远的地方。
如何解决这个问题?
找到解决方案,参考为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;