处理onClick()和onTouch()两者,android

时间:2017-02-21 07:24:40

标签: android

我在同一个imageView上执行onClick()和onTouch(),onClick()上的简单打开Gallery,但问题是我的onclick()不起作用,而onTouch()工作正常。任何人都可以清楚我的实际问题。谢谢。这是我的代码..,

boolean isMoved=true;
    Matrix matrix=new Matrix();
    Matrix savedMatrix=new Matrix();
    PointF startPoint=new PointF();
    PointF midPoint=new PointF();
    float oldDist = 1f;
    int NONE = 0;
    int DRAG = 1;
    int ZOOM = 2;
    int mode = NONE;
public boolean onTouch(View v, MotionEvent event) {

        ImageView imageView = (ImageView) v;
        imageView.setScaleType(ScaleType.MATRIX);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:
            matrix.set(imageView.getImageMatrix());
            isMoved = false;
            startPoint.set(event.getX(), event.getY());
            savedMatrix.set(matrix);
            mode = DRAG;
            break;

        case MotionEvent.ACTION_UP:
            if (!isMoved) {
                v.performClick();
            }
            break;

        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            savedMatrix.set(matrix);
            break;

        case MotionEvent.ACTION_MOVE:

            if (mode == DRAG) {
                matrix.set(savedMatrix);
                matrix.postTranslate(event.getX() - startPoint.x, event.getY()
                        - startPoint.y);
            } else if (mode == ZOOM) {

                float newDist = spacing(event);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = newDist / oldDist;
                    matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                }
            }
            isMoved = true;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:

            oldDist = spacing(event);

            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                midPoint(midPoint, event);
                mode = ZOOM;
                isMoved = false;
            }
            break;
        }

        imageView.setImageMatrix(matrix);
        return true;
    }
@SuppressLint("FloatMath")
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

4 个答案:

答案 0 :(得分:2)

onClick()onTouch()都是GesturesDetectors,并且有一个消费系统。每次在设备的触觉上制作手势时,将逐个调用附加到此特定视图的所有GesturesDetectors(onClick()onTouch()onDoubleTap() ...)。如果在GestureDetector的末尾返回true,那么事件就会消失,其他的GestureDetector将不会被调用。如果您返回false,则不会消除该事件并调用下一个GestureDetector。

因此,要在onClick()之后调用onTouch(),请务必在结尾处返回false。

答案 1 :(得分:0)

你还没有给出你的onclick代码,所以我认为没问题。 您正在返回 true 从ontouch返回 false 那里它将起作用 当你返回true然后onClick方法不执行如果你想执行onclick也然后你必须从ontouch返回false。

答案 2 :(得分:0)

您可以使用以下逻辑来区分onTouch()onClick()

宣告:

private static final int DurationOfClick = 2000;  //duration of tap on screen
private long clickTime;

现在在onTouch(View v, MotionEvent event)写条件..

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            clickTime = Calendar.getInstance().getTimeInMillis();
            break;
        }
        case MotionEvent.ACTION_UP: {
            long duration = Calendar.getInstance().getTimeInMillis() - clickTime;
            if(duration < DurationOfClick) {
                //click event has occurred write ur code
            }else{
                //touch event has occurred write ur code
            }
        }
    }
    return true;
}

答案 3 :(得分:0)

下面的thnik代码将对您有所帮助,请尝试此操作

Uncaught TypeError: Cannot read property 'reload' of undefined
at reloadGrid (index:53)
at HTMLSelectElement.onchange (index:1)

或者您可以使用android的 GestureDetector 属性浏览this