android-graphics--Canvas译

android.graphics
类 Canvas

相当于画布,控制着绘图的回调,为了画出东西,必须有4个基本组件:控制容纳像素的位图,一个主控绘制回调的画布(用于写位图),

绘图原语(如矩形,路径,文本),用于绘制的画笔。

 

嵌套类摘要
static classCanvas.EdgeType 边缘类型
static classCanvas.VertexMode 顶点模式,

TRIANGLES三角形,

TRIANGLE_STRIP去除三角形,TRIANGLE_FAN成扇形散开的三角形

字段介绍:

MATRIX_SAVE_FLAG

public static final int MATRIX_SAVE_FLAG 当restore()被回调时恢复当前的矩阵;

CLIP_SAVE_FLAG

public static final int CLIP_SAVE_FLAG 当restore()被回调时恢复当前的裁剪;

HAS_ALPHA_LAYER_SAVE_FLAG

public static final int HAS_ALPHA_LAYER_SAVE_FLAG图层需要的每一个像素的alpha;
 

FULL_COLOR_LAYER_SAVE_FLAG

public static final int FULL_COLOR_LAYER_SAVE_FLAG图层需要的每一个颜色组件的8位值;
 

CLIP_TO_LAYER_SAVE_FLAG

public static final int CLIP_TO_LAYER_SAVE_FLAG 图层裁剪的边界;
 

ALL_SAVE_FLAG

public static final int ALL_SAVE_FLAG当restore()回调时恢复所有的标记位;
 
     
     
构造方法详细信息

Canvas

public Canvas()构造空的画布,使用setBitmap填充指定的位图到画布;
 

Canvas

public Canvas(Bitmap bitmap)使用指定的位图填充的画布,位图必须可变;
 

Canvas

public Canvas(GL gl)使用指定的gl上下文构造画布,所有通过画布的绘制都将重定向到opengl中,注意:某些功能可能在这种模式下不支持(如有些GL的实现可能不支持抗锯齿或某些像ColorMatrix或影响某些Xfermodes)。然而,在这些情况下是不会抛出异常的。
 
方法详细:
 

getGL

public GL getGL()返回画布分配的GL对象,如果不支持GL就返回null;
 

freeGlCaches

public static void freeGlCaches()释放GL缓存,

setBitmap

public void setBitmap(Bitmap bitmap)

setViewport

public void setViewport(int width,
                        int height)
如果这是基于GL的画布就设置视窗尺寸。如果不是,这个方法将被忽略,没有异常。

isOpaque

public boolean isOpaque()当前设备绘制的图层是否是不透明的(即支不支持alpha);
 

getWidth

public int getWidth()得到当前绘制图层的宽;
 

getHeight

public int getHeight()
 

getDensityScale

public float getDensityScale()

返回这个画布的密度范围的支持位图的密度大小,表示为一个默认的密度的因素(160 dpi)。例如,位图设计为240 dpi显示将有一个密度1.5而位图的规模为160 dpi密度1.0的规模。

默认是Bitmap.DENSITY_SCALE_UNKNOWN.

 

setDensityScale

public void setDensityScale(float densityScale)

 

save

public int save()

保存当前矩阵在一个私人的堆栈。后续调用平移、缩放、旋转、倾斜、concat、clipRect clipPath都像往常一样运作,但当平衡恢复restore()调用,以上操作将被遗忘,并且这些存在的设置在save()前将被取消。返回保存的数量

save

public int save(int saveFlags)指定保存位;即哪一位被保存,在restore时才恢复;
 

saveLayer

public int saveLayer(RectF bounds,
                     Paint paint,
                     int saveFlags)
和save()一样,但是额外的它还分配了画面以外的位图,后续所有的绘制回调都直接映射那里,只有当restore()调用时屏幕外的绘制图像才会平移到画布上来(或者前一个图层),后续的平移、旋转等操作都是操作在此副本上,当restore()调用,这个副本就会被删除,前一个矩阵或者裁剪的状态就会恢复。

 

参数:
bounds - 可能是null。画面以外的位图需要的最大大小(以当地坐标)
paint - restore()调用时,应用在屏幕外的 Paint副本
saveFlags - see _SAVE_FLAG constants

saveLayer

public int saveLayer(float left,
                     float top,
                     float right,
                     float bottom,
                     Paint paint,
                     int saveFlags)

相当于上面的函数;

saveLayerAlpha

public int saveLayerAlpha(RectF bounds,
                          int alpha,
                          int saveFlags)功能类似上面 

saveLayerAlpha

public int saveLayerAlpha(float left,
                          float top,
                          float right,
                          float bottom,
                          int alpha,
                          int saveFlags)

 

restore

public void restore()

删除save()后操作的状态,调用次数不能超过save()的次数;

getSaveCount

public int getSaveCount()

restoreToCount

public void restoreToCount(int saveCount)

translate

public void translate(float dx,
                      float dy)

scale

public void scale(float sx,
                  float sy)

 

scale

public final void scale(float sx,
                        float sy,
                        float px,
                        float py)
参数:
sx - x轴放大的倍数
sy - The amount to scale in Y
px - x轴放大中心点(旋转也不会改变)
py - The y-coord for the pivot point (unchanged by the rotation)

rotate

public void rotate(float degrees)

rotate

public final void rotate(float degrees,
                         float px,
                         float py)

skew

public void skew(float sx,
                 float sy)错切
 

concat

public void concat(Matrix matrix)用指定的矩阵合成当前的矩阵,相当于求并集

setMatrix

public void setMatrix(Matrix matrix)
用指定的矩阵完全替换当前的矩阵,如果矩阵参数为null,那么将重置当前矩阵。
 

getMatrix

public void getMatrix(Matrix ctm)

返回当前画布矩阵的一个副本,对它进行操作并不会改变原矩阵;

 

getMatrix

public final Matrix getMatrix()

返回画布当前运动矩阵的一个副本矩阵;

clipRect

public boolean clipRect(RectF rect,
                        Region.Op op)

用指定的矩形裁剪

clipPath

public boolean clipPath(Path path,
                        Region.Op op)

用指定的路径进行裁剪;

clipRegion

public boolean clipRegion(Region region,
                          Region.Op op)

与clipRect()和clipPath()不同,并不会执行矩阵的变换;

 

getDrawFilter

public DrawFilter getDrawFilter()

setDrawFilter

public void setDrawFilter(DrawFilter filter)

quickReject

public boolean quickReject(RectF rect,
                           Canvas.EdgeType type)

getClipBounds

public boolean getClipBounds(Rect bounds)

getClipBounds

public final Rect getClipBounds()

drawRGB

public void drawRGB(int r,
                    int g,
                    int b)

用指定的RGB色填充整个位图;

drawARGB

public void drawARGB(int a,
                     int r,
                     int g,
                     int b)

drawColor

public void drawColor(int color)

drawColor

public void drawColor(int color,
                      PorterDuff.Mode mode)

 

drawPaint

public void drawPaint(Paint paint)

用画笔的配置填充画布的位图;

 

drawPoints

public void drawPoints(float[] pts,
                       int offset,
                       int count,
                       Paint paint)

画出一系列的点

drawLine

public void drawLine(float startX,
                     float startY,
                     float stopX,
                     float stopY,
                     Paint paint)

drawLines

public void drawLines(float[] pts,
                      int offset,
                      int count,
                      Paint paint)

画出一系列的线;

 

drawRect

public void drawRect(RectF rect,
                     Paint paint)

指定大小,画笔画出矩形;

drawOval

public void drawOval(RectF oval,
                     Paint paint)

画出椭圆;

drawCircle

public void drawCircle(float cx,
                       float cy,
                       float radius,
                       Paint paint)

画圆;

drawArc

public void drawArc(RectF oval,
                    float startAngle,
                    float sweepAngle,
                    boolean useCenter,
                    Paint paint)

画弧;

drawRoundRect

public void drawRoundRect(RectF rect,
                          float rx,
                          float ry,
                          Paint paint)

画圆角矩形;

drawPath

public void drawPath(Path path,
                     Paint paint)

用指定的路径画;

drawBitmap

public void drawBitmap(Bitmap bitmap,
                       float left,
                       float top,
                       Paint paint)

将指定位图绘制;

drawBitmap

public void drawBitmap(Bitmap bitmap,
                       Rect src,
                       RectF dst,
                       Paint paint)

将指定位图的大小位置画至指定的位置;

drawBitmap

public void drawBitmap(int[] colors,
                       int offset,
                       int stride,
                       float x,
                       float y,
                       int width,
                       int height,
                       boolean hasAlpha,
                       Paint paint)

drawBitmap

public void drawBitmap(Bitmap bitmap,
                       Matrix matrix,
                       Paint paint)

用指定的矩阵将原位图进行绘制;

 

drawBitmapMesh

public void drawBitmapMesh(Bitmap bitmap,
                           int meshWidth,
                           int meshHeight,
                           float[] verts,
                           int vertOffset,
                           int[] colors,
                           int colorOffset,
                           Paint paint)

位图网;

drawVertices

public void drawVertices(Canvas.VertexMode mode,
                         int vertexCount,
                         float[] verts,
                         int vertOffset,
                         float[] texs,
                         int texOffset,
                         int[] colors,
                         int colorOffset,
                         short[] indices,
                         int indexOffset,
                         int indexCount,
                         Paint paint)

drawText

public void drawText(CharSequence text,
                     int start,
                     int end,
                     float x,
                     float y,
                     Paint paint)

写字;

 

drawPosText

public void drawPosText(char[] text,
                        int index,
                        int count,
                        float[] pos,
                        Paint paint)

 

指定位置,数量写字;

drawPicture

public void drawPicture(Picture picture)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值