简体   繁体   English

canvas.scale(scale,scale,px,py)猛然移动到新位置

[英]canvas.scale(scale,scale,px,py) jerks to a new position

I am trying to use both scalegesture listener and gesturelistener in the view. 我试图在视图中使用scalegesture listener和gesturelistener。 Everything works perfectly if the scale is 1 but if the scale is set to some other value, the image jerks to a new position and then scales smoothly. 如果比例为1,则一切正常,但如果比例设置为其他值,则图像会跳到新位置,然后平滑地缩放。

Part of my code looks like as follows: 我的部分代码如下所示:

public class SimpleView extends View {

Bitmap image;
ScaleGestureDetector scaleGesture;
GestureDetector gestures;
float touchX, touchY;
float horizontalOffset;
float verticalOffset;
float scale;

public SimpleView(Context context) {
    super(context);
    image = BitmapFactory.decodeResource(getResources(), R.drawable.some_image);
    scaleGesture = new ScaleGestureDetector(getContext(),
            new ScaleListener());
    gestures = new GestureDetector(getContext(), new GestureListener(),
            null, true);
    scale = 0.6f; //if this is set to  1.0f everything works perfectly,
            // else the imagetransports to a new position
            // and scales perfectly thereafter
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    canvas.translate(horizontalOffset, verticalOffset);
    canvas.scale(scale, scale, touchX, touchY);
    canvas.drawBitmap(image, getMatrix(), new Paint);
    canvas.restore();
}

public class ScaleListener implements OnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float oldScale = scale;
        scale *= detector.getScaleFactor();
        scale = Math.max(0.5f, Math.min(scale, 5.0f));

        if (scale != oldScale) {
            touchX = detector.getFocusX();
            touchY = detector.getFocusY();

            invalidate(0, 0, screenWidth, screenHeight);
        } 
        return true;
    }

}

public class GestureListener implements OnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
                    //Helps in drag
        if (!scaleGesture.isInProgress()) {
            horizontalOffset -= distanceX;
            verticalOffset -= distanceY;

            invalidate();
            return true;
        }
        return false;
    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = scaleGesture.onTouchEvent(event);
    boolean isScaling = result = scaleGesture.isInProgress();
    if (!isScaling) {
        if (!(event.getPointerCount() > 1)) {
            result = gestures.onTouchEvent(event);
        } else
            result = false;
    }
    return result;
}

}

In case I set the value of scale to 1.0f, this works smoothly. 如果我将scale的值设置为1.0f,这将顺利进行。 When I change the value of scale to (say) 0.6f, it transports to a new position and then works perfectly. 当我将scale的值更改为(比如)0.6f时,它会传输到一个新位置,然后完美地工作。

Please see the answer by @ToreRudberg here - You might want to reform your code to correct the behavior your'e observing. 请在此处查看@ToreRudberg的答案 - 您可能希望改进代码以纠正您观察到的行为。

This is because your scaling pivot coords (touchX and touchY) are initially 0, and when you first scale, it suddenly changes them to the focus point of the scale gesture: 这是因为您的缩放枢轴坐标(touchX和touchY)最初为0,当您第一次缩放时,它会突然将它们更改为缩放手势的焦点:

        touchX = detector.getFocusX();
        touchY = detector.getFocusY();

It works with 1.0 initial scale because 1.0f scale does not change anything, so you don't see the sudden pivot jump at the beginning. 它适用于1.0初始比例,因为1.0f比例不会改变任何东西,所以你不会在开始时看到突然的枢轴跳跃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM