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讲义》
 
 
 
 

自定义绘制View的更多相关文章

  1. Flutter 自定义绘制 View

    在 Flutter 中自定义 View 有两种方式: 组合已有控件 自定义绘制 如何自定义绘制 有两个类做这件事情: CustomPaint :会在绘制阶段提供一个 Canvas 画布 CustomP ...

  2. 【转】Android绘制View的过程研究——计算View的大小

    Android绘制View的过程研究——计算View的大小 转自:http://liujianqiao398.blog.163.com/blog/static/18182725720121023218 ...

  3. Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)

    一.概述 Android实现圆角矩形,圆形或者椭圆等图形,一般主要是个自定义View加上使用Xfermode实现的.实现圆角图片的方法其实不少,常见的就是利用Xfermode,Shader.本文直接继 ...

  4. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  5. android绘制view的过程

    1 android绘制view的过程简单描述  简单描述可以解释为:计算大小(measure),布局坐标计算(layout),绘制到屏幕(draw):            下面看看每一步的动作到底是 ...

  6. 自定义View_2_关于自定义组合View

    自定义View(2) Android当中给我们提供了丰富的UI控件,当然也许满足不了我们的需求,我们就必须学会自定义自己的View,我们怎么算是自定义自己的view呢! 我们会根据原来有的View对V ...

  7. ASP.NET MVC学习笔记-----使用自定义的View Engine

    我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...

  8. Unity 扩展属性自定义绘制

    这么晚了准备睡觉的时候,去学习了一会. 发现一个标题好奇的点进去. 居然是自定义绘制属性.  在前几天这个问题把我难住了,没想到几分钟就能解决的问题. 我花了半天时间使用反射去解决...  如果我们想 ...

  9. android 开发 View _16 自定义计步器View、自定义柱状图View

    /** *@content:实现计步的环形View *@time:2018-7-30 *@build: */ public class CountStepsAnnularView extends Vi ...

随机推荐

  1. 《JavaScript高级程序设计》读书笔记

    Javascript由以下三部分组成: 核心(ECMAScript) 文档对象模型(DOM) 浏览器对象模型(BOM) ECMAScript组成部分: 语法.类型.语句.关键字.保留子.操作符.对象. ...

  2. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  3. CMake交叉编译配置

    很多时候,我们在开发的时候是面对嵌入式平台,因此由于资源的限制需要用到相关的交叉编译.即在你host宿主机上要生成target目标机的程序.里面牵扯到相关头文件的切换和编译器的选择以及环境变量的改变等 ...

  4. 跨控制器操作-thinkphp

    用A函数 或者 $use=new IndexController(); A跨控制器 $data->A("Admin/Index")//admin下面的index控制器 $da ...

  5. 初学Javascript对象

    <script> var p=new Object(); //属性 p.width=; p.height=; p.num=; p.autotime=; //方法 p.autoplay=fu ...

  6. c语言之函数指针

    一.基础研究 这里研究的内容是函数指针,需要我们在研究后构造程序来描述函数指针数组的用法和向函数传函数指针的方法. 指针有很多种:整型指针.结构体指针.数组指针等等,它们的本质是它们的值都是一个地址, ...

  7. HDU 3446 daizhenyang's chess

    http://acm.hdu.edu.cn/showproblem.php?pid=3446 题意:一个棋盘,有个KING,有一些能走的点,每次只能走到没走过的地方,没路可走的输,求先手是否必胜. 思 ...

  8. j2ee爬坑行之一:web容器

    什么是容器? servlet没用main方法,它们受控于另一个java应用程序,这个应用程序就称为容器. tomcat就是这样一个容器.当web服务器得到一个指向某servlet的请求,此时服务器不是 ...

  9. Android 有趣味的GridView

    工作这么久以来,都是以解决需求为目标.渐渐发现这种学习方式不好,学到的知识能马上解决问题,但没有经过梳理归纳.故想系统总结下一些有趣味的知识点.在这篇博客中想以一个例子系统讲解下GridView控件涉 ...

  10. sphinx,github和readthedocs配合使用

    http://daler.github.io/sphinxdoc-test/includeme.html http://pages.github.com/ http://www.lulinfeng.c ...