如何在ImageView上滑动手指时找到触摸事件

时间:2013-12-31 11:24:22

标签: android

如何在图像上滑动手指时找到触摸事件。

相对布局包含多个图像视图。它们是随机放置的。当我在屏幕上滑动手指时,我想捕获用户越过手指的图像视图。

我能够捕获相对布局触摸事件请提示一些捕获图像视图的方法 在相对布局中触摸了滑动动作。

附加图片布局以便更好地理解。

enter image description here

CODE:

public class MainActivity extends Activity {

    ImageView imageView;
    Rect imageViewArea;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView) findViewById(R.id.imageView);
        imageViewArea = new Rect();
        //imageView.getGlobalVisibleRect(imageViewArea);


        //not working
        imageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        });
    }

    //working
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch(event.getActionMasked())
        {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            Log.i("log", "ACTION_MOVE");

            int x = (int) event.getX();
            int y = (int) event.getY();

            //not working 
            if(imageViewArea.contains(x, y)) {
                Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
            }

            break;
        case MotionEvent.ACTION_UP:
            break;
        default:
            break;
        }
        return super.onTouchEvent(event);
    }

2 个答案:

答案 0 :(得分:3)

此示例代码可以帮助您......

    final RelativeLayout layout  =(RelativeLayout) findViewById(R.id.detailsView);
    final ImageView imageView = (ImageView) findViewById(R.id.imageView);
    final Rect imageViewArea = new Rect();
    imageView.getGlobalVisibleRect(imageViewArea);
    layout.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            if(imageViewArea.contains(x, y)) {
                // swipe is passing over ImageView....
            }
            return true;
        }
    });

编辑:

private ImageView imageView;
private Rect imageRect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView1);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    if (imageRect == null) {
        imageRect = new Rect();
        imageView.getGlobalVisibleRect(imageRect);
    }
    int x = (int) event.getX();
    int y = (int) event.getY();
    if (imageRect.contains(x, y)) {
        Log.i(TAG, "touch passing over imageView");
    }
    return true;
}

答案 1 :(得分:0)

使用覆盖onTouch方法

OnTouchListener设置为镜像或创建自己的ImageView类