Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Path (android.graphics.Path) with canvas in android?

I am working on a Custom View that implements Catch application like Circular Menu. After spending a lot of time, I have made a bit of progress, completed outer semi-circle with multile colors. Now, reading an answer provided by developer of Catch application to an user for his query, I came across the class Path. Google Android Developer page does not provides enough material to be understood and to be familiar with Path. so, Please ? Anyone ?

Thanks in advance.

like image 663
Paras Avatar asked Mar 24 '13 10:03

Paras


People also ask

What is Path Android canvas?

The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves. It can be drawn with canvas.

How does canvas work on Android?

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

How do you make a bitmap on canvas?

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.


1 Answers

You can use it to draw lines on a canvas. A path is basically a collection of lines. You can use it to create shapes that are not standard. E.g. there a lots of functions to create some default shapes:

canvas.drawRect();
canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint);
canvas.drawCircle(float cx, float cy, float radius, Paint paint);
canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint);
canvas.drawOval(RectF oval, Paint paint);
canvas.drawRect(float left, float top, float right, float bottom, Paint paint);

But if you want something custom, you can create a path, and by calling

// Set the beginning of the next contour to the point (x,y).
void     moveTo(float x, float y)

// Add a line from the last point to the specified point (x,y).
void     lineTo(float x, float y)

You have control of the pencil that draws the lines of your path. Here's a nice tutorial

like image 120
Entreco Avatar answered Oct 15 '22 08:10

Entreco