Paint(画笔)
 
Canvas(画布)
        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), anda paint (to describe the colors and styles for the drawing). 
  1. rotate(float degrees,float px,float py)//对canvas执行旋转变换
  2. scale(float sx,float sy)//对canvas执行缩放
  3. skew(float sx,float sy)//对canvas执行倾斜变换
  4. translate(float dx,float dy)//移动canvas,向右dx,向下dy
  5. //
  6. setBitmap(Bitmap bitmap)
 
Path(绘画路径)
    预先在View上将N个点连成一条“路径”,然后调用Canvas的drawPath(path,paint)即可沿着路径绘制图形
  1. //Path
  2. moveTo(float x,float y)//Set the beginning of the next contour to the point (x,y).
  3. lineTo(float x,float y)//Add a line from the last point to the specified point (x,y).
  4.  
  5. //canvas
  6. drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)//hOffset水平偏移 vOffset垂直偏移
     Android还为路径绘制提供了PathEffect来定义绘制效果,PathEffect包含如下子类:
 ComposePathEffect    CornerPathEffect    DashPathEffect    DiscretePathEffect    PathDashPathEffect    SumPathEffect
  1. Paint paint =newPaint();
  2. paint.setPathEffect(PathEffect effect);
  3.  
实例:采用双缓冲实现画图板
  1. publicclassDrawView extends View
  2. {
  3.     // 定义记录前一个拖动事件发生点的坐标
  4.     float preX;
  5.     float preY;
  6.     privatePath path;
  7.     publicPaint paint = null;
  8.     // 定义一个内存中的图片,该图片将作为缓冲区
  9.     Bitmap cacheBitmap = null;
  10.     // 定义cacheBitmap上的Canvas对象
  11.     Canvas cacheCanvas = null;
  12.  
  13.     publicDrawView(Context context,int width ,int height)
  14.     {
  15.         super(context);
  16.         // 创建一个与该View相同大小的缓存区
  17.         cacheBitmap =Bitmap.createBitmap(width, height,
  18.             Bitmap.Config.ARGB_8888);
  19.         cacheCanvas =newCanvas();
  20.         path =newPath();
  21.         // 设置cacheCanvas将会绘制到内存中的cacheBitmap上
  22.         cacheCanvas.setBitmap(cacheBitmap);
  23.  
  24.         // 设置画笔的颜色
  25.         paint =newPaint(Paint.DITHER_FLAG);
  26.         paint.setColor(Color.RED);
  27.         // 设置画笔风格
  28.         paint.setStyle(Paint.Style.STROKE);
  29.         paint.setStrokeWidth(1);
  30.         // 反锯齿
  31.         paint.setAntiAlias(true);
  32.         paint.setDither(true);
  33.     }
  34.  
  35.     @Override
  36.     public boolean onTouchEvent(MotionEvent event)
  37.     {
  38.         // 获取拖动事件的发生位置
  39.         float x = event.getX();
  40.         float y = event.getY();
  41.         switch(event.getAction())
  42.         {
  43.             caseMotionEvent.ACTION_DOWN:
  44.                 // 从前一个点绘制到当前点之后,把当前点定义成下次绘制的前一个点
  45.                 path.moveTo(x, y);
  46.                 preX = x;
  47.                 preY = y;
  48.                 break;
  49.             caseMotionEvent.ACTION_MOVE:
  50.                 // 从前一个点绘制到当前点之后,把当前点定义成下次绘制的前一个点
  51.                 path.quadTo(preX, preY, x, y);
  52.                 preX = x;
  53.                 preY = y;
  54.                 break;
  55.             caseMotionEvent.ACTION_UP:
  56.                 cacheCanvas.drawPath(path, paint);// ①
  57.                 path.reset();
  58.                 break;
  59.         }
  60.         invalidate();
  61.         // 返回true表明处理方法已经处理该事件
  62.         returntrue;
  63.     }
  64.     @Override
  65.     publicvoid onDraw(Canvas canvas)
  66.     {
  67.         Paint bmpPaint =newPaint();
  68.         // 将cacheBitmap绘制到该View组件上
  69.         canvas.drawBitmap(cacheBitmap,0,0, bmpPaint);// ②
  70.         // 沿着path绘制
  71.         canvas.drawPath(path, paint);
  72.     }
  73. }
 
整理自:《疯狂Android讲义》
 
 
 
 

最新文章

  1. .Net Log4Net配置多文件日志记录
  2. mysql 5.6 read-committed隔离级别下并发插入唯一索引导致死锁一例
  3. jsp学习笔记一
  4. 不用static,巧用对象.方法调用java中的函数
  5. HDU 5497 Inversion
  6. django 的auth.authenticate返回为None
  7. python 字典排序 关于sort()、reversed()、sorted()
  8. iis7伪静态
  9. OpenCV Mat 类型定义和赋值
  10. vue 使用总结
  11. linux之cal命令
  12. 01-UIKit
  13. 利用spring AOP实现每个请求的日志输出
  14. MySQL 常用语句总结
  15. 7-27 Codeforces Round #499 (Div. 2)
  16. Windows10 下安装 Apache2.4+PHP7.1+MySQL5.7
  17. format() expandtabs() 输入表格数据
  18. Javascript 京东轮播图
  19. JaveScript 中的正则表达式
  20. if __name__ == __'main'__: 判断讲解

热门文章

  1. Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解
  2. 初涉JavaScript模式系列 阶段总结及规划
  3. php中文件引入require
  4. jquery 与其他库冲突解决方案
  5. 栈的链式存储方法的C语言实现
  6. 多线程+fork 引发的bug查找
  7. How to solve “sudo: /etc/sudoers.d is world writable”
  8. 值得IT运维人员警示的“一件事儿”
  9. gnome/KDE安装,gnome出现问题,重新安装nvdia驱动
  10. 使用 CustomScript 扩展程序自动执行 Linux 虚拟机自定义任务