Android - 在Touch上减慢缩放自定义视图的速度

时间:2017-09-07 12:33:52

标签: android view zoom ontouchlistener

我正在开发应用程序,我实现了缩放功能但是当我试图捏缩放时,它是非常快速的缩放。如何减慢或规范此缩放功能? 很高兴有任何帮助。

以下是缩放中使用过的变量的初始化

private static final float MIN_ZOOM = 1f;
private static final float MAX_ZOOM = 4f;
private float scaleAll = 1;

OnTouchListener中用于缩放自定义视图中视图的代码是

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    if (mode != MOVING_MODE) {
        float[] v = new float[9];
        matrix.getValues(v);
        // translation 
        float mScalingFactor = v[Matrix.MSCALE_X];

        RectF r = new RectF();
        matrix.mapRect(r);

        // mScalingFactor shall contain the scale/zoom factor
        float scaledX = (x - r.left);
        float scaledY = (y - r.top);

        scaledX /= mScalingFactor;
        scaledY /= mScalingFactor;

        x = scaledX;
        y = scaledY;
    }

    int maskedAction = event.getActionMasked();
    switch (maskedAction) {
        case MotionEvent.ACTION_DOWN:

            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            touchMode = DRAG;

            if (mode == MOVING_MODE) {
                DownPT.x = event.getX();
                DownPT.y = event.getY();
            }
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:

            if (touchMode == DRAG) {
               if (mode == MOVING_MODE) {
                    PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
                    matrix.postTranslate(mv.x, mv.y);
                    DownPT.x = event.getX();
                    DownPT.y = event.getY();
                } 
                invalidate();
            } else if (touchMode == ZOOM && mode == MOVING_MODE) {
                // pinch zooming
                float newDist = spacing(event);

                if (newDist > 10f) {
                    scale = newDist / oldDist; //setting the scaling of the matrix...
                    // normalize scale
                    if (scale * scaleAll > MAX_ZOOM) {
                        scale = MAX_ZOOM / scaleAll;
                    }
                    if (scale * scaleAll < MIN_ZOOM) {
                        scale = MIN_ZOOM / scaleAll;
                    }
                    scaleAll *= scale;
                    //if scale > 1 means zoom in
                    // if scale < 1 means zoom out
                    matrix.postScale(scale, scale, mid.x, mid.y);
                }
                invalidate();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mode == ERASE_MODE || mode == UNERASE_MODE) {
                touch_up();
                is_from_init = false;

                temt_x = event.getX();
                temp_y = event.getY();

                Log.d(TAG, "add to stack");
                addToStack(false);
            } 
            invalidate();
            break;

        case MotionEvent.ACTION_POINTER_UP: // second finger lifted
            touchMode = NONE;
            Log.d(TAG, "mode=NONE");
            break;

        case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down
            oldDist = spacing(event);
            if (oldDist > 5f) {
                savedMatrix.set(matrix);
                midPoint(mid, event);
                touchMode = ZOOM;
                Log.d(TAG, "mode=ZOOM");
            }
            break;
    }
    return true;
}

0 个答案:

没有答案