首页 / 操作系统 / Linux / Android UI开发专题之绘图基础
继续了解Android.graphics包中比较重要的绘图类。 一、 android.graphics.Matrix 有关图形的变换、缩放等相关操作常用的方法有: void reset() // 重置一个matrix对象。 void set(Matrix src) //复制一个源矩阵,和本类的构造方法Matrix(Matrix src) 一样 boolean isIdentity() //返回这个矩阵是否定义(已经有意义) void setRotate(float degrees)//指定一个角度以0,0为坐标进行旋转 void setRotate(float degrees,float px, float py) //指定一个角度以px,py为坐标进行旋转 void setScale(float sx, floatsy) // 缩放 void setScale(float sx, floatsy, float px, float py) //以坐标px,py进行缩放 void setTranslate(float dx,float dy) //平移 void setSkew (float kx, floatky, float px, float py) //以坐标px,py进行倾斜 void setSkew (float kx, floatky) //倾斜相关阅读:Android UI开发专题之界面设计 http://www.linuxidc.com/Linux/2011-08/41238.htm
Android UI开发专题之各种Drawable http://www.linuxidc.com/Linux/2011-08/41240.htm 二、android.graphics.NinePatch NinePatch是Android平台特有的一种非矢量图形自然拉伸处理方法,可以帮助常规的图形在拉伸时不会缩放,实例中Android开发网提示大家对于Toast的显示就是该原理,同时SDK中提供了一个工具名为Draw9-Patch,有关该工具的使用方法可以参考我们经发布的Draw 9-Patch使用方法介绍一文。由于该类提供了高质量支持透明的缩放方式,所以图形格式为PNG,文件命名方式为.9.png 的后缀比如android123.9.png。 三、android.graphics.Paint Paint类我们可以理解为画笔、画刷的属性定义,本类常用的方法如下: void reset() //重置 void setARGB(int a, int r, intg, int b) 或void setColor(int color) 均为设置Paint对象的颜色 void setAntiAlias(boolean aa)//是否抗锯齿,需要配合voidsetFlags (Paint.ANTI_ALIAS_FLAG) 来帮助消除锯齿使其边缘更平滑。 Shader setShader(Shadershader) //设置阴影,Shader类是一个矩阵对象,如果为NULL将清除阴影。 void setStyle(Paint.Stylestyle) //设置样式,一般为FILL 填充,或者STROKE凹陷效果。 void setTextSize(floattextSize) //设置字体大小 void setTextAlign(Paint.Alignalign) //文本对齐方式 Typeface setTypeface(Typefacetypeface) //设置字体,通过Typeface可以加载Android内部的字体,一般为宋体对于中文,部分ROM可以自己添加比如雅黑等等 void setUnderlineText(booleanunderlineText) //是否设置下划线,需要撇和voidsetFlags (Paint.UNDERLINE_TEXT_FLAG) 方法。 四、android.graphics.Rect Rect我们可以理解为矩形区域,类似的还有Point一个点,Rect类除了表示一个矩形区域位置描述外,android123提示主要可以帮助我们计算图形之间是否碰撞(包含)关系,对于Android游戏开发比较有用,其主要的成员contains包含了三种重载方法,来判断包含关系 boolean contains(int left, inttop, int right, int bottom) boolean contains(int x, int y) boolean contains(Rect r) 五、android.graphics.Region Region在Android平台中表示一个区域和Rect不同的是,它表示的是一个不规则的样子,可以是椭圆、多边形等等,而Rect仅仅是矩形。同样Region的booleancontains(int x, int y) 成员可以判断一个点是否在该区域内 六、android.graphics.Typeface Typeface类是帮助描述一个字体对象,在TextView中通过使用setTypeface方法来制定一个输出文本的字体,其直接构造调用成员create方法可以直接指定一个字体名称和样式,比如 static Typefacecreate(Typeface family, int style) static Typeface create(StringfamilyName, int style) 同时使用isBold和isItalic方法可以判断出是否包含粗体或斜体的字型。 final boolean isBold() final boolean isItalic() 该类的创建方法还有从apk的资源或从一个具体的文件路径,其具体方法为 static TypefacecreateFromAsset(AssetManager mgr, String path) static TypefacecreateFromFile(File path) static TypefacecreateFromFile(String path)