当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(65):Graphics->Drawable->ShapeDrawable

除了在XML中使用Shape Drawable 资源外(如上例),也可以使用代码来定义Shape Drawable,下图为Android中定义的Shape 类层次图:

 \

如果熟悉二维图形开发,Path, Arc, Rect, Oval, RoundRect应该不陌生。 本例ShapeDrawable使用代码来创建各种ShapeDrawable 并自定义一个Shape :MyShapeDrawable.

Android 中的 Shader 类同于其它平台的画刷Brush (可以由单色画刷,线性渐变画刷,材质bitmap画刷等)。

ShapeDrawable 中的几个例子,只有第四个RoundRect 有些奇形怪状。这是因为绘制这个RoundRectShape的Paint 使用了PathEffect:

[java] 
PathEffect pe = new DiscretePathEffect(10, 4);  
PathEffect pe2 = new CornerPathEffect(4);  
mDrawables[3].getPaint().setPathEffect(  
 new ComposePathEffect(pe2, pe)); 

PathEffect pe = new DiscretePathEffect(10, 4);
PathEffect pe2 = new CornerPathEffect(4);
mDrawables[3].getPaint().setPathEffect(
 new ComposePathEffect(pe2, pe));
 

PathEffect 可以影响Paint绘制图形时几何形体的形状,Android提供了ComposePathEffect ,ConerPathEffect, DashPathEffec, DiscretePathEffect, PathDashPathEffect, SumPathEffect等几种PathEffect。其中DiscretePathEffect可以随机的偏移原路径位置,才有了本例的效果。

 \
作者:mapdigit
 
 


 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,