1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import android.content.Context; import android.graphics.BitmapFactory; 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; import com.leon.myappdraw1.R;
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) { super(context, attrs); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3);
canvas.drawCircle(40, 40, 30, paint); canvas.drawRect(10, 80, 70, 140, paint); canvas.drawRect(10, 150, 70, 190, paint); canvas.drawRoundRect(new RectF(10, 200, 70, 230), 15, 15, paint); canvas.drawOval(new RectF(10, 240, 70, 270), paint);
Path path1 = new Path(); path1.moveTo(10, 340); path1.lineTo(70, 340); path1.lineTo(40, 290); path1.close(); canvas.drawPath(path1, paint); Path path2 = new Path(); path2.moveTo(26, 360); path2.lineTo(54, 360); path2.lineTo(70, 392); path2.lineTo(40, 420); path2.lineTo(10, 392); path2.close(); canvas.drawPath(path2, paint);
paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); canvas.drawCircle(120, 40, 30, paint); canvas.drawRect(90, 80, 150, 140, paint); canvas.drawRect(90, 150, 150, 190, paint); canvas.drawRoundRect(new RectF(90, 200, 150, 230), 15, 15, paint); canvas.drawOval(new RectF(90, 240, 150, 270), paint); Path path3 = new Path(); path3.moveTo(90, 340); path3.lineTo(150, 340); path3.lineTo(120, 290); path3.close(); canvas.drawPath(path3, paint); 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);
Shader mShader = new LinearGradient(0, 0, 40, 60, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }, null, Shader.TileMode.REPEAT); paint.setShader(mShader); paint.setShadowLayer(25, 10, 10, Color.GRAY);
canvas.drawCircle(200, 40, 30, paint); canvas.drawRect(170, 80, 230, 140, paint); canvas.drawRect(170, 150, 230, 190, paint); canvas.drawRoundRect(new RectF(170, 200, 230, 230), 15, 15, paint); canvas.drawOval(new RectF(170, 240, 230, 270), paint);
Path path5 = new Path(); path5.moveTo(170, 340); path5.lineTo(230, 340); path5.lineTo(200, 290); path5.close(); canvas.drawPath(path5, paint);
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);
paint.setTextSize(24); paint.setShader(null);
canvas.drawText("圆形", 240, 50, paint); canvas.drawText("正方形", 240, 120,paint); canvas.drawText("长方形", 240, 175,paint); canvas.drawText("圆角矩形", 230,220, paint); canvas.drawText("椭圆形", 240, 260,paint); canvas.drawText("三角形", 240, 325, paint); canvas.drawText("五角形", 240, 390, paint); canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher), 500, 150, paint);
} }
|