以编程方式触发ontouch事件 [英] trigger ontouch event programmatically
问题描述
我要做的是,当我从arduino(通过蓝牙)接收到特定的字符串时,在活动的特定区域触发ontouch事件.我试图用wii和arduino构建一个控制器,以便在我正在编写自己的游戏中使用.
what i'm trying to do is trigger an ontouch event at a specific region on my activity when i receive a specific string from an arduino(via bluetooth). i trying to build a controller with a wii and an arduino to use in a game i'm writing myself.
在回答这个问题之前,我已经知道有一个函数 * openButton.performClick();.为此,但在游戏中我并不总是要使用按钮,因此效果不好.
before you answer this , i already know that there is a function * openButton.performClick(); for this but in the game i'm not always going to be using buttons so its not good .
我想模拟adb像猴子那样的触摸,但没有root许可 像注入触摸之类的东西只是没有根.
i want to simulate a touch like the the adb does it with monkey, but without root permission something like inject touch just no root.
这是代码的一部分:
int[] loc = new int[2];
openButton.getLocationOnScreen(loc);// get the region where to simulate the //touch event
xTest = (float)loc[0];
yTest = (float)loc[1];
@SuppressLint("Recycle") void testing(){
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = xTest;
float y = yTest;
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
dispatchTouchEvent(motionEvent);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
// do something
openButton.performClick();
break;
case MotionEvent.ACTION_MOVE:
// do something
// how to trigger a ACTION_DOWN event here?
break;
}
return false;
}
所以我们得到了一个特定的字符串,我想在屏幕上的特定区域以编程方式模拟一个toucht事件,这次是一个按钮,但这可能就是其他原因,这就是为什么"view.performclick()"不好的原因. 我看过一些例子,在他们将dispatchTouchEvent放在视图之前,如果这样做,我会得到一个错误 预先感谢您的帮助
so wenn i get a specific string i want to simulate a toucht event programmatically at a specific region on the screen, this time its a button , but it could be something else thats why "view.performclick()" is not good. I've seen some examples where before the dispatchTouchEvent they put view, if i do this i get an error thanks you in advance for your help
推荐答案
如果您使用的是onTouchListener,则onTouch(...)方法是公共的,因此我想您可以调用它.
If you're using an onTouchListener, the onTouch(...) method is public so I guess you could call it.
因此,当您实例化onTouchListener时,将其存储为变量.
So when you instantiate the onTouchListener store it as a variable.
OnTouchListener listener = new OnTouchListener() { [[override code etc.]] }
然后只调用您已重写的onTouch方法,并为其提供您要模拟触摸的视图和事件.
Then just call the onTouch method you've overridden and give it the view and event you want to simulate the touch on.
MotionEvent没有承包商,但是有一个工厂方法叫做get():
MotionEvent doesn't have a contractor, but has a factory method called obtain():
MotionEvent myEvent = MotionEvent.obtain(long downTime, long eventTime, int action, float x, float y, int metaState);
然后将其传递给您的onTouch:
Then just pass it to your onTouch:
onTouch(myView, myEvent);
假设您已将正确的值传递给事件,则不会知道两者之间的区别.
Assuming you've passed the correct values to your event, it won't know the difference.
有关MotionEvent的更多信息以及参数的含义,请访问:
For more on the MotionEvent and what the parameters mean, go to:
http://developer.android.com/reference/android/view/MotionEvent.html
谢谢!
这篇关于以编程方式触发ontouch事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!