捏缩放在Android画布上绘制一条线

时间:2014-09-14 19:34:26

标签: 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 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;
}

1 个答案:

答案 0 :(得分:0)

也许你可以按一个按钮来转动绘图" on"并且"关闭"。如果选择该按钮,则会发生绘图事件。如果再次按下该按钮以取消选择,则MotionEvent案例将执行滚动/缩放事件。这是某些应用程序(如Snapchat use)的UI技术,允许用户在绘图和更改图像过滤器之间切换。