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

标签 java android android-gesture

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

我正在尝试获取第一个和下一个之间耗时 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());

    } 

关于java - 第一次和第二次 ACTION_DOWN 之间耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609013/

相关文章:

java - 如何使用 ASM 创建局部变量?

java - 如何强制 Eclipse 换行?

android - 如何强制 CollapsingToolbarLayour 只有两种状态(展开和折叠)?

java - 使用 java api 在 Neo4j 中插入节点时性能不佳

java - IBM iSeries 上的 RPG 到 Java 迁移

php - 如何通过网站生成android应用程序?

android - SQLite异常: No such table found whereas a table was created

android - Android 实现不带滑动抽屉的滑动菜单

android - Android 的边框手势

android - 如何处理ListView Item LongPress手势