我通過編寫遊戲來學習Android,並且遇到了圖形API的問題。在帶有邊框的畫布中繪製當前剪輯的位圖(Paint)
我想繪製一個圖像形狀的路徑,然後在路徑上添加邊框。我能夠用Path剪輯圖像,但無法找到在其上添加邊框的方法。我雖然它會很簡單,因爲API支持Canvas.draw *方法上的Paint對象。
更新
在原來的問題,我的路徑包含兩個矩形和@克里斯托弗 - 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中的paint元素不是邊框顏色:http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap(android.graphics.Bitmap,android.graphics.Matrix,android。繪圖)繪製 – 2011-06-11 07:42:19
我們可以使用Paint創建drawRect(),drawCircle()的邊框。爲什麼它不適用於drawBitmap() – 2011-06-11 15:03:48