博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RectF用法
阅读量:4135 次
发布时间:2019-05-25

本文共 1672 字,大约阅读时间需要 5 分钟。

自定义控件的时候经常会遇到需要画弧线、话矩形。 这就要用到RectF方法。

先解释一下RectF的几个属性

  • RectF.left 矩形左上角的x坐标。
  • RectF.top 矩形左上角的y坐标。
  • RectF.right 矩形右下角的y坐标。
  • RectF.right 矩形右下角的y坐标。
    在这里插入图片描述
    使用方法
public class MyView extends View {
private String TAG = "MyView "; private int mRadius; private int mX; private int mY; private float mCenterX; private float mCenterY; private RectF mRectF; public MyView (Context context, AttributeSet attrs) {
super(context, attrs); Log.i(TAG,"MyView "); } @Override public void draw(Canvas canvas) {
super.draw(canvas); Log.i(TAG,"draw"); //RectF设置 mRectF = new RectF(); mRectF.left = mCenterX - mRadius; mRectF.top =mCenterY - mRadius; mRectF.right = mCenterX + mRadius; mRectF.bottom = mCenterY + mRadius; //矩形画笔 Paint mRectPaint = new Paint(); mRectPaint.setColor(Color.BLUE); mRectPaint.setStrokeWidth(20); mRectPaint.setStyle(Paint.Style.STROKE); //画矩形 canvas.drawRect(mRectF,mRectPaint); //圆弧画笔 Paint mArcPaint = new Paint(); mArcPaint.setColor(Color.RED); mArcPaint.setStrokeWidth(20); mArcPaint.setStyle(Paint.Style.STROKE); //画圆弧 //startAngle 圆弧开始位置,从3点钟方向开始 //sweepAngle 圆弧弧度 //useCenter ,false为空心圆弧,true为实心(扇形)圆弧 canvas.drawArc(mRectF,0,180,false,mArcPaint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); Log.i(TAG,"onSizeChanged "); mX = w; mY = h; mRadius = w/4; mCenterX = mX/2; mCenterY = mY/2; }}

转载地址:http://tnsvi.baihongyu.com/

你可能感兴趣的文章
C++ set_union,set_intersection,set_difference
查看>>
第一章:左旋转字符串
查看>>
程序员编程艺术系列
查看>>
第二章:字符串是否包含问题
查看>>
简洁的heap代码
查看>>
最大团问题
查看>>
C++ make_heap,push_heap,pop_heap,sort_heap(以最大的K个数为例)
查看>>
第三章:寻找最小的k个数
查看>>
第三章续:O(n)复杂度算法
查看>>
Hash算法
查看>>
第三章再续:伴随数组求数组中给定下标区间内的第K小(大)元素
查看>>
第四章:一些字符串函数的实现
查看>>
第五章:寻找满足和为定值的两个或多个数
查看>>
动态规划6:最长递增子序列问题
查看>>
第六章:求解500万以内的亲和数
查看>>
第八章:虚函数笔记(虚函数碉堡了)
查看>>
动态规划7:最长公共子序列(LCS)
查看>>
第十二章:数的判断
查看>>
第十三章:遍历n个元素取出等概率随机取出其中之一元素
查看>>
第十四章:提取出某日访问百度次数最多的那个IP
查看>>