扔物线大神的《Android 开发进阶: 自定义 View 1-1 绘制基础》Practice的补充代码

GitHub地址:PracticeDraw1

练习内容1:使用canvas.drawColor()方法把View涂成黄色

canvas.drawColor(Color.YELLOW);

canvas.drawColor()的使用方法汇总:

canvas.drawColor(Color.YELLOW);
canvas.drawColor(Color.parseColor("#00FF00"));
canvas.drawColor(Color.parseColor("#88FF0000"));
canvas.drawRGB(255, 0, 0);
canvas.drawARGB(127, 255, 0, 0);

练习内容2:使用canvas.drawCircle()方法画圆

一共四个圆:1.实心圆2.空心圆3.蓝色实心圆4.线宽为20的空心圆

mPaint.setAntiAlias(true);

canvas.drawCircle(350, 200, 150, mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(4);
canvas.drawCircle(700, 200, 150, mPaint);

mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(350, 550, 150, mPaint);

mPaint.setColor(Color.YELLOW);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(50);                   // setStrokeWidth只在STROKE的情况下起作用,FILL情况下不起作用
canvas.drawCircle(700, 550, 150, mPaint);

mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
canvas.drawCircle(700, 550, 150, mPaint);

mPaint.reset();

练习内容3:使用canvas.drawRect()方法画矩形

mPaint.setAntiAlias(true);

canvas.drawRect(100, 100, 300, 300, mPaint);

mPaint.setColor(Color.RED);
Rect rect = new Rect(400, 100, 600, 300);
canvas.drawRect(rect, mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(4);
RectF rectF = new RectF(700, 100, 900, 300);
canvas.drawRect(rectF, mPaint);

mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.GREEN);
canvas.drawRoundRect(100, 400, 300, 600, 50, 50, mPaint);

mPaint.setStyle(Paint.Style.STROKE);
RectF rectF2 = new RectF(400, 400, 600, 600);
canvas.drawRoundRect(rectF2, 50, 80, mPaint);

mPaint.reset();

练习内容4:使用canvas.drawPoint()方法画点

mPaint.setAntiAlias(true);

mPaint.setStrokeWidth(100);
mPaint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawPoint(100, 100, mPaint);

mPaint.setStrokeCap(Paint.Cap.SQUARE);
mPaint.setColor(Color.YELLOW);
canvas.drawPoint(250, 100, mPaint);

mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setColor(Color.RED);
canvas.drawPoint(400, 100, mPaint);

mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.GREEN);
float[] points = {550, 100, 700, 100, 850, 100, 850, 250, 700, 250, 550, 250};
//   canvas.drawPoints(points, mPaint);
canvas.drawPoints(points, 2, 8, mPaint);

mPaint.reset();

练习内容5:使用canvas.drawOval()方法画椭圆

mPaint.setAntiAlias(true);

canvas.drawOval(50, 50, 350, 200, mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(4);
mPaint.setColor(Color.RED);
RectF rectF = new RectF(450, 50, 750, 200);
canvas.drawOval(rectF, mPaint);

mPaint.reset();

练习内容6:使用canvas.drawLine()方法画直线

mPaint.setAntiAlias(true);

mPaint.setStrokeWidth(10);
canvas.drawLine(50, 50, 200, 200, mPaint);

mPaint.setColor(Color.RED);
canvas.drawLine(100, 50, 250, 200, mPaint);

mPaint.setColor(Color.GREEN);
float[] points = {300, 50, 450, 200, 600, 200, 500, 300};
//   canvas.drawLines(points, mPaint);
canvas.drawLines(points, 2, 4, mPaint);

mPaint.reset();

练习内容7:使用canvas.drawRoundRect()方法画圆角矩形

mPaint.setAntiAlias(true);

canvas.drawRoundRect(100, 100, 500, 300, 50, 50, mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(4);
mPaint.setColor(Color.RED);
RectF rectF = new RectF(100, 400, 500, 600);
canvas.drawRoundRect(rectF, 50, 50, mPaint);

mPaint.reset();

练习内容8:使用canvas.drawArc()方法画弧形和扇形

mPaint.setAntiAlias(true);

mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(200, 100, 800, 500, -110, 100, true, mPaint);
mPaint.setColor(Color.RED);
canvas.drawArc(200, 100, 800, 500, 20, 140, false, mPaint);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.GREEN);
canvas.drawArc(new RectF(200, 100, 800, 500), 180, 60, false, mPaint);

mPaint.reset();

练习内容9:使用canvas.drawPath()方法画心形

mPaint.setAntiAlias(true);
Path path = new Path();
path.addArc(new RectF(600, 200, 800, 400), -225, 225);
path.arcTo(new RectF(800, 200, 1000, 400), -180, 225, false);
path.lineTo(800, 542);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawPath(path, mPaint);

mPaint.reset();

练习内容10:使用各种Canvas.drawXXX()方法画直方图

mPaint.setAntiAlias(true);

// 画图表坐标轴
Path path = new Path();
path.moveTo(150, 80);
path.lineTo(150, 500);
path.lineTo(900, 500);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(2);
canvas.drawPath(path, mPaint);

// 画矩形
Path path2 = new Path();
path2.addRect(170, 490, 250, 500, Path.Direction.CW);
path2.addRect(270, 430, 350, 500, Path.Direction.CW);
path2.addRect(370, 430, 450, 500, Path.Direction.CW);
path2.addRect(470, 380, 550, 500, Path.Direction.CW);
path2.addRect(570, 350, 650, 500, Path.Direction.CW);
path2.addRect(670, 300, 750, 500, Path.Direction.CW);
path2.addRect(770, 400, 850, 500, Path.Direction.CW);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.parseColor("#72B916"));
canvas.drawPath(path2, mPaint);

// 画文字
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(25);
canvas.drawText("Froyo", 170, 530, mPaint);
canvas.drawText("GB", 290, 530, mPaint);
canvas.drawText("ICS", 390, 530, mPaint);
canvas.drawText("JB", 490, 530, mPaint);
canvas.drawText("KitKat", 580, 530, mPaint);
canvas.drawText("L", 700, 530, mPaint);
canvas.drawText("M", 800, 530, mPaint);

// 画图表文字
mPaint.setTextSize(40);
canvas.drawText("直方图", 450, 650, mPaint);

mPaint.reset();

练习内容11:使用各种Canvas.drawXXX()方法画饼图

mPaint.setAntiAlias(true);

// 画6个扇形
mPaint.setColor(Color.parseColor("#FFC107"));
canvas.drawArc(200, 100, 700, 600, -60, 59, true, mPaint);
mPaint.setColor(Color.parseColor("#9C27B0"));
canvas.drawArc(200, 103, 700, 603, 0, 6, true, mPaint);
mPaint.setColor(Color.parseColor("#9E9E9E"));
canvas.drawArc(200, 106, 700, 606, 8, 6, true, mPaint);
mPaint.setColor(Color.parseColor("#009688"));
canvas.drawArc(200, 109, 700, 609, 16, 45, true, mPaint);
mPaint.setColor(Color.parseColor("#2196F3"));
canvas.drawArc(197, 109, 697, 609, 63, 117, true, mPaint);
mPaint.setColor(Color.parseColor("#F44336"));
canvas.drawArc(190, 95, 690, 595, -180, 120, true, mPaint);
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(40);
canvas.drawText("饼图", 400, 700, mPaint);

mPaint.reset();

最后

扔物线大神的博客地址:HenCoder

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,875评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,569评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,475评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,459评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,537评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,563评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,580评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,326评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,773评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,086评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,252评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,921评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,566评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,190评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,435评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,129评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,125评论 2 352

推荐阅读更多精彩内容