引路蜂二维图形画笔(Pen)示例含有四个示例,Lines ,Dashes ,LineJoin 和LineCap。打算采用Option Menu(主菜单)的方式来选择不同示例。
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站制作、成都网站制作、石屏网络推广、小程序开发、石屏网络营销、石屏企业策划、石屏品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供石屏建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
首先要对GuidebeeGraphics2DView,和Graphics2DActivity做些改动,从这个示例开 始,GuidebeeGraphics2DView需要动态绘制不同图形(可以通过菜单,或是Thread)。在 GuidebeeGraphics2DView增加下面两个方法:
- final Runnable updateCanvas = new Runnable() {
- public void run() {
- invalidate();
- }
- };
- public void refreshCanvas(){
- post(updateCanvas);
- }
post可以用在非UI Thread中Call UI Thread中方法。这里只是触发屏幕重绘事件以刷新屏幕显示。
在Graphics2DActivity增加一个变量protected GuidebeeGraphics2DView graphic2dView; 来获得对应的GuidebeeGraphics2DView实例。
graphic2dView=(GuidebeeGraphics2DView)findViewById(R.id.graphics2dview);
使用Option Menu,尽管也可以完全使用代码来创建菜单,更一般的方法是使用菜单资源。在res 下创建menu 子目录,然后在res\menu下创建menu_option_line.xml 用来显示Pen的四个示例选项:
定义菜单,
- android:icon=”@drawable/item1″
- android:title=”@string/item1″ />
- android:title=”@string/groupItem1″ />
- android:title=”@string/groupItem2″ />
定义好菜单资源后,就可以使用MenuInflater.inflate()展开菜单,一般需要在Activity的onCreateOptionsMenu()展开菜单:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu_option_line, menu);
- return true;
- }
最后是响应菜单事件:
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- menuOption = item.getItemId();
- drawImage();
- return true;
- }
item.getItemId(); 返回菜单的ID(在菜单资源中定义)。
完整代码如下:
- 1 public class Pen extends Graphics2DActivity {
- 2
- 3 int menuOption;
- 4
- 5 @Override
- 6 protected void drawImage() {
- 7 switch (menuOption) {
- 8 case R.id.mnuLines:
- 9 drawLines();
- 10 break;
- 11
- 12 case R.id.mnuDashes:
- 13 drawDash();
- 14 break;
- 15
- 16 case R.id.mnuLineCap:
- 17 drawLineCap();
- 18 break;
- 19
- 20 case R.id.mnuLineJoin:
- 21 drawLineJoin();
- 22 break;
- 23 default:
- 24 drawLines();
- 25 break;
- 26
- 27 }
- 28 graphic2dView.refreshCanvas();
- 29
- 30 }
- 31
- 32 @Override
- 33 public boolean onCreateOptionsMenu(Menu menu) {
- 34 MenuInflater inflater = getMenuInflater();
- 35 inflater.inflate(R.menu.menu_option_line, menu);
- 36 return true;
- 37 }
- 38
- 39 @Override
- 40 public boolean onOptionsItemSelected(MenuItem item) {
- 41 menuOption = item.getItemId();
- 42 drawImage();
- 43 return true;
- 44 }
- 45
- 46 private void drawLineJoin() {
- 47 Color blackColor = new Color(0xff000000);
- 48 Color whiteColor = new Color(0xffffffff);
- 49
- 50 com.mapdigit.drawing.geometry.Path path
- 51 = new com.mapdigit.drawing.geometry.Path();
- 52 path.moveTo(40, 60);
- 53 path.lineTo(90, 20);
- 54 path.lineTo(140, 60);
- 55 // Clear the canvas with white color.
- 56 graphics2D.clear(Color.WHITE);
- 57
- 58 AffineTransform matrix = new AffineTransform();
- 59 graphics2D.setAffineTransform(matrix);
- 60 com.mapdigit.drawing.Pen pen
- 61 = new com.mapdigit.drawing.Pen(blackColor,
- 62 20, com.mapdigit.drawing.Pen.CAP_BUTT,
- 63 com.mapdigit.drawing.Pen.JOIN_MITER);
- 64 graphics2D.draw(pen, path);
- 65 pen = new com.mapdigit.drawing.Pen(whiteColor, 1);
- 66 graphics2D.draw(pen, path);
- 67
- 68 matrix.translate(0, 50);
- 69 graphics2D.setAffineTransform(matrix);
- 70
- 71 pen = new com.mapdigit.drawing.Pen(blackColor, 20,
- 72 com.mapdigit.drawing.Pen.CAP_BUTT,
- 73 com.mapdigit.drawing.Pen.JOIN_ROUND);
- 74 graphics2D.draw(pen, path);
- 75 pen = new com.mapdigit.drawing.Pen(whiteColor, 1);
- 76 graphics2D.draw(pen, path);
- 77
- 78 matrix = new AffineTransform();
- 79 matrix.translate(0, 100);
- 80 graphics2D.setAffineTransform(matrix);
- 81
- 82 pen = new com.mapdigit.drawing.Pen(blackColor, 20,
- 83 com.mapdigit.drawing.Pen.CAP_BUTT,
- 84 com.mapdigit.drawing.Pen.JOIN_BEVEL);
- 85 graphics2D.draw(pen, path);
- 86 pen = new com.mapdigit.drawing.Pen(whiteColor, 1);
- 87 graphics2D.draw(pen, path);
- 88
- 89 }
- 90
- 91 private void drawLineCap() {
- 92 Color blackColor = new Color(0xff000000);
- 93 Color whiteColor = new Color(0xffffffff);
- 94 // Clear the canvas with white color.
- 95 graphics2D.clear(Color.WHITE);
- 96 AffineTransform matrix = new AffineTransform();
- 97 graphics2D.setAffineTransform(matrix);
- 98
- 99 com.mapdigit.drawing.Pen pen
- 100 = new com.mapdigit.drawing.Pen(blackColor,
- 101 20, com.mapdigit.drawing.Pen.CAP_BUTT,
- 102 com.mapdigit.drawing.Pen.JOIN_MITER);
- 103 graphics2D.drawLine(pen, 40, 60, 140, 60);
- 104 pen = new com.mapdigit.drawing.Pen(whiteColor, 1);
- 105 graphics2D.drawLine(pen, 40, 60, 140, 60);
- 106
- 107 pen = new com.mapdigit.drawing.Pen(blackColor, 20,
- 108 com.mapdigit.drawing.Pen.CAP_ROUND,
- 109 com.mapdigit.drawing.Pen.JOIN_MITER);
- 110 graphics2D.drawLine(pen, 40, 100, 140, 100);
- 111 pen = new com.mapdigit.drawing.Pen(whiteColor, 1);
- 112 graphics2D.drawLine(pen, 40, 100, 140, 100);
- 113
- 114 pen = new com.mapdigit.drawing.Pen(blackColor, 20,
- 115 com.mapdigit.drawing.Pen.CAP_SQUARE,
- 116 com.mapdigit.drawing.Pen.JOIN_MITER);
- 117 graphics2D.drawLine(pen, 40, 140, 140, 140);
- 118 pen = new com.mapdigit.drawing.Pen(whiteColor, 1);
- 119 graphics2D.drawLine(pen, 40, 140, 140, 140);
- 120 }
- 121
- 122 private void drawLines() {
- 123 Color greenColor = new Color(0xff00ff00);
- 124 // Clear the canvas with white color.
- 125 graphics2D.clear(Color.WHITE);
- 126 AffineTransform matrix = new AffineTransform();
- 127 graphics2D.setAffineTransform(matrix);
- 128
- 129 com.mapdigit.drawing.Pen pen
- 130 = new com.mapdigit.drawing.Pen(greenColor,1);
- 131 graphics2D.drawLine(pen, 20, 150, 60, 50);
- 132
- 133 pen = new com.mapdigit.drawing.Pen(greenColor, 2);
- 134 graphics2D.drawLine(pen, 40, 150, 80, 50);
- 135
- 136 pen = new com.mapdigit.drawing.Pen(greenColor, 3);
- 137 graphics2D.drawLine(pen, 60, 150, 100, 50);
- 138
- 139 pen = new com.mapdigit.drawing.Pen(greenColor, 5);
- 140 graphics2D.drawLine(pen, 80, 150, 120, 50);
- 141
- 142 pen = new com.mapdigit.drawing.Pen(greenColor, 7);
- 143 graphics2D.drawLine(pen, 100, 150, 140, 50);
- 144
- 145 pen = new com.mapdigit.drawing.Pen(greenColor, 10);
- 146 graphics2D.drawLine(pen, 120, 150, 160, 50);
- 147
- 148 }
- 149
- 150 private void drawDash() {
- 151 Color blackColor = new Color(0xff000000);
- 152 int dashArray1[] = { 2, 2 };
- 153 int dashArray2[] = { 6, 6 };
- 154 int dashArray3[] = { 4, 1, 2, 1, 1, 6 };
- 155 // Clear the canvas with white color.
- 156 graphics2D.clear(Color.WHITE);
- 157 AffineTransform matrix = new AffineTransform();
- 158 graphics2D.setAffineTransform(matrix);
- 159
- 160 com.mapdigit.drawing.Pen pen
- 161 = new com.mapdigit.drawing.Pen(blackColor,
- 162 20, com.mapdigit.drawing.Pen.CAP_BUTT,
- 163 com.mapdigit.drawing.Pen.JOIN_MITER, dashArray1, 0);
- 164 graphics2D.drawLine(pen, 40, 60, 140, 60);
- 165
- 166 pen = new com.mapdigit.drawing.Pen(blackColor, 20,
- 167 com.mapdigit.drawing.Pen.CAP_BUTT,
- 168 com.mapdigit.drawing.Pen.JOIN_MITER, dashArray2, 0);
- 169 graphics2D.drawLine(pen, 40, 100, 140, 100);
- 170
- 171 pen = new com.mapdigit.drawing.Pen(blackColor, 20,
- 172 com.mapdigit.drawing.Pen.CAP_BUTT,
- 173 com.mapdigit.drawing.Pen.JOIN_MITER, dashArray3, 0);
- 174 graphics2D.drawLine(pen, 40, 140, 140, 140);
- 175 }
- 176 }
网站名称:Android开发速成简洁教程十三:OptionMenu画笔示例
浏览路径:http://www.shufengxianlan.com/qtweb/news38/109888.html
成都网站建设公司_创新互联,为您提供做网站、网站维护、虚拟主机、手机网站建设、商城网站、移动网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联