千家信息网

Android开发中如何理解RadioButton及路径绘制

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章将为大家详细讲解有关Android开发中如何理解RadioButton及路径绘制,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。这个例子是绘制多
千家信息网最后更新 2026年11月04日Android开发中如何理解RadioButton及路径绘制

这篇文章将为大家详细讲解有关Android开发中如何理解RadioButton及路径绘制,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

这个例子是绘制多边形,多义形和路径,采用单选钮RadioButton来选择Polys 和Path示例:

UI 设计为 上部分用来显示绘图内容,下部分为两个单选按钮 Polys ,Path。这样layout就和main.xml 不一样,main.xml只含一个com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 res\layout下新建一个polys.xml:

                                  

RadioButton 需包含在RadioGroup中做为一个分组,这里将Polys 设为选中。

定义好Layout资源后,修改 Path.java

private RadioButton radioPoly;     private RadioButton radioPath;        public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.polys);      graphic2dView       = (GuidebeeGraphics2DView)         findViewById(R.id.graphics2dview);      radioPath = (RadioButton) findViewById(R.id.radioPath);      radioPoly = (RadioButton) findViewById(R.id.radioPolys);      radioPath.setOnClickListener(this);      radioPoly.setOnClickListener(this);     }

应为需要处理按键消息,所以定义了两个RadioButton对象,可以通过findViewById获取实例。因为两个RadioButton这里采用 同样的处理方法,可以让Path实现OnClickListener ,即:public class Path extends Graphics2DActivity implements OnClickListener。完整代码如下:

1   public class Path extends Graphics2DActivity 2      implements OnClickListener { 3     4       private RadioButton radioPoly; 5       private RadioButton radioPath; 6     7       public void onCreate(Bundle savedInstanceState) { 8           super.onCreate(savedInstanceState); 9           setContentView(R.layout.polys); 10          graphic2dView 11           = (GuidebeeGraphics2DView) 12             findViewById(R.id.graphics2dview); 13          radioPath = (RadioButton) findViewById(R.id.radioPath); 14          radioPoly = (RadioButton) findViewById(R.id.radioPolys); 15          radioPath.setOnClickListener(this); 16          radioPoly.setOnClickListener(this); 17      } 18    19      @Override 20      protected void drawImage() { 21          if (radioPoly.isChecked()) { 22              drawPolys(); 23          } else { 24              drawPaths(); 25          } 26          graphic2dView.refreshCanvas(); 27    28      } 29    30      @Override 31      public void onClick(View view) { 32          drawImage(); 33      } 34    35      private void drawPaths() { 36          AffineTransform mat1; 37    38          // The path. 39          com.mapdigit.drawing.geometry.Path path; 40    41          // Colors 42          Color redColor = new Color(0x96ff0000, true); 43          Color greenColor = new Color(0xff00ff00); 44          Color blueColor = new Color(0x750000ff, true); 45    46          String pathdata 47             = "M 60 20 Q -40 70 60 120 Q 160 70 60 20 z"; 48          mat1 = new AffineTransform(); 49          mat1.translate(30, 40); 50          mat1.rotate(-30 * Math.PI / 180.0); 51          path = com.mapdigit.drawing.geometry.Path.fromString(pathdata); 52          // Clear the canvas with white color. 53          graphics2D.clear(Color.WHITE); 54    55          graphics2D.setAffineTransform(new AffineTransform()); 56          SolidBrush brush = new SolidBrush(greenColor); 57          graphics2D.fill(brush, path); 58          graphics2D.setAffineTransform(mat1); 59    60          brush = new SolidBrush(blueColor); 61          com.mapdigit.drawing.Pen pen 62             = new com.mapdigit.drawing.Pen(redColor, 5); 63          graphics2D.setPenAndBrush(pen, brush); 64          graphics2D.draw(null, path); 65          graphics2D.fill(null, path); 66    67      } 68    69      private void drawPolys() { 70          AffineTransform mat1; 71    72          // Colors 73          Color redColor = new Color(0x96ff0000, true); 74          Color greenColor = new Color(0xff00ff00); 75          Color blueColor = new Color(0x750000ff, true); 76    77          Polyline polyline; 78          Polygon polygon; 79          Polygon polygon1; 80    81          String pointsdata1 82          = "59,45,95,63,108,105,82,139,39,140,11,107,19,65"; 83          mat1 = new AffineTransform(); 84          mat1.translate(30, 40); 85          mat1.rotate(-30 * Math.PI / 180.0); 86          polyline = new Polyline(); 87          polygon = new Polygon(); 88          polygon1 = new Polygon(); 89          Point[] points = Point.fromString(pointsdata1); 90          for (int i = 0; i < points.length; i++) { 91              polyline.addPoint(points[i].x, points[i].y); 92              polygon.addPoint(points[i].x, points[i].y); 93              polygon1.addPoint(points[i].x, points[i].y); 94          } 95          // Clear the canvas with white color. 96          graphics2D.clear(Color.WHITE); 97    98          graphics2D.setAffineTransform(new AffineTransform()); 99          SolidBrush brush = new SolidBrush(greenColor); 100         graphics2D.fillPolygon(brush, polygon); 101         graphics2D.setAffineTransform(mat1); 102   103         brush = new SolidBrush(blueColor); 104         com.mapdigit.drawing.Pen pen 105            = new com.mapdigit.drawing.Pen(redColor, 5); 106         graphics2D.setPenAndBrush(pen, brush); 107         graphics2D.fillPolygon(null, polygon1); 108         graphics2D.drawPolyline(null, polyline); 109   110     } 111   112 }

关于Android开发中如何理解RadioButton及路径绘制就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

路径 两个 内容 开发 文章 更多 知识 篇文章 处理 不错 代码 例子 可以通过 多边形 实例 对象 按钮 按键 方法 消息 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 科技金融是不是互联网金融 重庆互联网络科技 广州互联网科技公司 北京互联网科技有限公司 宜安科技 互联网金融 互联网科技公司有哪些 互联网巨头投资的硬核科技 互联网科技公司招聘 成都玖崟互联网科技有限公司乐商圈怎么样 中盈科技互联网信息有限公司 佛山市翊博互联网络科技有限公司 元气玛特互联网科技公司是外包吗 中国互联网有什么科技 产业互联网科技服务公司 互联网科技资金流入 广州三三互联网科技有限公司 上海互联网科技园景观 互联网科技创新方面的资料 前海云享互联网科技 颐高云石互联网科技 重生之互联网科技帝国 远信互联网科技 互联网科技的英文 互联网 新科技 互联网科技博客 科技赋能视角下互联网保险发展 南通创西澳互联网科技有限公司 21世纪互联网科技成就 吉林省码上互联网科技电话 科技互联网企业在行动
0