欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android绘制几何图形的实例代码

程序员文章站 2023-11-17 19:55:16
本文实例为大家分享了android绘制几何图形展示的具体代码,供大家参考,具体内容如下 效果图: 代码(仅绘制类,不可直接运行): public...

本文实例为大家分享了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 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); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。