绘制路径变焦和DARG(Draw Path with zoom and darg)

2019-09-22 05:39发布

“M发展中,我粘贴图片,做绘画在画布上的应用程序。 这个应用程序还可以放大/缩小画布或将其拖动到不同的位置。 我的问题是:我不能让缩放或拖动画布后的正确的画布坐标。 我想在画布上进行缩放或拖动,但无法检索在那里我已经碰到合适的地方后得出手指画...

@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);

}

当用户放大位图他是不是能画过它,缩放拉伸,以适当的地方不会发生了。

Answer 1:

使用下列公式转化为积分,之后变焦过程中或滚动之后用户将不会面临任何困难画。

 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);


文章来源: Draw Path with zoom and darg