自定view实践(一) 自定义loadingView

     昨天同事遇到如下效果的加载view,实现了一下

        


    1.首先创建一个类继承view.

     

   public ProgreesView(Context context) {
        this(context,null);
    }

    public ProgreesView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ProgreesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        p = new Paint();
    }

          

     2.测量view的大小

    MeasureSpec的specMode,一共三种类型:

    EXACTLY:一般是设置了明确的值或者是MATCH_PARENT

   AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT

    UNSPECIFIED:表示子布局想要多大就多大,很少使用

    这里给出EXACTLY的测量值,AT_MOST模式给了一个固定值

   

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else {
            mWidth = 200;
        }


        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        } else {
            mHeight = 200;
        }
        setMeasuredDimension(mWidth, mHeight);
    }
      3.重写onDraw方法

       

 @Override
    protected void onDraw(Canvas canvas) {

        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(8);
        p.setAntiAlias(true);

        //百分比弧的矩形
        RectF rectF = new RectF(5, 5, mWidth - 5, mHeight - 5);

        p.setColor(Color.parseColor("#2EA4F2"));

        if (two < 360){
            if (progress < 360)
            progress += 10;

            if (progress == 360)
            two += 5;

            int per = progress-two;

            //绘制圆
            canvas.drawArc(rectF,two,per,false,p);

//            if (progress != 360){
                postInvalidateDelayed(100);

//            }

            Log.e("two ",two + "");
            Log.e("progress ",progress + "");
        }


    }
         这里用了canvas.drawArc画弧度 这个方法有5个参数,第一个参数Recf代表绘制的区域,第二个参数是其实的度数,第三个参数是画的弧度数.  刚开始先画360度弧,接着当第一条弧画满360度的时候,去不断改变起始弧的度数和所画弧的区域。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值