ImM*_*han 1 android canvas android-canvas
我正在绘制一个矩形填充外的矩形.我尝试了一些这个.但是不能得到我所期望的完美的.
这就是我的预期.
我试过了
Point pTopLeft = new Point();
Point pBotRight = new Point();
pTopLeft.x = 100;
pTopLeft.y = 100;
pBotRight.x = canvas.getWidth() - 100;
pBotRight.y = canvas.getHeight() - 100;
Rect above = new Rect(0, 0, canvas.getWidth(), pTopLeft.y);
paint.setColor(Color.parseColor("#77000000"));
canvas.drawRect(above, paint);
Rect left = new Rect(0, pTopLeft.y, pTopLeft.x, pBotRight.y);
paint.setColor(Color.parseColor("#77000000"));
canvas.drawRect(left, paint);
Rect right = new Rect(pBotRight.x, pTopLeft.y, canvas.getWidth(),
pBotRight.y);
paint.setColor(Color.parseColor("#77000000"));
canvas.drawRect(right, paint);
Rect bottom = new Rect(0, pBotRight.y, canvas.getWidth(),
canvas.getHeight());
paint.setColor(Color.parseColor("#77000000"));
Paint paint_text = new Paint();
paint_text.setColor(Color.WHITE);
paint_text.setTextSize(50);
paint_text.setTextAlign(Align.CENTER);
canvas.drawText("Position Card in this Frame", canvas.getWidth() / 2,
canvas.getHeight() - 30, paint_text);
canvas.drawRect(bottom, paint);
Run Code Online (Sandbox Code Playgroud)
得到这样的
但我想绘制矩形并填充边界以实现圆形边框.我该怎么做?
编辑 当我试图一个一个地绘制矩形.布局就像这样..
我不能给Color.TRANSPARENT中心矩形完全转移第二个矩形..
我还不完全确定你要完成什么.您显示的形状可以如下绘制:
// set up some constants
int w = canvas.getWidth();
int h = canvas.getHeight();
RectF rect = new RectF(100, 100, w - 100, h - 100);
float radius = 10.0f; // should be retrieved from resources and defined as dp
float borderWidth = 2.0f; // ditto
int innerRectFillColor = 0x33000000; // or whatever shade it should be
// first fill the interior
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(innerRectFillColor);
paint.setStyle(Paint.Style.FILL);
canvas.drawRoundRect(rect, radius, radius, paint);
// then draw the border
paint.setColor(Color.WHITE);
paint.setStrokeWidth(borderWidth);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRoundRect(rect, radius, radius, paint);
Run Code Online (Sandbox Code Playgroud)
如果您想要围绕一个洞绘制(因此背景显示通过),绘制周围矩形的技巧将无法工作,因为圆角(边框也使其更复杂).相反,您可以创建一个Bitmap具有透明孔的单独的,然后绘制它.您需要使用Porter-Duff传输模式CLEAR在位图中打孔:
// same constants as above except innerRectFillColor is not used. Instead:
int outerFillColor = 0x77000000;
// first create an off-screen bitmap and its canvas
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas auxCanvas = new Canvas(bitmap);
// then fill the bitmap with the desired outside color
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(outerFillColor);
paint.setStyle(Paint.Style.FILL);
auxCanvas.drawPaint(paint);
// then punch a transparent hole in the shape of the rect
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
auxCanvas.drawRoundRect(rect, radius, radius, paint);
// then draw the white rect border (being sure to get rid of the xfer mode!)
paint.setXfermode(null);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
auxCanvas.drawRoundRect(rect, radius, radius, paint);
// finally, draw the whole thing to the original canvas
canvas.drawBitmap(bitmap, 0, 0, paint);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3013 次 |
| 最近记录: |