自定义绘制View

时间:2021-05-24 14:57:01
Paint(画笔)
自定义绘制View
 
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). 
自定义绘制View
  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讲义》