Android画布绘制矩形
如何使用等绘制空矩形。 borderWidth=3 和 borderColor=black 且矩形内的部分没有内容或颜色。使用 Canvas 中的哪个函数
void drawRect(float left, float top, float right, float bottom, Paint paint)
void drawRect(RectF rect, Paint paint)
void drawRect(Rect r, Paint paint)
谢谢。
我尝试这个例子,
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(0, 0, 0));
myPaint.setStrokeWidth(10);
c.drawRect(100, 100, 200, 200, myPaint);
它绘制矩形并用黑色填充它,但我只想围绕“框架” 像这样的图片:
how to draw empty rectangle with etc. borderWidth=3 and borderColor=black and part within rectangle don't have content or color. Which function in Canvas to use
void drawRect(float left, float top, float right, float bottom, Paint paint)
void drawRect(RectF rect, Paint paint)
void drawRect(Rect r, Paint paint)
Thanks.
I try this example
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(0, 0, 0));
myPaint.setStrokeWidth(10);
c.drawRect(100, 100, 200, 200, myPaint);
It draws rectangle and fill it with black color but I want just "frame" around
like this image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试
paint.setStyle(Paint.Style.STROKE)
?Try
paint.setStyle(Paint.Style.STROKE)
?假设“矩形内的部分没有内容颜色”意味着您希望在矩形内进行不同的填充;您需要在矩形内绘制一个矩形,然后使用描边宽度 0 和所需的填充颜色。
例如:
DrawView.java
启动它的活动:
StartDraw.java
...将变成这样:
Assuming that "part within rectangle don't have content color" means that you want different fills within the rectangle; you need to draw a rectangle within your rectangle then with stroke width 0 and the desired fill colour(s).
For example:
DrawView.java
The activity to start it:
StartDraw.java
...will turn out this way:
创建一个新类
MyView,它扩展了View
。重写onDraw(Canvas canvas)
方法以在Canvas
上绘制矩形。然后使用我们的自定义视图 MyView.Call 将您的 Java 活动移动到
setContentView()
。欲了解更多详情,您可以访问这里
Create a new class
MyView, Which extends View
. Override theonDraw(Canvas canvas)
method to draw rectangle onCanvas
.Then Move your Java activity to
setContentView()
using our custom View, MyView.Call this way.For more details you can visit here
并且您的任何一个
drawRect
都应该可以工作。and either one of your
drawRect
should work.代码很好,只需将 PaintStyle 设置为 STROKE
The code is fine just setStyle of paint as STROKE
不知道这是否太晚了,但我解决这个问题的方法是绘制四个薄矩形,它们一起构成一个大边框。用一个矩形绘制边框似乎是不可撤销的,因为它们都是不透明的,因此您应该单独绘制边框的每个边缘。
Don't know if this is too late, but the way I solved this was to draw four thin rectangles that together made up a one big border. Drawing the border with one rectangle seems to be undoable since they're all opaque, so you should draw each edge of the border separately.