如何在setOnLongClickListener中获取onTouch x和y值?

时间:2015-05-05 11:23:17

标签: android

运行时间我更改了背景图像(设置背景)。在图像中,我使用Touch Listener执行操作。但就我而言,我需要对Touch事件采取另一种行动。为此,我需要继续进行长时间触摸事件。建议我,任何其他想法。

2 个答案:

答案 0 :(得分:2)

回答here

final Handler handler = new Handler(); 
Runnable mLongPressed = new Runnable() { 
    public void run() { 
        Log.i("", "Long press!");
        if(myEvent!=null)
        {
           int requiredXvalue=myEvent.getX();               
           int requiredYvalue=myEvent.getY();

        }
    }   
};
MotionEvent myEvent;
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView){
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        handler.postDelayed(mLongPressed, 1000);
        myEvent=event;
    }
    if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
        handler.removeCallbacks(mLongPressed);
    return super.onTouchEvent(event, mapView);
}

你可以使用Gesture Detector作为回答here

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    public void onLongPress(MotionEvent e) {
        Log.e("", "Longpress detected");
        int requiredXvalue=e.getX();               
        int requiredYvalue=e.getY();
    }
});

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
};

答案 1 :(得分:0)

         imageButton.setOnLongClickListener(new OnLongClickListener() { 

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