怎么在android中绘制一个几何图形
本篇文章为大家展示了怎么在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 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中绘制一个几何图形,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站题目:怎么在android中绘制一个几何图形
文章地址:http://myzitong.com/article/jgcpji.html