android绘制几何图形的实例代码
程序员文章站
2022-06-19 19:08:34
本文实例为大家分享了android绘制几何图形展示的具体代码,供大家参考,具体内容如下
效果图:
代码(仅绘制类,不可直接运行):
public...
本文实例为大家分享了android绘制几何图形展示的具体代码,供大家参考,具体内容如下
效果图:
代码(仅绘制类,不可直接运行):
public class myview extends view { public myview(context context, attributeset set) { super(context, set); } @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); rectf re1 = new rectf(10, 200, 70, 230); // 绘制圆角矩形 canvas.drawroundrect(re1, 15, 15, paint); rectf re11 = new rectf(10, 240, 70, 270); // 绘制椭圆 canvas.drawoval(re11, paint); // 定义一个path对象,封闭成一个三角形。 path path1 = new path(); path1.moveto(10, 340);//将路径点设置到10,340位置 path1.lineto(70, 340);//将起始路径点连接都70,340位置 path1.lineto(40, 290);//将第二连接点连接到40,290位置 path1.close();//关闭路径的绘制 // 根据path进行绘制,绘制三角形 canvas.drawpath(path1, paint); // 定义一个path对象,封闭成一个五角形。 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(); // 根据path进行绘制,绘制五角形 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); rectf re2 = new rectf(90, 200, 150, 230); //绘制圆角矩形 canvas.drawroundrect(re2, 15, 15, paint); 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(); //绘制三角形 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); // ----------设置渐变器后绘制---------- // 为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(45 , 10 , 10 , color.gray); // 绘制圆形 canvas.drawcircle(200, 40, 30, paint); // 绘制正方形 canvas.drawrect(170, 80, 230, 140, paint); // 绘制矩形 canvas.drawrect(170, 150, 230, 190, paint); rectf re3 = new rectf(170, 200, 230, 230); // 绘制圆角矩形 canvas.drawroundrect(re3, 15, 15, paint); rectf re31 = new rectf(170, 240, 230, 270); // 绘制椭圆 canvas.drawoval(re31, paint); path path5 = new path(); path5.moveto(170, 340); path5.lineto(230, 340); path5.lineto(200, 290); path5.close(); // 根据path进行绘制,绘制三角形 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(); // 根据path进行绘制,绘制五角形 canvas.drawpath(path6, paint); // ----------设置字符大小后绘制---------- paint.settextsize(24); paint.setshader(null); // 绘制7个字符串 canvas.drawtext(getresources().getstring(r.string.circle), 240, 50, paint); canvas.drawtext(getresources().getstring(r.string.square), 240, 120, paint); canvas.drawtext(getresources().getstring(r.string.rect), 240, 175, paint); canvas.drawtext(getresources().getstring(r.string.round_rect), 230, 220, paint); canvas.drawtext(getresources().getstring(r.string.oval), 240, 260, paint); canvas.drawtext(getresources().getstring(r.string.triangle), 240, 325, paint); canvas.drawtext(getresources().getstring(r.string.pentagon), 240, 390, paint); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP 判断常量,变量和函数是否存在
下一篇: 孙策临死前留下一句话,可惜孙权却没把握住
推荐阅读
-
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
-
ThinkPHP中图片按比例切割的代码实例
-
C# 填充Excel图表、图例背景色的实例代码
-
PHP类的使用 实例代码讲解
-
php上传文件,创建递归目录的实例代码
-
php实现 master-worker 守护多进程模式的实例代码
-
Android 实现抖音头像底部弹框效果的实例代码
-
Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)
-
Flutter 自定义Drawer 滑出位置的大小实例代码详解
-
Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)