十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇文章为大家展示了怎么在android中绘制一个几何图形,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联建站专注于中大型企业的成都网站设计、成都网站建设、外贸网站建设和网站改版、网站营销服务,追求商业策划与数据分析、创意艺术与技术开发的融合,累计客户上千,服务满意度达97%。帮助广大客户顺利对接上互联网浪潮,准确优选出符合自己需要的互联网运用,我们将一直专注品牌网站制作和互联网程序开发,在前进的路上,与客户一起成长!代码:
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 path2 = new Path(); path2.moveTo(10, 340);//将路径点设置到10,340位置 path2.lineTo(70, 340);//将起始路径点连接都70,340位置 path2.lineTo(40, 290);//将第二连接点连接到40,290位置 path2.close();//关闭路径的绘制 // 根据Path进行绘制,绘制三角形 canvas.drawPath(path2, paint); // 定义一个Path对象,封闭成一个五角形。 Path path3 = new Path(); path3.moveTo(26, 360); path3.lineTo(54, 360); path3.lineTo(70, 392); path3.lineTo(40, 420); path3.lineTo(10, 392); path3.close(); // 根据Path进行绘制,绘制五角形 canvas.drawPath(path3, 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 path4 = new Path(); path4.moveTo(90, 340); path4.lineTo(150, 340); path4.lineTo(120, 290); path4.close(); //绘制三角形 canvas.drawPath(path4, paint); Path path5 = new Path(); path5.moveTo(106, 360); path5.lineTo(134, 360); path5.lineTo(150, 392); path5.lineTo(120, 420); path5.lineTo(90, 392); path5.close(); //绘制五角形 canvas.drawPath(path5, 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 path6 = new Path(); path6.moveTo(170, 340); path6.lineTo(230, 340); path6.lineTo(200, 290); path6.close(); // 根据Path进行绘制,绘制三角形 canvas.drawPath(path6, paint); Path path7 = new Path(); path7.moveTo(186, 360); path7.lineTo(214, 360); path7.lineTo(230, 392); path7.lineTo(200, 420); path7.lineTo(170, 392); path7.close(); // 根据Path进行绘制,绘制五角形 canvas.drawPath(path7, 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); } }
上述内容就是怎么在android中绘制一个几何图形,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。