我创建了这个小程序,向用户显示屏幕上的手指,但问题是其他手指的X,Y值没有被修改。我做错了什么?
谢谢!
public class TestandoActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
private TextView txtV;
private TextView txtV2;
private int nf = 0;
private Map<Integer, String> info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
info = new HashMap<Integer, String>();
setContentView(R.layout.main);
this.txtV = (TextView)this.findViewById(R.id.textview);
this.txtV2 = (TextView)this.findViewById(R.id.textview2);
this.txtV.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
int actionCode = event.getAction() & MotionEvent.ACTION_MASK;
int pid = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
info.put(pid, pid + ": X=" + event.getX() + " Y=" + event.getY() + " pressure=" + event.getPressure() + " size=" + event.getSize());
if (actionCode == MotionEvent.ACTION_POINTER_UP || actionCode == MotionEvent.ACTION_UP)
info.remove(pid);
String total = "";
for (Map.Entry<Integer, String> e : this.info.entrySet()) {
total += e.getValue() + "\n";
}
this.txtV2.setText(total);
return true;
}
}
答案 0 :(得分:1)
此问题的主要原因是您使用了getX()和getY()方法。
getX()方法总是返回第一个指针的x位置,getY()返回其y位置。
如果你想在屏幕上获得其他手指的X,Y值,你必须使用这些方法:
getX(int pointerId) //get #pointerId pointer's x value
getY(int pointerId) //get #pointerId pointer's y value
其他手指'pointerId可以通过您使用的方式找到:
int pid = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
或使用指针索引获取ID:
getPointerId (int pointerIndex) //pointerIndex is from 0 to getPointerCount()-1
我希望它会对你有所帮助。 :)