This article is an example of how Android programming developed a way to draw basic graphics using path in canvas. Share to everyone for your reference, specific as follows:
To draw a basic set of graphics in Android, this program is a custom view component that rewrites the OnDraw (Canvase) method of the view component, and then draws a large number of basic collection graphics on the canvas.
Directly on the code:
1. Custom View Component code:
Package com.infy.configuration;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import android.graphics.LinearGradient;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import Android.graphics.RectF;
Import Android.graphics.Shader;
Import Android.util.AttributeSet;
Import Android.view.View;
public class MyView extends view{public myview (context, AttributeSet attrs) {Super (context, attrs); @Override protected void OnDraw (Canvas Canvas) {//TODO auto-generated method Stub Super.ondraw (
Canvas);
Draw the entire canvas into a white canvas.drawcolor (color.white);
Paint Paint = new Paint ();
Sawtooth Paint.setantialias (TRUE);
Paint.setcolor (Color.Blue);
Paint.setstyle (Paint.Style.STROKE);
Paint.setstrokewidth (3);
Draw round Canvas.drawcircle (n, paint);
Draw Square Canvas.drawrect (ten, N, 140, paint); Draws a rectangular canvas.drawrect (10, 190, paint);
RECTF rel = new RECTF (10,240,70,270);
Draw Elliptical Canvas.drawoval (rel, paint);
Defines a path object that closes a triangle path path1 = new Path ();
Path1.moveto (10, 340);
Path1.lineto (70, 340);
Path1.lineto (40, 290);
Path1.close ();
Draw the Triangle Canvas.drawpath (path1, paint) according to Path;
Defines a path object that closes a pentagram path path2 = new Path ();
Path2.moveto (27, 360);
Path2.lineto (54, 360);
Path2.lineto (70, 392);
Path2.lineto (40, 420);
Path2.lineto (10, 392);
Path2.close ();
Drawing according to path, drawing the pentagram Canvas.drawpath (path2, paint);
After setting the style of the fill bundle, Draw Paint.setstyle (Paint.Style.FILL);
Paint.setcolor (color.red);
Canvas.drawcircle (paint);
Draw a square canvas.drawrect (140, paint);
Draw rectangular Canvas.drawrect (n, 190, paint);
Draw Rounded Rectangle RECTF Re2 = new RECTF (90,200,150,230);
Canvas.drawroundrect (Re2, paint); Draw Ellipse RECTF re21 = new RECTF (90, 240, 150, 270);
Canvas.drawoval (RE21, paint);
Path path3 = new Path ();
Path3.moveto (90, 340);
Path3.lineto (150, 340);
Path3.lineto (120, 290);
Path3.close ();
Draw triangular Canvas.drawpath (path3,paint);
Draw the five-angled path Path4 = new Path ();
Path4.moveto (106, 360);
Path4.lineto (134, 360);
Path4.lineto (150, 392);
Path4.lineto (120, 420);
Path4.lineto (90, 392);
Path4.close ();
Canvas.drawpath (Path4, paint); Set gradient after setting the gradient Shader Mshasder = new LinearGradient (0, 0, paint, new Int[]{color.red,color.green,co Lor.
Blue,color.yellow}, NULL, Shader.TileMode.REPEAT);
Paint.setshader (Mshasder);
Set Shadow Paint.setshadowlayer (A, color.gray);
Draw round Canvas.drawcircle (n, paint);
Draw Square Canvas.drawrect (170, 230, 140, paint); Draw rectangular Canvas.drawrect (170, 230, 190, PaiNT);
Rectangle that draws rounded corners rectf re31 = new RECTF ();
Canvas.drawroundrect (re31, paint);
Draw Ellipse RECTF re32 =new RECTF ();
Canvas.drawoval (re32, paint);
According to path, draw the triangular path Path5 = new Path ();
Path5.moveto (170, 340);
Path5.lineto (230, 340);
Path5.lineto (200, 290);
Path5.close ();
Canvas.drawpath (Path5, paint);
According to Path, draw the five-corner path PATH6 = new Path ();
Path6.moveto (186, 360);
Path6.lineto (214, 360);
Path6.lineto (230, 392);
Path6.lineto (200, 420);
Path6.lineto (170, 392);
Path6.close ();
Canvas.drawpath (Path6, paint);
}
}
2. Use a basic activity to implement custom MyView components,
Define a Zidingyiviewtes activity:
Package com.infy.configuration;
Import android.app.Activity;
Import Android.os.Bundle;
public class Zidingyiviewtes extends activity{
private myview myview =null;
@Override
protected void onCreate (Bundle savedinstancestate) {
//TODO auto-generated method stub
Super.oncreate (savedinstancestate);
MyView = new MyView (this, null);
Setcontentview (MyView);
}
Android's canvas can either draw simple collection graphics or draw a bitmap directly onto the canvas.
Finally attach the effect diagram (one more ellipse):
I hope this article will help you with the Android program.