在android画布上通过捏缩缩放绘制一条线

在android画布上通过捏缩缩放绘制一条线,android,android-canvas,Android,Android Canvas,我在一个绘图应用程序上实现了收缩缩放,但当我缩放时,它会画一条线。我知道为什么我的onTouchEvent方法的结构会发生这种情况,但我想不出一个解决方法。有人能帮忙吗 public boolean onTouchEvent(MotionEvent event){ //get x and y values of user touch float touchx = event.getX(); float touchy = event.getY(); boolean

我在一个绘图应用程序上实现了收缩缩放,但当我缩放时,它会画一条线。我知道为什么我的onTouchEvent方法的结构会发生这种情况,但我想不出一个解决方法。有人能帮忙吗

public boolean onTouchEvent(MotionEvent event){

    //get x and y values of user touch
    float touchx = event.getX();
    float touchy = event.getY();
    boolean isScaling;

    if (event.getPointerCount() > 1){
        sgd.onTouchEvent(event);
        isScaling = true;
        return true;
    }
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchx, touchy);
            drawPath.lineTo((touchx+1.0f), touchy); //enable drawing points 
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchx, touchy);
            break;
        case MotionEvent.ACTION_UP:
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;
        default:
            return false;
    }
    invalidate();
    return true;
}
编辑:明白了!还有一个尚未实现的拖动功能

public boolean onTouchEvent(MotionEvent event){
    //get x and y values of user touch
    float touchx = event.getX();
    float touchy = event.getY();

    if (event.getPointerCount() > 1){
        sgd.onTouchEvent(event);
        isScaling=true;
        return true;
    }

    else if (moving){
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                oldx = touchx;
                oldy = touchy;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isScaling == false){
                    tx = touchx-oldx;
                    ty = touchy-oldy;
                    //canvas.translate
                }
                break;
            case MotionEvent.ACTION_UP:
                if(isScaling){
                    isScaling = false;
                }
                break;
            default:
                return false;
        }
    }
    else{
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchx, touchy);
                oldx = touchx;
                oldy = touchy;
                drawPath.lineTo((touchx+1.0f), touchy); //enable drawing points, silence during scaling somehow
                break;
            case MotionEvent.ACTION_MOVE:
                if (isScaling == false){
                    drawPath.lineTo(touchx, touchy);
                }
                break;
            case MotionEvent.ACTION_UP:
                if(isScaling){
                    isScaling = false;
                }
                else{
                    drawCanvas.drawPath(drawPath, drawPaint);
                }
                drawPath.reset();
                break;
            default:
                return false;
        }   
    }
    invalidate();
    return true;
}

也许您可以按一个按钮来打开和关闭绘图。如果选择该按钮,则会发生绘图事件。如果再次按下按钮取消选择,则MotionEvent案例将执行滚动/缩放事件。这是一种UI技术,某些应用程序喜欢允许用户在绘制和更改图像过滤器之间切换。

添加您的解决方案作为答案+1.为了弄清楚这件事