这是由于Android模拟器在API 30上的一个已知问题。解决方法是在代码中手动启用多点触摸。下面是代码示例:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private int MAX_TOUCHES = 2 // 设置最大触摸点数,此处为2点触控
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.my_view);
view.setOnTouchListener(this);
// 添加代码以启用多点触摸
view.setClickable(true);
view.setLongClickable(true);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int pointerCount = motionEvent.getPointerCount();
if (pointerCount > MAX_TOUCHES) {
pointerCount = MAX_TOUCHES;
}
for (int i = 0; i < pointerCount; i++) {
int pointerId = motionEvent.getPointerId(i);
int action = motionEvent.getActionMasked();
int x = (int) motionEvent.getX(i);
int y = (int) motionEvent.getY(i);
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
// 添加代码以处理多点触摸按下事件
break;
case MotionEvent.ACTION_MOVE:
// 添加代码以处理多点触摸移动事件
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
// 添加代码以处理多点触摸抬起事件
break;
}
}
return true;
}
});
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// 在此处添加您的触摸事件处理代码
return true;
}
}
下一篇:Android模拟器无法加载