Android 画图常用类

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形), 1、首先说一下canvas类:

Class Overview

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

这个类相当于一个画布,你可以在里面画很多东西;

我们可以把这个Canvas理解成系统提供给我们的一块内存区域(但实际上它只是一套画图的API,真正的内存是下面的Bitmap),而且它还提供了一整套对这个内存区域进行操作的方法,所有的这些操作都是画图API。也就是说在这种方式下我们已经能一笔一划或者使用Graphic来画我们所需要的东西了,要画什么要显示什么都由我们自己控制。

这种方式根据环境还分为两种:一种就是使用普通View的canvas画图,还有一种就是使用专门的SurfaceView的canvas来画图。两种的主要是区别就是可以在SurfaceView中定义一个专门的线程来完成画图工作,应用程序不需要等待View的刷图,提高性能。前面一种适合处理量比较小,帧率比较小的动画,比如说象棋游戏之类的;而后一种主要用在游戏,高品质动画方面的画图。

下面是Canvas类常用的方法:

drawRect(RectF rect, Paint paint) //绘制区域,参数一为RectF一个区域

drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) //贴图,参数一就是我们常规的Bitmap对象,参数二是源区域(这里是bitmap),参数三是目标区域(应该在canvas的位置和大小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。

drawLine(float startX, float startY, float stopX, float stopY, Paintpaint) //画线,参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint 画刷对象。

drawPoint(float x, float y, Paint paint) //画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。

drawText(String text, float x, floaty, Paint paint) //渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数四是Paint对象。

drawOval( RectFoval, Paintpaint)//画椭圆,参数一是扫描区域,参数二为paint对象;

drawCircle(float cx, float cy, float radius, Paintpaint) // 绘制圆,参数一是中心点的x轴,参数二是中心点的y轴,参数三是半径,参数四是paint对象;

drawArc(RectFoval, float startAngle, float sweepAngle, boolean useCenter,Paintpaint)//画弧,

参数一是RectF对象,一个矩形区域椭圆形的界限用于定义在形状、大小、电弧,参数二是起始角(度)在电弧的开始,

参数三扫描角(度)开始顺时针测量的,参数四是如果这是真的话,包括椭圆中心的电弧,并关闭它,如果它是假这将是一个弧线,参数五是Paint对象;

还要理解一个paint类:

Class Overview

The Paint class holds the style and color information about how to draw geometries, text and bitmaps.

paint类拥有风格和颜色信息如何绘制几何学,文本和位图。

Paint 代表了Canvas上的画笔、画刷、颜料等等;

Paint类常用方法:

setARGB(int a, int r, int g, int b) // 设置 Paint对象颜色,参数一为alpha透明值

setAlpha(int a) // 设置alpha不透明度,范围为0~255

setAntiAlias(boolean aa) // 是否抗锯齿

setColor(int color) // 设置颜色,这里Android内部定义的有Color类包含了一些常见颜色定义

setTextScaleX(float scaleX) // 设置文本缩放倍数,1.0f为原始

setTextSize(float textSize) // 设置字体大小

setUnderlineText(booleanunderlineText) // 设置下划线


  Bitmap.createBitmap函数有6个重载方法

  1. public static Bitmap createBitmap (Bitmap src)
    从原位图src复制出一个新的位图,和原始位图相同

  2. public static Bitmap createBitmap (int[] colors, int width, int height, Bitmap.Config config)
    这个函数根据颜色数组来创建位图,注意:颜色数组的长度>=width*height

    此函数创建位图的过程可以简单概括为为:更加width和height创建空位图,然后用指定的颜色数组colors来从左到右从上至下一次填充颜色。config是一个枚举,可以用它来指定位图“质量”。

  3. public static Bitmap createBitmap (int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
    此方法与2类似,但我还不明白offset和stride的作用。

  4. public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
    从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图
    参数说明:
      Bitmap source:要从中截图的原始位图
      int x:起始x坐标
      int y:起始y坐标
    int width:要截的图的宽度
    intheight:要截的图的宽度
    Bitmap.Configconfig:一个枚举类型的配置,可以定义截到的新位图的质量
    返回值:返回一个剪切好的Bitmap
  5. public static Bitmap createBitmap (int width, int height, Bitmap.Config config)
    根据参数创建新位图

  6. public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)

    简单的剪切图像的方法,可以参考上面的4.


更多相关文章

  1. Android高手进阶教程(十七)之---Android中Intent传递对象的两种
  2. Android中将资源文件转为Bitmap对象
  3. 面向UDP的Android——PC双向通信(三):在Android客户端和PC服务器端
  4. Android调用WebService系列之KSoap2对象解析
  5. Android 模拟器创建参数说明
  6. Android Application对象必须掌握的七点
  7. android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入S
  8. Android判断网络状态是否断开+Android完全关闭应用程序+ 本文讲
  9. 参数设置

随机推荐

  1. Android 第七天(下午)
  2. Android Menu
  3. Android/java 多线程(六)-AsyncTask使用
  4. ios/android 程序员
  5. View基础知识总结
  6. android的init.rc文件的语法
  7. 解决WARNING: APP_PLATFORM android-19 i
  8. Android日志系统分析之日志设备驱动程序
  9. Android应用程序开发入门
  10. Android用户界面与布局