Skip to main content
 首页 » 编程设计

java之第一次和第二次 ACTION_DOWN 之间耗时

2024年07月26日40davidwang456

- 我想通过这个问题完成的主要是区分单击和双击,我想根据单击/双击执行操作

我正在尝试获取第一个和下一个之间耗时 MotionEvent.ACTION_DOWN .我看过this他在哪里问如何获得MotionEvent.ACTION_DOWN之间耗时的问题和 MotionEvent.ACTION_UP使用 System.nanoTime();我的问题不一样...

Android 图像查看器具有此功能,当您双击标签时 - 它会放大。当您单击时 - 它会隐藏工具。我认为这是通过测量第一个和下一个之间的时间来完成的 ACTION_DOWN

这是我目前拥有的:

ImageView.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 
            if(!gone){ 
                hide.setVisibility(View.GONE); 
                gone = true; 
            }else{ 
                hide.setVisibility(View.VISIBLE); 
                gone = false; 
            } 
        } 
        return true; 
    } 
}); 

上面的代码将在每次点击时隐藏 View ,无论耗时如何。

我正在寻找一种方法来测量第一个 MotionEvent.ACTION_DOWN 之间耗时和下一个 MotionEvent.ACTION_DOWN , 有什么想法吗?


我做过的研究:

和无数的堆栈溢出问题。

我仍然找不到我要找的东西。


编辑 1:

好的,我正在使用 this处理 ImageView 缩放的库,但该库不支持单个选项卡(我找不到说明它可以的文档)。现在我正试图通过检测一次点击来解决这个问题。这就是我现在拥有的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    ImageHolder = (PhotoView) findViewById(R.id.ImageHolder); 
    ImageHolder.setImageURI(myUri); 
 
    ImageHolder.setOnTouchListener(new View.OnTouchListener() { 
        long lastDown = 0; 
        @Override 
        public boolean onTouch(View view, MotionEvent motionEvent) { 
            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 
                long now = System.currentTimeMillis(); 
                if(now - lastDown < 500) {// the second touch was 500ms less ago 
                    Toast.makeText(getApplicationContext(), "Double Tap", Toast.LENGTH_LONG).show(); 
 
                } 
 
                    lastDown = now; 
 
            } 
            return true; 
        } 
    }); 

这可以很好地检测第一个和下一个之间的时间 ACTION_DOWN , 但它覆盖了库的功能,因此在实现此功能时双击缩放不起作用。


编辑 2:

我找到了 this其中包括以下手势类,发布太大所以请看HERE .

在信息中,那个人说要使用以下内容来检测单击:

img.setOnTouchImageViewListener(new TouchImageView.OnTouchImageViewListener() { 
 
        @Override 
        public void onMove() { 
            Toast.makeText(getApplicationContext(), "Single", Toast.LENGTH_LONG).show();                
 
        } 
    }); 

但是我还是有同样的问题,无法区分单击和双击。即使我双击也可以单击。

我也看到了this问题,但仍然没有运气。

请您参考如下方法:

为此,您需要创建自定义手势检测器并为单击、双击、长按初始化您的界面。为了简单起见, 这是您的代码:

img.setOnTouchListener(new MyTouchDetector(mContext,this)); 
 
 
public class MyTouchDetector extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener { 
    int position; 
    private Context mContext; 
    private GestureDetector mGestureDetector; 
    private ClickDetector clickDetector; 
 
 
 
    public MyTouchDetector(Context mContext, ClickDetector clickDetector) { 
        this.mGestureDetector = new GestureDetector(mContext, this); 
        this.clickDetector = clickDetector; 
    } 
    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
        Log.d("onDoubleTap :", "" + e.getAction()); 
        clickDetector.doubleClick(); 
        return super.onDoubleTap(e); 
    } 
 
    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
        Log.d("onSingleTap :", "" + e.getAction()); 
        clickDetector.singleClick(); 
        return super.onSingleTapConfirmed(e); 
    } 
 
    @Override 
    public boolean onDown(MotionEvent e) { 
        //return super.onDown(e); 
        return true; 
    } 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
        return mGestureDetector.onTouchEvent(event); 
    } 
    @Override 
    public void onLongPress(MotionEvent e) { 
        super.onLongPress(e); 
        Log.d("onLongPress :", "" + e.getAction()); 
 
    }