2012-09-07 117 views
1

正在開發一個應用程序,我在其中粘貼圖像,在畫布上進行繪畫和繪畫。此應用程序還可以放大/縮小畫布或將其拖到不同的位置。我的問題是:縮放或拖動畫布後無法獲得正確的畫布座標。我想提請畫布進行縮放或拖動,但無法檢索在那裏我已經碰到合適的位置後,手指畫...使用縮放和darg繪製路徑

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

}

當用戶放大位圖,他是不是能繪製縮放後,繪製不會發生在適當的地方。

+0

請幫我解決上述問題。 – strike

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

優秀的解決方案!謝謝!!! –

+0

什麼是m_transformedPoints? – MohsinSyd