Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

java - Injection of touch events using screen driver

Using android-event-injector library, I wrote an application to inject a touch event when some event is triggered. The problem is that i need to inject touch at absolute coordinates of a given View, so I do the following to get the location on screen:

View v = /* find view*/;
int [] coords = new int[2];
v.getLocationOnScreen(coords);

This gives me the absolute coordinates on screen. The problem is that touch injection doesn't work.

I can inject correctly touches in screen driver, but for some reason the coordinates are misunderstood and touches are injected elsewhere. Here are some examples (my screen is 1024x600 landscape oriented):

  • Coords (0,0) -> Injected in (0,0)
  • Coords (0,600) -> Injected in (0,351)
  • Coords (1024,0) -> Not injected (most likely x is out of range)
  • Coords (1024,600) -> Not injected (most likely x is out of range)
  • Coords (640,480) -> Not injected (most likely x is out of range)
  • Coords (512,300) -> Injected in (872,175)
  • Coords (100,100) -> Injected in (170,58)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on the sample values it appears that the

  1. Touchscreen is (600, 1024),
  2. Mapped to a (1024,600) display.

To generate a touch event at (X,Y)display co-ordinates, the (x,y)touch co-ordinates that need to be injected can be calculated as follows

x(touch) = (600/1024) * X(disp)
y(touch) = (1024/600) * Y(disp)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...