1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.content.Context; 5 import android.graphics.Canvas; 6 import android.graphics.Color; 7 import android.graphics.Paint; 8 import android.graphics.RectF; 9 import android.graphics.Path; 10 import android.graphics.Shader; 11 import android.graphics.LinearGradient; 12 //主程序中继承自Android.view.View的 MyView类,重写 MyView的onDraw()方法, 13 //一开始就会运行绘制的工作,在onDraw()中以Paint将几何图形绘制在Canvas上, 14 //以 paint.setColor() 改变图形颜色、以 paint.setStyle()的设置来控制画出的的 15 //图形是空心还是实心。程序的最后一段,就是直接在Canvas写上文字,随着Paint对 16 //象里的属性设置,也会有不同的外观模式。 17 public class GraphicesActivity extends Activity { 18 /** Called when the activity is first created. */ 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 /*设置ContentView为自定义的MyVieW*/ 23 MyView myView=new MyView(this); 24 setContentView(myView); 25 } 26 27 /* 自定义继承View 的MyView*/ 28 private class MyView extends View { 29 public MyView(Context context){ 30 super(context) ; 31 } 32 33 /*重写onDraw()*/ 34 @Override 35 protected void onDraw(Canvas canvas) 36 { 37 super.onDraw(canvas); 38 /*设置背景为白色*/ 39 canvas.drawColor(Color.WHITE); 40 41 Paint paint=new Paint(); 42 /*去锯齿*/ 43 paint.setAntiAlias(true); 44 /*设置paint的颜色*/ 45 paint.setColor(Color.RED); 46 /*设置paint的 style 为STROKE:空心*/ 47 paint.setStyle(Paint.Style.STROKE); 48 /*设置paint的外框宽度*/ 49 paint.setStrokeWidth(3); 50 51 /*画一个空心圆形*/ 52 canvas.drawCircle(40, 40, 30, paint); 53 /*画一个空心正方形*/ 54 canvas.drawRect(10, 90, 70, 150, paint); 55 /*画一个空心长方形*/ 56 canvas.drawRect(10, 170, 70,200, paint); 57 /*画一个空心椭圆形*/ 58 canvas.drawOval(new RectF(10,220,70,250), paint); 59 /*画一个空心三角形*/ 60 Path path=new Path(); 61 path.moveTo(10, 330); 62 path.lineTo(70,330); 63 path.lineTo(40,270); 64 path.close(); 65 canvas.drawPath(path, paint); 66 /*画一个空心梯形*/ 67 Path path1=new Path(); 68 path1.moveTo(10, 410); 69 path1.lineTo(70,410); 70 path1.lineTo(55,350); 71 path1.lineTo(25, 350); 72 path1.close(); 73 canvas.drawPath(path1, paint); 74 75 /*设置paint的颜色*/ 76 paint.setColor(Color.BLUE); 77 /*设置paint 的style为 FILL:实心*/ 78 paint.setStyle(Paint.Style.FILL); 79 /*画一个实心圆*/ 80 canvas.drawCircle(120,40,30, paint); 81 /*画一个实心正方形*/ 82 canvas.drawRect(90, 90, 150, 150, paint); 83 /*画一个实心长方形*/ 84 canvas.drawRect(90, 170, 150,200, paint); 85 /*画一个实心椭圆*/ 86 RectF re2=new RectF(90,220,150,250); 87 canvas.drawOval(re2, paint); 88 /*画一个实心三角形*/ 89 Path path2=new Path(); 90 path2.moveTo(90, 330); 91 path2.lineTo(150,330); 92 path2.lineTo(120,270); 93 path2.close(); 94 canvas.drawPath(path2, paint); 95 /*画一个实心梯形*/ 96 Path path3=new Path(); 97 path3.moveTo(90, 410); 98 path3.lineTo(150,410); 99 path3.lineTo(135,350); 100 path3.lineTo(105, 350); 101 path3.close(); 102 canvas.drawPath(path3, paint); 103 104 /*设置渐变色*/ 105 Shader mShader=new LinearGradient(0,0,100,100, 106 new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, 107 null,Shader.TileMode.REPEAT); 108 //Shader.TileMode三种模式 109 //REPEAT:沿着渐变方向循环重复 110 //CLAMP:如果在预先定义的范围外画的话,就重复边界的颜色 111 //MIRROR:与REPEAT一样都是循环重复,但这个会对称重复 112 paint.setShader(mShader);//用Shader中定义定义的颜色来话 113 114 /*画一个渐变色圆*/ 115 canvas.drawCircle(200,40,30, paint); 116 /*画一个渐变色正方形*/ 117 canvas.drawRect(170, 90, 230, 150, paint); 118 /*画一个渐变色长方形*/ 119 canvas.drawRect(170, 170, 230,200, paint); 120 /*画一个渐变色椭圆*/ 121 RectF re3=new RectF(170,220,230,250); 122 canvas.drawOval(re3, paint); 123 /*画一个渐变色三角形*/ 124 Path path4=new Path(); 125 path4.moveTo(170,330); 126 path4.lineTo(230,330); 127 path4.lineTo(200,270); 128 path4.close(); 129 canvas.drawPath(path4, paint); 130 /*画一个渐变色梯形*/ 131 Path path5=new Path(); 132 path5.moveTo(170, 410); 133 path5.lineTo(230,410); 134 path5.lineTo(215,350); 135 path5.lineTo(185, 350); 136 path5.close(); 137 canvas.drawPath(path5, paint); 138 139 /*写字*/ 140 paint.setTextSize(24); 141 canvas.drawText("圆形", 240, 50, paint); 142 canvas.drawText("正方形", 240, 120, paint); 143 canvas.drawText("长方形", 240, 190, paint); 144 canvas.drawText("椭圆形", 240, 250, paint); 145 canvas.drawText("三角形", 240, 320, paint); 146 canvas.drawText("梯形", 240, 390, paint); 147 } 148 } 149 }