加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex 学习笔记 graphics(一)

发布时间:2020-12-15 04:52:11 所属栏目:百科 来源:网络整理
导读:1、Graphics 类概述 Graphics 类包含一组可用来创建矢量形状的方法。支持绘制的显示对象包括 Sprite 和 Shape 对象。这些类中的每一个类都包括 graphics 属性,该属性是一个 Graphics 对象。以下是为便于使用而提供的一些辅助函数: drawRect() 、 drawRound

1、Graphics 类概述

Graphics 类包含一组可用来创建矢量形状的方法。支持绘制的显示对象包括 Sprite 和 Shape 对象。这些类中的每一个类都包括 graphics 属性,该属性是一个 Graphics 对象。以下是为便于使用而提供的一些辅助函数:drawRect()drawRoundRect()drawCircle()drawEllipse()

无法通过 ActionScript 代码直接创建 Graphics 对象。如果调用 new Graphics(),则会引发异常。

Graphics 类是最终类;无法从其派生子类。

2、Graphics 一点示例:

下面定义了一个GraphicsExample 类,使用 GraphicsExample 类绘制圆形、圆角矩形和正方形。执行下列步骤可完成该任务:

  1. 声明 size 属性以备日后在确定每个形状的大小时使用。
  2. 声明以下属性:将背景色设置为橙色、将边框颜色设置为深灰色、将边框大小设置为 0 个像素、将角半径设置为 9 个像素并将舞台边缘与其他对象之间的间距设置为 5 个像素。
  3. 使用在前面步骤中声明的属性以及使用 Graphics 类的内置方法,可在坐标 x = 0,y = 0 处绘制圆形、圆角矩形和正方形。
  4. 沿舞台顶部重新绘制每个形状,起点为 x = 5,y = 5,各形状之间的间隔为 5 个像素。
?
 
 
  1. package?{ ?
  2. ????import?flash.display.DisplayObject; ?
  3. ????import?flash.display.Graphics; ?
  4. ????import?flash.display.Shape; ?
  5. ????import?flash.display.Sprite; ?
  6. ?
  7. ????public?class?GraphicsExample?extends?Sprite?{ ?
  8. ????????private?var?size:uint?????????=?80; ?
  9. ????????private?var?bgColor:uint??????=?0xFFCC00; ?
  10. ????????private?var?borderColor:uint??=?0x666666; ?
  11. ????????private?var?borderSize:uint???=?0; ?
  12. ????????private?var?cornerRadius:uint?=?9; ?
  13. ????????private?var?gutter:uint???????=?5; ?
  14. ?
  15. ????????public?function?GraphicsExample()?{ ?
  16. ????????????doDrawCircle(); ?
  17. ????????????doDrawRoundRect(); ?
  18. ????????????doDrawRect(); ?
  19. ????????????refreshLayout(); ?
  20. ????????} ?
  21. ?
  22. ????????private?function?refreshLayout():void?{ ?
  23. ????????????var?ln:uint?=?numChildren; ?
  24. ????????????var?child:DisplayObject; ?
  25. ????????????var?lastChild:DisplayObject?=?getChildAt(0); ?
  26. ????????????lastChild.x?=?gutter; ?
  27. ????????????lastChild.y?=?gutter; ?
  28. ????????????for?(var?i:uint?=?1;?i?<?ln;?i++)?{ ?
  29. ????????????????child?=?getChildAt(i); ?
  30. ????????????????child.x?=?gutter?+?lastChild.x?+?lastChild.width; ?
  31. ????????????????child.y?=?gutter; ?
  32. ????????????????lastChild?=?child; ?
  33. ????????????} ?
  34. ????????} ?
  35. ?
  36. ????????private?function?doDrawCircle():void?{ ?
  37. ????????????var?child:Shape?=?new?Shape(); ?
  38. ????????????var?halfSize:uint?=?Math.round(size?/?2); ?
  39. ????????????child.graphics.beginFill(bgColor); ?
  40. ????????????child.graphics.lineStyle(borderSize,?borderColor); ?
  41. ????????????child.graphics.drawCircle(halfSize,?halfSize,?halfSize); ?
  42. ????????????child.graphics.endFill(); ?
  43. ????????????addChild(child); ?
  44. ????????} ?
  45. ?
  46. ????????private?function?doDrawRoundRect():void?{ ?
  47. ????????????var?child:Shape?=?new?Shape(); ?
  48. ????????????child.graphics.beginFill(bgColor); ?
  49. ????????????child.graphics.lineStyle(borderSize,?borderColor); ?
  50. ????????????child.graphics.drawRoundRect(0,?0,?size,?cornerRadius); ?
  51. ????????????child.graphics.endFill(); ?
  52. ????????????addChild(child); ?
  53. ????????} ?
  54. ?
  55. ????????private?function?doDrawRect():void?{ ?
  56. ????????????var?child:Shape?=?new?Shape(); ?
  57. ????????????child.graphics.beginFill(bgColor); ?
  58. ????????????child.graphics.lineStyle(borderSize,?borderColor); ?
  59. ????????????child.graphics.drawRect(0,?size); ?
  60. ????????????child.graphics.endFill(); ?
  61. ????????????addChild(child); ?
  62. ????????} ?
  63. ????} ?
  64. } ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读