在我的应用程序中,我希望以这种方式在同一视图中管理许多内容:
我想获得在这个视图上方的确切手指数,也就是说,如果我的手指总是应该是1,如果我躺下第二根手指,则总数应为2,等等为3,4, 5 ...(直到我的设备支持其最大触摸次数)。 没关系但是如果我从屏幕上移开一根手指,也就是说,如果我在屏幕上放置两根手指,那么我实时也想要总数,那么总数是2,但是如果我移开一根手指,那么总应该是变成了1。
我创建了这段代码,但它没有正常工作。
touch_view = (View)findViewById(R.id.touch_view);
touch_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN ){
touches++;
Log.i("", "touches "+ touches);
Log.i("", "action down");
}
else if (action == MotionEvent.ACTION_POINTER_DOWN){
touches++;
Log.i("", "touches "+ touches);
Log.i("", "pointer down");
}
else if (action == MotionEvent.ACTION_POINTER_UP){
touches--;
Log.i("", "touches " + touches);
Log.i("", "pointer up");
}
else if (action == MotionEvent.ACTION_UP){
touches--;
Log.i("", "touches " + touches);
Log.i("", "action up");
}
else if (action == MotionEvent.ACTION_CANCEL){
Log.i("", "cancel");
}
else if (action == MotionEvent.ACTION_HOVER_ENTER){
Log.i("", "action over enter");
}
else if (action == MotionEvent.ACTION_HOVER_EXIT){
Log.i("", "action over exit");
}
else if (action == MotionEvent.ACTION_OUTSIDE){
Log.i("", "action outside");
}
else if (action == MotionEvent.ACTION_POINTER_INDEX_MASK){
Log.i("", "index mask");
}
if (action != MotionEvent.ACTION_MOVE){
Log.i("","pointer count"+event.getPointerCount());
Log.i("","action type" +event.getAction());
}
我注意到我的计数"接触"不能很好地工作......有时我的日志" event.getAction()"给我一个奇怪的数字(261,262 ... 517,518 ......)这些数字是什么类型的动作? 你能救我吗?
答案 0 :(得分:0)
我的解决方案
touch_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action != MotionEvent.ACTION_MOVE){
if (action == MotionEvent.ACTION_DOWN ){
current_touches = 1;
Log.i("", "current touches " + current_touches);
}
else if (action == MotionEvent.ACTION_UP){
current_touches = 0;
Log.i("", "current touches " + current_touches);
}
else{
int action_code = event.getAction();
if (action_code%2 == 0){
if (current_touches != 0)
current_touches--;
}
else
current_touches++;
Log.i("", "current touches " + current_touches);
}
}
return true;
}
});