使用缩放和darg绘制路径

时间:2012-09-07 06:09:25

标签: android bitmap android-canvas drawing2d pinchzoom

开发一个应用程序,我在其中粘贴图像,在画布上绘图和绘画。此应用程序还可以向上/向下缩放画布或将其拖动到不同的位置。我的问题是:在缩放或拖动画布后,我无法获得正确的画布坐标。我想在缩放或拖动画布后绘制手指画,但无法找到我触摸过的正确位置......

@Override
public boolean onTouchEvent(MotionEvent event) {

    scaleListener.onTouchEvent(event);

    synchronized(thread.getSurfaceHolder()){

        switch ( event.getAction() & MotionEvent.ACTION_MASK ){

            case MotionEvent.ACTION_DOWN :
                // Remember our initial down event location.
                savedMatrix.set(m_matrix);
                start.set(event.getX() , event.getY());
                mode = DRAG;                
            break;

            case MotionEvent.ACTION_POINTER_DOWN :

                oldDist = spacing(event);
                if (oldDist > 10f) {

                    savedMatrix.set(m_matrix);
                    m_matrix.getValues(matrixValues);
                    midPoint(mid, event);
                    mode = ZOOM;
                }

             break;

            case MotionEvent.ACTION_UP :
            case MotionEvent.ACTION_POINTER_UP :
                    mode = NONE;

            break;

            case MotionEvent.ACTION_MOVE :

                if(mode==DRAG && isDragAllowd){
                        m_matrix.set(savedMatrix);
                        rebound(event.getX() , event.getY());
                }
                else if(mode==ZOOM){

                    float newDist = spacing(event);
                    if (newDist > 10f){

                            m_matrix.set(savedMatrix);

                            float scale = newDist/oldDist;
                            m_matrix.getValues(matrixValues);
                            float currentScale = matrixValues[Matrix.MSCALE_X];

                            // limit zoom
                            if (scale * currentScale > maxZoom){
                                    scale = maxZoom / currentScale;
                                    //scale = maxZoom;
                            }
                            else if (scale * currentScale < minZoom){
                                    scale = minZoom / currentScale;
                                    //scale = minZoom;
                            }

                            m_matrix.postScale(scale, scale, mid.x, mid.y);
                            m_matrix.getValues(matrixValues);

                  }

            }
       break;

    }

    return true; //done with this event so consume it


    }       

}

@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

            canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(currentBitmap, m_matrix, null);
        c2.drawColor(Color.BLACK); 
        commandManager.executeAll(c2); // Drawing all the Paints on Canvas c2
        // Overlay bitmap it's a transparent layour 
            canvas.drawBitmap(overlayBitmap, 0, 0, pTouch);

}

当用户缩放位图时,他无法在其上绘制,在缩放后不会在适当的位置进行绘制。

1 个答案:

答案 0 :(得分:3)

使用以下等式转换点,之后用户在缩放或滚动后不会遇到任何难度绘画。

 float f1 = (( motionEvent.getX() - m_transformedPoints[0] )/( m_transformedPoints[2] - m_transformedPoints[0] ))*currentBitmapWidth;
     float f2 = (( motionEvent.getY() - m_transformedPoints[1] )/( m_transformedPoints[3] - m_transformedPoints[1] ))*currentBitmapHeight; 

    //calculate the transformed view in onDraw Method
    this.m_transformedPoints[0] = 0.0F;
    this.m_transformedPoints[1] = 0.0F;
    this.m_transformedPoints[2] = currentBitmapWidth;
    this.m_transformedPoints[3] = currentBitmapHeight;
    this.m_matrix.mapPoints(this.m_transformedPoints);