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. onresize的定义方式

    1.直接在html中定义如<body onresize="doResize()"/> 2.直接给onresize赋值给window和body的onresize赋值如wi ...

  2. Java学习----反复做某件事情

    for循环: public class TestFor{ public static void main(String[] args){ for(int x = 1; x < 3; x++) { ...

  3. 转:Remote debugging with Visual Studio 2010

    Original URL http://www.codeproject.com/Articles/146838/Remote-debugging-with-Visual-Studio-2010 you ...

  4. 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。

    引用里找到Microsoft.Office.Interop.Word右键属性 在嵌入互操作类型里,选上False就行了.

  5. django+nginx+supervisor+gunicorn+gevent 网站部署

    django+nginx+supervisor+gunicorn+gevent 网站部署 django,nginx,supervisor,gunicorn,gevent这几个都是在本领域大名鼎鼎的软件 ...

  6. cypress的EZ-USB对于USB的介绍

    Host is MasterThis is a fundamental USB concept. There is exactly onemaster in a USB system: the hos ...

  7. 符合altium designer操作习惯的cadence快捷键设置

    本人开始学习画PCB的时候,用的都是protel,后来转投altium desinger,因为这两个软件上手快且大学里教的也就是这两种.但由于工作需要换成cadence,这就给我造成了很大的困扰,尤其 ...

  8. 转:windows xp下如何安装SQL server2000企业版

    SQL2000企业版本 适用于WIN 2000 Server系统和Windows 2003系统,Windows XP一般装不了需要选用个人版或开发板.但是企业版也可以安装在xp系统下.这里介绍一个XP ...

  9. Zend Cache的学习和实例

    前一段时间,公司让我组织一下关于Zend Cache的培训. 培训的具体内容有: 前端core缓存 前端Output缓存 前端Function缓存 前端Class缓存 前端File缓存 前端Page缓 ...

  10. Android 判断当前网络连接类型

    实际应用开发时,如果存在需要用户获取大量数据的情况,最好是先判断下网络类型,提示用户当前的网络类型,是否需要连接Wifi,etc.(手机流量太贵啦,当然土豪是无视这玩意的, (/ □ \)). 定义网 ...