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() 绘制圆形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(@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
扇形,有焦点圆心
@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@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)
绘制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()绘制文字