Android应用开发Android 开发自定义加载进度框解析
白羽 2019-03-15 来源 :网络 阅读 560 评论 0

摘要:本文将带你了解Android应用开发Android 开发自定义加载进度框解析,希望本文对大家学Android有所帮助。

    本文将带你了解Android应用开发Android 开发自定义加载进度框解析,希望本文对大家学Android有所帮助。



Android应用开发Android 开发自定义加载进度框解析


思路:
    自定义控件继承View 实现onLayout(),onDraw()方法获取控件的大小Canvas类绘制样式 传入参数改变进度框的样式状态
   

    //实现三个方法默认调用最多参数的一个
    public SuperCircleView(Context context) {
        this(context, null);
    }
     
    public SuperCircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
     
    public SuperCircleView(Context context, AttributeSet attrs, int   defStyleAttr) {
        super(context, attrs,   defStyleAttr);
    }
     
    //配置传入的参数 初始化画笔
    public SuperCircleView(Context context, AttributeSet attrs, int   defStyleAttr) {
        super(context, attrs,   defStyleAttr);
        TypedArray a =   context.obtainStyledAttributes(attrs, R.styleable.SuperCircleView);
        mMinRadio =   a.getInteger(R.styleable.SuperCircleView_min_circle_radio, 400);
        mRingWidth =   a.getFloat(R.styleable.SuperCircleView_ring_width, 40);
        mSelect =   a.getInteger(R.styleable.SuperCircleView_select, 7);
        mSelectAngle =   a.getInteger(R.styleable.SuperCircleView_selec_angle, 3);
     
        mMinCircleColor =   a.getColor(R.styleable.SuperCircleView_circle_color, Color.WHITE);
        mMaxCircleColor = a.getColor(R.styleable.SuperCircleView_max_circle_color,   context.getResources().getColor(R.color.huise2));
        mRingNormalColor =   a.getColor(R.styleable.SuperCircleView_ring_normal_color,   context.getResources().getColor(R.color.huise));
     
        isShowSelect =   a.getBoolean(R.styleable.SuperCircleView_is_show_select, false);
        mSelectRing = a.getInt(R.styleable.SuperCircleView_ring_color_select,   0);
        a.recycle();
     
        mMinRadio =   ConvertDpAndPx.Dp2Px(context, mMinRadio);
        mRingWidth =   ConvertDpAndPx.Dp2Px(context, mRingWidth);
        mSelect =   ConvertDpAndPx.Dp2Px(context, mSelect);
        mSelectAngle =   ConvertDpAndPx.Dp2Px(context, mSelectAngle);
     
        mPaint = new   Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
     
        this.setWillNotDraw(false);
     
        this.color[0] =   Color.parseColor(#4D993B);
        this.color[2] =   Color.parseColor(#4D993B);
        this.color[1] = Color.parseColor(#4D993B);
    }
   

    获取自定义控件的宽高、设置画笔


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int   bottom) {
        super.onLayout(changed, left,   top, right, bottom);
        mViewWidth =   getMeasuredWidth();
        mViewHeight =   getMeasuredHeight();
        mViewCenterX = mViewWidth /   2;
        mViewCenterY = mViewHeight /   2;
     
        mRectF = new RectF(mViewCenterX -   mMinRadio - mRingWidth / 2, mViewCenterY - mMinRadio - mRingWidth / 2,
                mViewCenterX + mMinRadio   + mRingWidth / 2, mViewCenterY + mMinRadio + mRingWidth / 2);
     
        mRingAngleWidth = (360 - mSelect   * mSelectAngle) / mSelect;
     
        mRectF1 = new RectF(mViewCenterX   - mMinRadio, mViewCenterY - mMinRadio,
                mViewCenterX + mMinRadio,   mViewCenterY + mMinRadio);
     
    }
   

    绘制图像
 
 
     @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**
         * 显示彩色断大于总共的段数是错误的
         */
        if (isShowSelect &&   mSelectRing > mSelect) {
            return;
        }
     
        mPaint.setColor(mMinCircleColor);
        //        canvas.drawCircle(mViewCenterX,   mViewCenterY, mMinRadio, mPaint);
        //画默认圆环
        drawNormalRing(canvas);
        //        //画灰色描边圆环
        //        drawNormalStrokeRing(canvas);
        //画彩色圆环
        drawColorRing(canvas);
    }
     
     
     
        import   android.content.Context;
        import   android.content.res.TypedArray;
        import   android.graphics.Canvas;
        import   android.graphics.Color;
        import   android.graphics.Paint;
        import   android.graphics.RectF;
        

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程