簡體   English   中英

在 canvas 中的當前剪輯上繪制 bitmap 並帶有邊框(油漆)

[英]Draw bitmap on current clip in canvas with border (Paint)

我正在通過編寫游戲來學習 Android 並且圖形 API 有問題。

我想將圖像繪制成路徑的形狀,然后在路徑上添加邊框。 我能夠使用 Path 剪輯圖像,但找不到在其上添加邊框的方法。 我認為這很簡單,因為 API 在 Canvas.draw* 方法上支持 Paint object。

更新

在原始問題中,我的路徑包含兩個矩形,@christopher-souvey 回答正確。 但是,在處理添加另一個 clipPath() 方法時,我遇到了另一個問題。

我通過在 Path 中再添加一個圓圈來更新之前的代碼。 這是我的新代碼:

Bitmap srcImage = BitmapFactory.decodeStream(getAssets().open("panda.jpg"));
Bitmap bitmapResult = Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Path path = new Path();

// This is my border
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);

Canvas canvas = new Canvas(bitmapResult);

// Overlay two rectangles
path.addRect(10, 10, 70, 70, Path.Direction.CCW); 
path.addRect(40, 40, 120, 120, Path.Direction.CCW);
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.INTERSECT);

path.reset();
path.addCircle(40, 80, 20, Path.Direction.CCW); 
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.DIFFERENCE);

// The image is drawn within the area of two rectangles and a circle
// Although I suppose that puting Paint object into drawBitmap() method will add a red border on result image but it doesn't work
canvas.drawBitmap(srcImage, 0, 0, paint);

((ImageView)this.findViewById(R.id.imageView1)).setImageBitmap(bitmapResult);

這是我的代碼的結果: http://i.stack.imgur.com/8j2Kg.png

這就是我所期望的: http://i.stack.imgur.com/iKhIr.png

我想念什么讓它工作嗎?

嘗試在 drawBitmap 之后使用canvas.drawPath(path, paint)您可能必須在剪輯之前放入canvas.savecanvas.restore中風是否發生在 drawPath 內部或外部之前.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM