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

断了今生、忘了曾经 提交于 2019-12-22 09:46:24

问题


I am trying to use both scalegesture listener and gesturelistener in the view. 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.

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. When I change the value of scale to (say) 0.6f, it transports to a new position and then works perfectly.


回答1:


Please see the answer by @ToreRudberg here - You might want to reform your code to correct the behavior your'e observing.




回答2:


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 = 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.



来源:https://stackoverflow.com/questions/19471356/canvas-scalescale-scale-px-py-jerks-to-a-new-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!