Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a triangle, a star, a square or a heart on the canvas?

I am able to draw a circle and a rectangle on canvas by using
path.addCircle()

and

path.addRect().

And now I am wondering how to draw a triangle or a star or a square or a heart?

like image 341
nickfrancis.me Avatar asked Aug 10 '11 07:08

nickfrancis.me


People also ask

How do I draw a triangle in Canvas android?

Simply call drawTriangle with the Canvas to draw on, the Paint to draw with, the X/Y coordinates to draw at, and the width of the triangle. Not bad, with a little customization such as modifying the color or applying antialiasing this can work quite well for your triangle drawing needs.


1 Answers

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!

like image 128
Moss Avatar answered Sep 17 '22 12:09

Moss