如何在画布上绘制三角形、星形、正方形或心形?
我可以使用
在画布上绘制一个圆形和一个矩形 path.addCircle()
和
path.addRect()
。
现在我想知道如何画三角形、星形、正方形或心形?
I am able to draw a circle and a rectangle on canvas by usingpath.addCircle()
and
path.addRect()
.
And now I am wondering how to draw a triangle or a star or a square or a heart?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于未来的直接答案寻求者,我使用画布绘制了一个几乎对称的星星,如图所示:
主要工具正在使用路径。
假设您已经设置:
然后在 onDraw 中您可以像我下面那样使用路径。它将正确缩放到任何尺寸的画布
For future direct answer seekers, I have drawn an almost symmetric star using canvas, as shown in the image:
The main tool is using Paths.
Assuming you have setup:
Then in you onDraw you can use the path like I do below. It will scale properly to any sizes canvas
对于每个需要心形的人:
抱歉所有的数字,但这些对我来说效果最好:)结果如下所示:
For everybody that needs a heart shape:
Sorry for all the numbers but these worked best for me :) The result looks like this:
你必须找出这些数字背后的数学原理。三角形和星形很容易画。以下是绘制心形的方法: http://www.mathematische-basteleien.de/heart .htm
要绘制特殊路径,您应该通过添加点、椭圆等来创建它们。画布支持指定路径的剪贴蒙版,因此您可以设置心形的剪贴蒙版,将路径推入矩阵,绘制心脏的内容,然后再次弹出。
这就是我在 andriod 上实现模拟 2D 页面卷曲效果的方法: http: //code.google.com/p/android-page-curl/
希望这有帮助!
You have to find out the math behind that figures. The triangle and the star are quite easy to draw. Here is how you can draw a heart: http://www.mathematische-basteleien.de/heart.htm
To draw special paths you should create them by adding points, ellipses etc. The canvas supports a clipping mask of a specified path, so you can set the clipping mask of a heart, push the paths to the matrix, draw the content of the heart, and then pop it again.
That's what I'm doing to achieve a simulated 2D page curl effect on andriod: http://code.google.com/p/android-page-curl/
Hope this helps!
此方法将返回一条路径,其中包含给定宽度的正方形内给定的角数。添加更多参数来处理小半径之类的事情。
this method will return a path with the number of corners given inside a square of the given width. Add more parameters to handle small radius and such things.
如果您需要在正方形内画一颗星星,可以使用下面的代码。
posX
和posY
是包围星形尖端的正方形左上角的坐标(该正方形实际上并未绘制)。size
是正方形的宽度和高度。a
是星形的顶端。路径按顺时针方向创建。这绝不是一个完美的解决方案,但它可以很快完成工作。
If you need to draw a star inside a square, you can use the code below.
posX
andposY
are the coordinates for the upper left corner of the square that encloses the tips of the star (the square is not actually drawn).size
is the width and height of the square.a
is the top tip of the star. The path is created clockwise.This is by no means a perfect solution, but it gets the job done very quickly.
除了椭圆形和矩形之外,您还需要这两个(至少):
drawLine(float startX, float startY, float stopX,浮动停止Y,油漆油漆)
drawLines(float[] pts, int offset, int count, Paint Paint)
示例:
如何在 android 中绘制一条线
Canvas 上的文档:http://developer.android.com/reference/android/graphics/Canvas.html
In addition to ellipse and rectangular you will need those two (as minimum):
drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
drawLines(float[] pts, int offset, int count, Paint paint)
example:
How to draw a line in android
Documentation on Canvas: http://developer.android.com/reference/android/graphics/Canvas.html
使用 Shape 类的实例非常好))
创建一个渲染良好的形状类 Heart
Its very good to use instance of Shape class ))
Create a shape class which was render nice Heart
好吧,如果您只想绘制您指定的形状,我建议首先为每个形状创建一个类,并使每个形状实现 draw() 方法,您可以在其中传递画布和绘画目的。
例如,要创建多个星星,首先创建一个类“Star”并在那里实现逻辑。
然后创建一个自定义视图,您可以在其中绘制形状,下面是创建 100 个随机星星并将它们绘制在画布上的代码。
结果如下:
如果您想创建许多不同的复杂形状,您可以使用此 库。您可以在其中使用路径数据作为字符串传递 svg 形状。首先使用任何 SVG 矢量编辑器(例如
Well if you want to draw only the shapes you specify I recommend first creat a class for each shape and make each shape implement draw() method where you can pass canvas and paint object.
For example to create multiple stars, first create a class "Star" and implement the logic there.
And then create a custom view where you can draw your shapes, below is the code that creates 100 random stars and draws them on canvas.
Here is the result:
If you want to create a lot of different complex shapes you can use this library. Where you can pass svg shapes using the Path Data as a string. By first creating complex shapes using any SVG vector editor such as