Android ApiDemo(十二)-- Graphics2

Shape

shape资源可以定义矩形(rectangle)、椭圆(oval)、线段(line)、环形(ring)四种简单二维图形,例如1、椭圆

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#00000000"/>
    <stroke android:width="4dp" android:color="#99000000"
            android:dashWidth="4dp" android:dashGap="2dp" />
    <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <corners android:radius="4dp" />
</shape>

线段

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
    <stroke android:width="1dp" android:color="#FF000000"
            android:dashWidth="1dp" android:dashGap="2dp" />
</shape>

shape默认情况下是矩形

MaskFilter

maskFilter可以对图像做一些边缘的处理,android中提供了两种效果
1、EmbossMaskFilter 浮雕效果
     EmbossMaskFilter (float[] direction, float ambient, float specular, float blurRadius)
     direction指定光源方向;light指定光源亮度;specular光等级;blurRadius模糊度
例如:
new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
2、BlurMaskFilter 模糊效果

     BlurMaskFilter (float radius, BlurMaskFilter.Blur style)

     radius范围大小?;style样式,一共有四种样式。

PathEffect    

pathEffect可以对通过path绘制的线条改变样式,有以下几种

1、CornerPathEffect,路径连接处平滑处理;

2、DashPathEffect,路径变为虚线 ;

3、PathDashPathEffect,对2的扩展,使虚线点具有形状;

4、ComposePathEffect,组合两种pathEffect;

5、DiscretePathEffect,使每段路径产生随机的偏移;

6、SumPathEffect,叠加两种pathEffect。

Picture

picture可以用来记录一个canvas的绘制过程,之后可以通过该picture重复相同的绘制,而且可以取得较高的性能。例如
mPicture = new Picture();
drawSomething(mPicture.beginRecording(200, 100));
mPicture.endRecording();

Matrix

Matrix可以提供一系列的矩阵变换,用于图形的平移,缩放,旋转,还提供了两种用于生成变换矩阵的方法:
1、点到点的变换 setPolyToPoly (float[] src, int srcIndex, float[] dst, int dstIndex, int pointCount) ;
这种变换通过点的个数可以达到特殊的效果,例如,一个点--平移,两个--旋转、缩放,三个点--旋转、变形,四个点--透视效果。
    1)src,源点坐标数组,形如[x0,y0,x1,y1,…];
    2)srcIndex,指出在1)的数组中从哪个点开始变换;
    3)dst,目标坐标数组,形式同1),用于与源点坐标对应;
    4)dstIndex,指出在3)中的数组中从哪个点用于对应;
    5)pointCount,用于对应点的个数。
2、矩形到矩形的变换 setRectToRect (RectF src, RectF dst, Matrix.ScaleToFit stf) ,其中Matrix.ScaleToFit用于指定变换方式,一共有三种变换方式:
    1)start,保持变换前矩形长宽比,最大限度填充变换后的矩形,并位于左上方;
    2)end,同1),但位于右下方;
    3)center,同1),位于中央;
    4)fill,最大限度填充,使长宽比满足变换后的矩形;

其他

1、canvas可以通过cavase.saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)来产生一个新的图层,当在新图层绘制完成以后,通过canvas.restore()将新图层与上一图层合并。

2、paint通过setTextSize()指定绘制文字的大小,getTextWidth()可以获得字符串中每一个字符的宽度;measureText()获得字符串总的宽度;getTextBound()获得所占矩形区域的大小。



你可能感兴趣的:(Android ApiDemo(十二)-- Graphics2)