2013-06-21 87 views
8

嘗試實現圖像的徒手剪裁,所以我可以在圖像上繪圖。但它超出了位圖區域。我只是想限制該用戶只能繪製位圖區域內,檢查下面的屏幕截圖。手繪圖​​像作物在位圖區域內繪製

我正在嘗試實現像Photoshop lasso tool這樣的功能。

其繪圖在視圖外的區域,從而產生不正確的輸出。 Input

輸出 Output

代碼@

的onDraw

public void onDraw(Canvas canvas) { 

     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     canvas.drawBitmap(bitmap, rect, rect, null); 
     // RectF r = new RectF(); 
     // Matrix matrix = new Matrix(); 
     // matrix.mapRect(r); 
     // Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " + 
     // r.bottom + " "); 
     // canvas.clipRect(r.left, r.top, r.right, r.bottom); 

     Path path = new Path(); 
     boolean first = true; 

     for (int i = 0; i < points.size(); i += 2) { 
      Point point = points.get(i); 
      if (first) { 
       first = false; 
       path.moveTo(point.x, point.y); 
      } else if (i < points.size() - 1) { 
       Point next = points.get(i + 1); 
       path.quadTo(point.x, point.y, next.x, next.y); 
      } else { 
       mlastpoint = points.get(i); 
       path.lineTo(point.x, point.y); 
      } 
     } 
     canvas.drawPath(path, paint); 
    } 

onCrop

Bitmap resultingImage = Bitmap.createBitmap(widthOfscreen,heightOfScreen, bitmap1.getConfig()); 

     Canvas canvas = new Canvas(resultingImage); 

     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     Path path = new Path(); 
     for (int i = 0; i < SomeView.points.size(); i++) { 
      path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y); 
     } 
     // path.lineTo(150, 0); 
     // path.lineTo(230, 120); 
     // path.lineTo(70, 120); 
     // path.lineTo(150, 0); 

     canvas.drawPath(path, paint); 
     if(crop){ 
      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 

     }else{ 
      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
     } 

建議我實現我的目標。

回答

0

還有就是要實現自己的目標的一種方式..

遵循以下步驟:

1)請像裁剪圖像作爲內側部分一個形象應該是透明的,外面的部分應爲背景畫布檢查下面的圖像。

2)在畫布上繪製該圖像。 3)在你的畫布上繪製任何東西,並在畫布的頂部繪製該圖像。

圖片:

enter image description here

0

可以忽略所有觸摸事件,是你的圖像視圖之外。記住圖像視圖中的最後一個事件位置,並將其與視圖內的下一個事件位置連接起來。如果兩個事件都是單個動作的一部分(不需要擡起手指),這應該起作用,但當繪圖在視圖之外完成,然後從內部開始繪製新圖形時,這可能會更復雜。試圖覆蓋這種情況。

0

遲到的答案,但使用完整的其他ü可以覆蓋onmeasure方法,並在該方法中設置您的圖像位圖寬度和高度,並調整您的畫布,所以現在你只能畫在你的畫布。所以你的圖像來center.it drawathpath只在你的位圖上。

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    height = bitmap.getHeight(); 
    width = bitmap.getWidth(); 

    setMeasuredDimension(width,height); 
} 

然後改變你的oncrop方法,這樣你可以解決esaly。

Bitmap resultingImage = Bitmap.createBitmap(yourbitmap.getwidth(),yourbitmap.getheight(), yourbitmap.getConfig());