问题
I want know how to perform a drag on android based in X, Y mouse coordinates? consider as two simple examples, the Team Viewer/QuickSupport drawing the "password pattern" on remote smartphone and the Pen of Windows Paint respectively.
All that i'm able of make is simulate touch (with dispatchGesture()
and also AccessibilityNodeInfo.ACTION_CLICK
).
I found these relevants links, but not know if they can be useful:
- Perform swipe on screen using AccessibilityService
- Example 1
- Continued gestures
Below is my working code that is used to send mouse coordinates (inside of PictureBox
control) to remote phone and simulate touch.
Windows Forms Application:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
foreach (ListViewItem item in lvConnections.SelectedItems)
{
// Remote screen resolution
string[] tokens = item.SubItems[5].Text.Split('x'); // Ex: 1080x1920
int xClick = (e.X * int.Parse(tokens[0].ToString())) / (pictureBox1.Size.Width);
int yClick = (e.Y * int.Parse(tokens[1].ToString())) / (pictureBox1.Size.Height);
Client client = (Client)item.Tag;
if (e.Button == MouseButtons.Left)
client.sock.Send(Encoding.UTF8.GetBytes("TOUCH" + xClick + "<|>" + yClick + Environment.NewLine));
}
}
Edit:
My last attempt was a "swipe screen" using mouse coordinates (C# Windows Forms Application) and a custom android routine (with reference to code of "swipe screen" linked above), respectively:
private Point mdownPoint = new Point();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
foreach (ListViewItem item in lvConnections.SelectedItems)
{
// Remote screen resolution
string[] tokens = item.SubItems[5].Text.Split('x'); // Ex: 1080x1920
Client client = (Client)item.Tag;
if (e.Button == MouseButtons.Left)
{
xClick = (e.X * int.Parse(tokens[0].ToString())) / (pictureBox1.Size.Width);
yClick = (e.Y * int.Parse(tokens[1].ToString())) / (pictureBox1.Size.Height);
// Saving start position:
mdownPoint.X = xClick;
mdownPoint.Y = yClick;
client.sock.Send(Encoding.UTF8.GetBytes("TOUCH" + xClick + "<|>" + yClick + Environment.NewLine));
}
}
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
foreach (ListViewItem item in lvConnections.SelectedItems)
{
// Remote screen resolution
string[] tokens = item.SubItems[5].Text.Split('x'); // Ex: 1080x1920
Client client = (Client)item.Tag;
if (e.Button == MouseButtons.Left)
{
xClick = (e.X * int.Parse(tokens[0].ToString())) / (pictureBox1.Size.Width);
yClick = (e.Y * int.Parse(tokens[1].ToString())) / (pictureBox1.Size.Height);
client.sock.Send(Encoding.UTF8.GetBytes("MOUSESWIPESCREEN" + mdownPoint.X + "<|>" + mdownPoint.Y + "<|>" + xClick + "<|>" + yClick + Environment.NewLine));
}
}
}
android AccessibilityService:
public void Swipe(int x1, int y1, int x2, int y2, int time) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
System.out.println(" ======= Swipe =======");
GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
Path path = new Path();
path.moveTo(x1, y1);
path.lineTo(x2, y2);
gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path, 100, time));
dispatchGesture(gestureBuilder.build(), new GestureResultCallback() {
@Override
public void onCompleted(GestureDescription gestureDescription) {
System.out.println("SWIPE Gesture Completed :D");
super.onCompleted(gestureDescription);
}
}, null);
}
}
that produces the following result (but still not is able to draw "pattern password" like TeamViewer for example). But like said on comment below, I think that with a similar approach this can be achieved using Continued gestures probably. Any suggestions in this direction will be welcome.
Edit 2:
Definitely, the solution is continued gestures like said on previous Edit.
- Simulating joystick movement using AccessibilityService
- Why the continueStroke function is not work
And below is a supposed fixed code that i found here =>
android AccessibilityService:
// Simulates an L-shaped drag path: 200 pixels right, then 200 pixels down.
Path path = new Path();
path.moveTo(200,200);
path.lineTo(400,200);
final GestureDescription.StrokeDescription sd = new GestureDescription.StrokeDescription(path, 0, 500, true);
// The starting point of the second path must match
// the ending point of the first path.
Path path2 = new Path();
path2.moveTo(400,200);
path2.lineTo(400,400);
final GestureDescription.StrokeDescription sd2 = sd.continueStroke(path2, 0, 500, false); // 0.5 second
HongBaoService.mService.dispatchGesture(new GestureDescription.Builder().addStroke(sd).build(), new AccessibilityService.GestureResultCallback(){
@Override
public void onCompleted(GestureDescription gestureDescription){
super.onCompleted(gestureDescription);
HongBaoService.mService.dispatchGesture(new GestureDescription.Builder().addStroke(sd2).build(),null,null);
}
@Override
public void onCancelled(GestureDescription gestureDescription){
super.onCancelled(gestureDescription);
}
},null);
Then, my doubt is: how send correctly mouse coordinates for code above, of the way that can perform drag to any direction? Some idea?
Edit 3:
I found two routines that are used to perform drag, but they are using UiAutomation + injectInputEvent(). AFAIK, injection of event works only in a system app like said here and here and i not want it.
This are routines found:
- public boolean swipe(int downX, int downY, int upX, int upY, int steps, boolean drag)
- public boolean swipe(Point[] segments, int segmentSteps)
Then to achieve my goal, i think that 2rd routine is more appropriated to use (following the logic, excluding event injection) with code showed on Edit 2 and send all points in pictureBox1_MouseDown
and pictureBox1_MouseUp
(C# Windows Forms Application) respectively to fill Point[]
dynamically and on pictureBox1_MouseUp
send cmd to execute the routine and use this array filled. If you have a idea to 1st routine, let me know :D.
If after read this Edit you have a possible solution, show me in a answer please, while i will try and test this idea.
回答1:
Have you tried to use AutoIt scripting?
You can save coordinates within specific windows/screens. You can hold mouseclick pressed while drawing the pattern.
I also have some example code / scripts for you if you want them!
EDIT:
According to this tutorial you can use Auto-IT on C#.
Follow these steps:
- Install Auto-IT
- Add Auto-IT as reference in reference manager (AutoItX3.dll)
- Then import library that you added with:
Using AutoItX3Lib;
- Make new AutoItX3 object called 'auto':
AutoItX3 auto = new AutoItX3();
- You can now execute Auto It commands.
This is the complete example for executing a mouseclick:
Using AutoItX3Lib;
AutoItX3 auto = new AutoItX3();
auto.MouseClick("left", 78, 1133, 1, 35)
With the AutoIt Window Info Tool
you can check the coordinates you want to use.
Please note there are differences between mouse coordinates modes:
for example: auto.AutoItSetOption("MouseCoordMode", 1)
will use absolute screen coordinates. See source here.
For holding the mouseclick down, you can check MouseDown Function
回答2:
you can view android matrix solution please view a perfect example of a matrix. https://www.dev2qa.com/android-imageview-matrix-rotate-scale-skew-translate-example/
a second example for draggable. https://github.com/kencheung4/android-StickerView
https://github.com/aritraroy/PatternLockView - pattern lock library view these I think you want these.
来源:https://stackoverflow.com/questions/59278085/how-perform-a-drag-based-in-x-y-mouse-coordinates-on-android-using-accessibili