OnTouch MotionEvent行动将不会触发 [英] OnTouch MotionEvent Action Move not triggering
问题描述
我试图让X,Y,而我拖着键的同时拖动,即使trothing我坐标不断更新,但由于某些原因,code波纹管连接在触摸监听器触发只有两次,而不是将得到满足,以提高性能,但正如我所说, onTouch
方法触发只是动作下来,一个更多的时间之后。
这是我的code:
最后按钮dragBtn =(按钮)findViewById(R.id.MyTestButton);
dragBtn.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent我){
// TODO自动生成方法存根
ClipData数据= ClipData.newPlainText(,);
View.DragShadowBuilder阴影=新View.DragShadowBuilder(dragBtn);
v.startDrag(数据,阴影,NULL,0);
INT行动= me.getAction();
如果(动作== MotionEvent.ACTION_DOWN){
Log.i(TEST,ACTION_DOWN);
返回true;
}
如果(动作== MotionEvent.ACTION_MOVE){
Log.i(TEST,ACTION_MOVE);
浮X = me.getX();
浮动Y = me.getY();
// TODO:我的code在这里休息。
返回true;
}
如果(动作== MotionEvent.ACTION_UP){
Log.i(测试,ACTION_UP);
返回true;
}
Log.i(TEST,UKNOWN ACTION);
返回true;
}
});
和日志我刚:
ACTION_DOWN
窗口管理:将已在进行中
UKNOWN行动
您想捕捉的X和Y坐标数据在你的 OnDragListener
不是你 OnTouchListener
。对于要拖动对象在屏幕上的全部时间,Android的跨$ P $其中pts,作为一个单一的ACTION_MOVE事件,因此你只能从获取一次数据。将下面的code自定义里面 OnDragListener
类:
@覆盖
公共ondrag当布尔(视图V,的dragEvent事件){
INT行动= event.getAction();
浮动XCOORD;
浮动YCOORD;
如果(动作== DragEvent.ACTION_DRAG_LOCATION){
XCOORD = event.getX();
YCOORD = event.getY();
}
返回event.getResult(); //返回真有效降还是假的无效降
}
在其他地方,在您的自定义 OnDragListener
类,你将不得不做一些与XCOORD和YCOORD数据。例如,在我的应用程序,我有一个的TextView
我的调试版本,我显示XCOORD和YCOORD数据的应用程序中。我执行 TextView.setText()
方法之后,我得到这两个值在我的如果(动作== DragEvent.ACTION_DRAG_LOCATION){...
部分。
希望这有助于。
I'm trying to get X, Y coordinates while I'm dragging my button, but for some reason, the code bellow attached on touch listener triggers only twice instead of constant update while dragging, even with trothing I will be satisfied in order to improve performance, but as I said, onTouch
method triggered just on action down, and one more time later.
This is my code:
final Button dragBtn = (Button) findViewById(R.id.MyTestButton);
dragBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragBtn);
v.startDrag(data, shadow, null, 0);
int action = me.getAction();
if (action==MotionEvent.ACTION_DOWN) {
Log.i("TEST", "ACTION_DOWN");
return true;
}
if (action==MotionEvent.ACTION_MOVE){
Log.i("TEST", "ACTION_MOVE");
float x = me.getX();
float y = me.getY();
//TODO: Rest of my code here.
return true;
}
if (action==MotionEvent.ACTION_UP){
Log.i("TEST", "ACTION_UP");
return true;
}
Log.i("TEST", "UKNOWN ACTION");
return true;
}
});
And in log I have just:
ACTION_DOWN
WindowManager﹕ Drag already in progress
UKNOWN ACTION
You want to capture the X and Y coordinate data in your OnDragListener
not your OnTouchListener
. For the whole time that you are dragging the object around the screen, Android interprets that as one single ACTION_MOVE event, hence you only get data once from that. Place the following code inside your custom OnDragListener
class:
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
float xCoord;
float yCoord;
if(action == DragEvent.ACTION_DRAG_LOCATION) {
xCoord = event.getX();
yCoord = event.getY();
}
return event.getResult(); //returns true for valid drop or false for invalid drop
}
Somewhere else in your custom OnDragListener
class, you will have to do something with that xCoord and yCoord data. For example in my app, I have a TextView
on my "debug" version of the app where I display the xCoord and yCoord data. I execute the TextView.setText()
method right after I get those two values in my if(action == DragEvent.ACTION_DRAG_LOCATION){...
section.
Hope this helps.
这篇关于OnTouch MotionEvent行动将不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!