Android 为多个 ImageView 触发 onTouch 事件
我有几个 ImageView,当我在多个图像上拖动手指时,我希望为每个 ImageView 触发 onTouch 事件。目前,onTouch 事件仅在第一个 ImageView 上触发(或者实际上在多个 ImageView 上触发,但仅在多点触摸屏幕时触发)。伪代码:
for(int i=0;i<5;i++){
ImageView img=new ImageView(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width,height);
img.setImageResource(R.drawable.cell);
img.setOnTouchListener(this);
mainLayout.addView(img,layoutParams);
}
...
public boolean onTouch (View v, MotionEvent event){
Log.d("MY_APP","View: " + v.getId());
return false;
}
我是不是完全找错了树?
感谢您的任何帮助。
I have several ImageViews, and I want the onTouch event to fire for each of them when I drag my finger across multiple images. At present the onTouch event is only firing on the first ImageView (or actually on multiple ImageViews but only when multitouching the screen). Pseudo code:
for(int i=0;i<5;i++){
ImageView img=new ImageView(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width,height);
img.setImageResource(R.drawable.cell);
img.setOnTouchListener(this);
mainLayout.addView(img,layoutParams);
}
...
public boolean onTouch (View v, MotionEvent event){
Log.d("MY_APP","View: " + v.getId());
return false;
}
Am I barking up completely the wrong tree?
Thanks for any help.
我认为您需要使用移动事件而不是触摸事件,并将 getX 和 getY 与视图的位置进行比较。
I think you need to use move event rather than touch event and compare getX and getY with the location of your views.