Android OnTouch操作并不会随着扫地而发生

时间:2015-07-30 11:57:58

标签: java android swipe ontouchlistener

我在gridview中的imageview上设置了一个OnTouchListener,因此每次点击“ACTION DOWN”时都会触发,我会更改其点击版本图像的背景,因此它会模拟点击并在“动作”上进行操作我退回旧的

当我点击它时一切正常但是当我稍微滑出边界时UP事件不会发生并且图像保持不变。这对我来说似乎合乎逻辑,因为我没有释放触摸,但我将它滑到imageview之外。我试图限制背景更改只有在点击位于视图范围内时才会发生,但没有任何效果,这是我的触摸侦听器事件:

imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                    // Log.i("IMAGE", "motion event: " + imageView.toString());
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                       ((ImageView) view).setImageResource(R.drawable.test01);
                       Log.i("IMAGE", "motion event: " + "Action down Inside");

                        //imageView.setBackgroundResource(0);
                        Log.i("IMAGE", "motion event: " + "Action down");
                        return true;
                    case MotionEvent.ACTION_UP:
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                        ((ImageView)view).setImageResource(mThumbIds[position]);
                        Log.i("IMAGE", "motion event: " + "Action up");
                        return true;

                }
                return false;
            }

        });

这是另一个我试过别的东西的版本:

imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                    // Log.i("IMAGE", "motion event: " + imageView.toString());
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i("IMAGE", "xC: " + (int)motionEvent.getX() + "   yC:" + (int)motionEvent.getX());
                        if(isViewContains(view, (int)motionEvent.getX(), (int)motionEvent.getY())) {
                            ((ImageView) view).setImageResource(R.drawable.test01);
                            Log.i("IMAGE", "motion event: " + "Action down Inside");
                        }
                        //imageView.setBackgroundResource(0);
                        Log.i("IMAGE", "motion event: " + "Action down");
                        return true;
                    case MotionEvent.ACTION_UP:
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                        ((ImageView)view).setImageResource(mThumbIds[position]);
                        Log.i("IMAGE", "motion event: " + "Action up");
                        return true;
                }
                return true;
            }
        });

这是我正在使用的功能

private boolean isViewContains(View view, int rx, int ry) {
        int[] l = new int[2];
        view.getLocationOnScreen(l);
        Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        Log.i("IMAGE", "x: " + rx + "  y:" + ry);
        if(rect.contains(view.getLeft()+ rx, view.getTop() + ry)) {
                Log.i("IMAGE", "contains");
        }
        return rect.contains(view.getLeft()+ rx, view.getTop() + ry);
}

您有任何提示或建议吗?我想要的是无论我做什么刷卡或点击原始图像返回。

1 个答案:

答案 0 :(得分:0)

onTouch中尝试 MotionEvent.ACTION_CANCEL (查看视图,MotionEvent motionEvent)