我被要求在在线课程的一部分中修改现有代码。目前,该应用程序注册并显示屏幕被触摸的位置的x和y坐标以及以毫秒为单位的时间量。
我想添加获取起始和结束坐标的功能,即手指开始和结束拖动的位置。目前,已注册的坐标似乎只是结束坐标。如何添加两组坐标(例如,如果我想计算运动事件的距离)?这是代码的一部分。感谢您的帮助!
我想添加获取起始和结束坐标的功能,即手指开始和结束拖动的位置。目前,已注册的坐标似乎只是结束坐标。如何添加两组坐标(例如,如果我想计算运动事件的距离)?这是代码的一部分。感谢您的帮助!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Calendar tiempoInicio = null;
Calendar tiempoFinal = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
tiempoInicio = Calendar.getInstance();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
tiempoFinal = Calendar.getInstance();
Intent intent = new Intent (
AplicacionSencillaActivity.this,
AplicacionSencillaResults.class);
Bundle bundle = new Bundle();
bundle.putLong(
"TIEMPOPRESIONADO",
tiempoFinal.getTimeInMillis() -
tiempoInicio.getTimeInMillis());
bundle.putInt("X", x);
bundle.putInt("Y", y);
intent.putExtras(bundle);
startActivity(intent);
break;
boolean mIsSwiping
。当你收到ACTION_DOWN
时将其设置为true
并读取起始点坐标。当你收到ACTION_UP
时:如果mIsSwiping
是true
,则读取结束坐标并将mIsSwiping
设置为false
,否则 - 无事可做。 - Ivan BartsovACTION_DOWN
中,他们基本上将mIsBeingDragged
(根据子项计数,但这是因为它是ScrollView
,除非有子项,否则无法滚动)设置为true
,而对于ACTION_UP
或ACTION_CANCEL
,他们将mIsBeingDragged
设置为false。 - Ivan Bartsov