Android 为多个 ImageView 触发 onTouch 事件

发布于 10-20 20:12 字数 727 浏览 4 评论 0原文

我有几个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i2024-10-27 20:12:25

我认为您需要使用移动事件而不是触摸事件,并将 getX 和 getY 与视图的位置进行比较。

     @Override
    public boolean onTouchEvent(MotionEvent ev) {

        final int action = ev.getAction();

        switch (action) {

            // MotionEvent class constant signifying a finger-down event

            case MotionEvent.ACTION_DOWN: {
                break;
            }

            // MotionEvent class constant signifying a finger-drag event  

            case MotionEvent.ACTION_MOVE: {

                    X = ev.getX();
                    Y = ev.getY();
                    //compare here using a loop of your views
                    break;

            }

            // MotionEvent class constant signifying a finger-up event

            case MotionEvent.ACTION_UP:

                break;

        }
        return true;
    }

I think you need to use move event rather than touch event and compare getX and getY with the location of your views.

     @Override
    public boolean onTouchEvent(MotionEvent ev) {

        final int action = ev.getAction();

        switch (action) {

            // MotionEvent class constant signifying a finger-down event

            case MotionEvent.ACTION_DOWN: {
                break;
            }

            // MotionEvent class constant signifying a finger-drag event  

            case MotionEvent.ACTION_MOVE: {

                    X = ev.getX();
                    Y = ev.getY();
                    //compare here using a loop of your views
                    break;

            }

            // MotionEvent class constant signifying a finger-up event

            case MotionEvent.ACTION_UP:

                break;

        }
        return true;
    }
天冷不及心凉2024-10-27 20:12:25

是的,我也有同样的问题。答案是为我嵌入 ImageView 的布局创建一个触摸监听器,然后计算 X、Y 位置以了解我在哪个视图之上。
仅针对其连接到的视图调用 OnTouchListener。我的意思是,据我所知,如果调用 ImageView,则在您开始对该 ImageView 进行运动之前,它不会触发。

Yes, I had the same problem. The answer was to create a touch listener for the layout I embed my ImageViews in and then to calculate the X, Y position to know above which of my View am I.
OnTouchListener is called only for the view it is connected to. I mean if it is called for an ImageView it won't fire until you start your motion onto that ImageView, as far as I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文