canvas教程

Android 自定义View学习(二)(2)

字号+ 作者:H5之家 来源:H5之家 2017-03-21 11:02 我要评论( )

RectF holds four float coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom). These fields can be accessed directly. Use width() and he

RectF holds four float coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom). These fields can be accessed directly. Use width() and height() to retrieve
the rectangle's width and height. Note: most methods do not check to see that the coordinates are sorted correctly (i.e. left <= right and top <= bottom).

两者差别就是:Rect 坐标为integer 而RectF 坐标为float

使用:

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect = new RectF(100,100,200,200); canvas.drawRect(rect,paint); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); RectF rect = new RectF(100.5f,100.5f,200.5f,200.5f); canvas.drawRect(rect,paint); }

注意构造方法中的参数顺序

2.2 drawCricle() 绘制圆形
  • drawCircle(float cx, float cy, float radius, @NonNull Paint paint)
  • Draw the specified circle using the specified paint. If radius is <= 0, then nothing will be drawn. The circle will be filled or framed based on the Style in the paint.
    @param cx The x-coordinate of the center of the cirle to be drawn
    @param cy The y-coordinate of the center of the cirle to be drawn
    @param radius The radius of the cirle to be drawn
    @param paint The paint used to draw the circle

    radius: 半径
    cx : 圆心的x坐标
    cy : 圆心的y坐标
    使用的时候需要考虑圆心和半径


    绘制圆形

    使用:

    @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float width = getWidth(); float height = getHeight(); float radius = Math.min(width,height)/2; canvas.drawCircle(width/2,height/2,radius,paint); }

    绘制圆形时,半径是宽和高中较小者的二分之一

    2.3 drawArc() 绘制扇形
  • drawArc(float left, float top, float right, float bottom, float startAngle,float sweepAngle, boolean useCenter, @NonNull Paint paint)
  • drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)
  • 两个方法的差别:

  • 方法2把坐标封装进RectF对象中
  • 方法1要求系统最低为21
  • drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)

    @param oval The bounds of oval used to define the shape and size of the arc
    @param startAngle Starting angle (in degrees) where the arc begins
    @param sweepAngle Sweep angle (in degrees) measured clockwise
    @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge
    @param paint The paint used to draw the arc

  • float startAngle 开始绘制的角度
  • float sweepAngle 扇形扫过的角度,并不是停止时的角度。停止角度 = startAngle+ sweepAngle
  • boolean useCenter ture就是有焦点圆心 , false 没有

  • 扇形,有焦点圆心

    @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); RectF rect = new RectF(0f,0f,500f,500f); canvas.drawArc(rect,0,60,true,paint); canvas.drawArc(rect,60,30,true,paint_2); }

    此时的boolean useCenter为true

    当把boolean useCenter设置为false时


    扇形无焦点圆形

    此时之画出了开始点和结束点两点之间的区域

    2.4 drawBitmap() 绘制Bitmap
  • drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)
  • @param bitmap The bitmap to be drawn
    @param left The position of the left side of the bitmap being drawn
    @param top The position of the top side of the bitmap being drawn
    @param paint The paint used to draw the bitmap (may be null)

  • left 左上角横坐标
  • top 左上角纵坐标

  • 绘制bitmap

    @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); float width = (getWidth()-bitmap.getWidth())/2; float height = (getHeight()-bitmap.getHeight())/2; canvas.drawBitmap(bitmap,width,height,paint); }

    根据left和top确定绘制的位置,此时Paint的用于绘制文字的属性设置在绘制Bitmap时是无效的。

    2.5 drawText()绘制文字
  • drawText(@NonNull String text, float x, float y, @NonNull Paint paint)

     

  • 1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • Android给图片添加水印的三步方法

      Android给图片添加水印的三步方法

      2017-03-18 13:00

    • Android模仿支付宝支付密码输入框效果

      Android模仿支付宝支付密码输入框效果

      2017-03-18 12:01

    • Android实现九宫格图案解锁功能

      Android实现九宫格图案解锁功能

      2017-03-17 16:01

    • Android SDK 百度网盘下载地址链接

      Android SDK 百度网盘下载地址链接

      2017-03-13 09:07

    网友点评