android - 绘制填充外边界的矩形

标签 android canvas android-canvas

我正在绘制一个矩形填充外部的矩形。我尝试了其中一些。但是无法得到我所期望的完美。

这是我所期望的。

Viewfinder

我试过了

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

变成这样

enter image description here

但我想绘制矩形并在边界外填充以实现圆角边框。我该怎么做?

编辑 当我尝试一对一地绘制矩形时。布局是这样的。。

enter image description here

我不能为完全透明第二个矩形的中心矩形提供 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);

如果您想要围绕一个孔绘制(这样背景就会显示出来),绘制周围矩形的技巧将不会起作用,因为圆角(边框也会使它变得更复杂)。相反,您可以创建一个单独的 Bitmap,它有一个透明的孔,然后绘制它。您需要使用 CLEAR 的 Porter-Duff 传输模式在位图中打洞:

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

关于android - 绘制填充外边界的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679259/

相关文章:

安卓工作室错误 : Failed to find target with hash string 'android-24'

android - 如何将 Activity 中复选框的值传递给服务?

javascript - 如何使用 javascript Canvas 添加悬停功能

javascript - JS Canvas-将图像数据放入 Alpha 中?

android - 使 Canvas drawLine() 可点击的技术?

android - 如何在不打开新浏览器的情况下从 webview 下载 apk 文件

java - Hello World 编译错误 - Android

javascript - Canvas,如何设置线段虚线?

android - 始终在屏幕上的固定点绘制一个点,即使在放大或缩小 Canvas 时也是如此? - 安卓

用于实时图表的 Android Paths