打造属于自己的进度条(笔记)

根据从前面的学习,初步学会了基本的自定义控件的方法,就开始学习者写一下自定义进度条玩哇
下面是效果图
这里写图片描述
下面的是代码部分
首先自定义属性

 <declare-styleable name="LoadView">
        <attr name="loadWidth" format="dimension">20</attr>
        <attr name="loadColor" format="color">0xffababab</attr>
        <attr name="loadBackgroundColor" format="color">0xff343434</attr>
        <attr name="backgroundColor" format="color">0xff565656</attr>
        <attr name="textColor" format="color">0xffbcbcbc</attr>
        <attr name="radius" format="dimension">60</attr>
        <attr name="loadTextSize" format="dimension">28</attr>
    </declare-styleable>

下面是自定义V进度条代码

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

/**
 * Created by Andream on 2017/11/4.
 * 自定义动感加载图
 */

public class LoadView extends ProgressBar{
    private final int RADIUS=60;
    private final int LOAD_WIDTH=20;
    private final int TEXT_SIZE=28;
    private final int LOAD_COLOR=0xffababab;
    private final int LOAD_BACKGROUND_COLOR=0xff343434;
    private final int BACKGROUND_COLOR=0xff565656;
    private final int TEXT_COLOR=0xffbcbcbc;
    private float loadWidth=dp2sp(LOAD_WIDTH);
    private float radius=dp2sp(RADIUS);
    private int textSize=sp2dp(TEXT_SIZE);
    private int loadColor=LOAD_COLOR;
    private int loadBackgroundColor=LOAD_BACKGROUND_COLOR;
    private int backgroundColor=BACKGROUND_COLOR;
    private int textColor=TEXT_COLOR;

    private Paint mPaint;
    private RectF oval;
    private Rect rect;
    private Bitmap mTarget,mBmp;
    public LoadView(Context context) {
        this(context,null);
    }

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

    public LoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyledAttrs(attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        oval=new RectF();
        rect = new Rect();
    }
    /**
     * 获取自定义属性
     * @param attrs attrs
     */
    private void obtainStyledAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.LoadView);
        loadWidth = (int) ta.getDimension(R.styleable.LoadView_loadWidth, loadWidth);
        loadColor = (int) ta.getDimension(R.styleable.LoadView_loadColor, loadColor);
        backgroundColor = (int) ta.getDimension(R.styleable.LoadView_backgroundColor, backgroundColor);
        loadBackgroundColor = (int) ta.getDimension(R.styleable.LoadView_loadBackgroundColor, loadBackgroundColor);
        textColor = (int) ta.getDimension(R.styleable.LoadView_textColor, textColor);
        radius = (int) ta.getDimension(R.styleable.LoadView_radius, radius);
        textSize = (int) ta.getDimension(R.styleable.LoadView_loadTextSize, textSize);
        ta.recycle();
    }

    /**
     * dp2sp
     * @param dpVal sp
     * @return dp
     */
    private int dp2sp(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }

    /**
     * sp2dp
     * @param spVal dp
     * @return sp
     */
    private int sp2dp(int spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics());
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            int widthSize = (int) (radius*2f+loadWidth*2f + getPaddingLeft() + getPaddingRight());
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        }
        if (heightMode != MeasureSpec.EXACTLY) {
            int heightSize = (int) (radius*2f+loadWidth*2f + getPaddingTop() + getPaddingBottom());
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        float mWidth=getWidth();
        float mHeight=getHeight();
        mPaint.setColor(backgroundColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(mWidth/2f,mHeight/2,radius,mPaint);


     //绘画图片
        oval.set(mWidth / 2 - radius, mWidth / 2 - radius, mHeight / 2
                + radius, mHeight / 2+ radius);//用于定义的圆弧的形状和大小的界限
         mBmp= BitmapFactory.decodeResource(getResources(),R.mipmap.mytest);
        //canvas.drawBitmap(bitmap,new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),oval,mPaint);
        if(mTarget == null){
            mTarget = Bitmap.createScaledBitmap(mBmp, (int) (radius*2+loadWidth), (int) (radius*2+loadWidth), false);
        }
        if(mTarget!=mBmp){
            mBmp.recycle();
        }
        Bitmap target = Bitmap.createBitmap(mTarget, 0, 0, mTarget.getWidth(), mTarget.getHeight());

        float sx = mWidth/2f - target.getWidth() / 2;
        float sy = mHeight/2f- target.getHeight() / 2;
        canvas.drawBitmap(target, sx, sy, mPaint);


        mPaint.setColor(textColor);
        mPaint.setTextSize(textSize);
        String progressText= getProgress()+"%";
        mPaint.getTextBounds(progressText, 0, progressText.length(), rect);//获取文字宽高
        canvas.drawText(progressText,mWidth/2f-rect.width()/2f,mHeight/2f+rect.height()/2f,mPaint);

        mPaint.setColor(loadBackgroundColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(loadWidth);

        canvas.drawArc(oval, 0, 360, false, mPaint);  //根据进度画圆弧
        mPaint.setColor(loadColor);
        canvas.drawArc(oval, 270, (getProgress()*3.6f), false, mPaint);  //根据进度画圆弧
        canvas.restore();
    }
}

这就是全部的了,是不是感觉很简单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值