作为在线课程的一部分,我被要求修改现有代码。目前,应用程序注册并显示屏幕被触摸位置的 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;