当前位置: 移动技术网 > 移动技术>移动开发>Android > 记录一个自定义View ProgressBar

记录一个自定义View ProgressBar

2020年07月09日  | 移动技术网移动技术  | 我要评论

记录一个进度条的自定义控件,效果不错。

使用效果:

x效果图

自定义View 继承 ProgressBar

添加属性动画,可以设置背景,加载背景,圆角,加载动画执行时长。其实就是吧ProgressBar的方法封装一下,更方便的使用。没有什么技术难点。这里记录分享一下。方便各位同学 VC

public class FlikerProgressBar extends ProgressBar {

    private final Context mContext;

    /**
     * 背景颜色(默认为灰色)
     */
    private int mBackgroundColor = Color.LTGRAY;
    /**
     * 进度条颜色(默认为红色)
     */
    private int mProgressColor = Color.BLUE;
    /**
     * 背景弧度(默认为 0)
     */
    private float mRadius = 0f;
    /**
     * 动画时长(默认 500 毫秒)
     */
    private int mDuration = 500;

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

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

    public FlikerProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttrs(attrs);
        createDrawable();
    }


    public void setColor(int color) {
        this.mProgressColor = color;
        createDrawable();
    }


    public void initAttrs(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.FlikerProgressBar);
        mBackgroundColor = a.getColor(R.styleable.FlikerProgressBar_zpb_backgroundColor, mBackgroundColor);
        mProgressColor = a.getColor(R.styleable.FlikerProgressBar_zpb_progressColor, mProgressColor);
        mRadius = a.getDimension(R.styleable.FlikerProgressBar_zpb_radius, mRadius);
        mDuration = a.getInt(R.styleable.FlikerProgressBar_zpb_duration, mDuration);
        a.recycle();
    }

    private void createDrawable() {
        Drawable[] layers = new Drawable[2];
        Drawable background = makeBackground();
        Drawable progress = makeProgress();
        ClipDrawable clip = new ClipDrawable(progress
                , Gravity.LEFT, ClipDrawable.HORIZONTAL);
        layers[0] = background;
        layers[1] = clip;
        LayerDrawable layer = new LayerDrawable(layers);
        layer.setId(0, android.R.id.background);
        layer.setId(1, android.R.id.progress);
        setProgressDrawable(layer);
    }

    /**
     * 设置带动画进度
     *
     * @param progress 进度
     */
    public void setAnimProgress(int progress) {
        startAnimator(progress);
    }

    private void startAnimator(int progress) {
        ValueAnimator animator = ValueAnimator.ofInt(0, progress);
        animator.setDuration(mDuration);
        animator.start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                setProgress(value);
            }
        });
    }

    /**
     * 设置默认背景颜色
     *
     * @param color 色值
     */
    public void setDefBackgroundColor(int color) {
        this.mBackgroundColor = color;
        createDrawable();
    }

    /**
     * 设置进度条颜色
     *
     * @param color 色值
     */
    public void setProgressColor(int color) {
        this.mProgressColor = color;
        createDrawable();
    }

    /**
     * 设置背景弧度
     *
     * @param radius 弧度
     */
    public void setRadius(float radius) {
        this.mRadius = radius;
        createDrawable();
    }

    /**
     * 设置动画时长
     *
     * @param duration 时长
     */
    public void setDuration(int duration) {
        this.mDuration = duration;
    }

    /**
     * 生成背景样式 drawable
     *
     * @return drawable
     */
    private Drawable makeBackground() {
        return createShape(mRadius, mBackgroundColor);
    }

    /**
     * 生成 Progress 样式 drawable
     *
     * @return drawable
     */
    private Drawable makeProgress() {
        return createShape(mRadius, mProgressColor);
    }

    /**
     * 根据 radius 和 color 来创建 ShapeDrawable
     *
     * @param radius 弧度
     * @param color  颜色
     * @return drawable
     */
    private Drawable createShape(float radius, int color) {
        ShapeDrawable shape = new ShapeDrawable();
        // 设置弧度
        radius = dp2px(radius);
        float[] outerRadii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
        RoundRectShape roundShape = new RoundRectShape(outerRadii, null, null);
        shape.setShape(roundShape);
        // 设置颜色
        shape.getPaint().setColor(color);
        return shape;
    }

    private int dp2px(float dpValue) {
        return (int) (dpValue * (mContext.getResources().getDisplayMetrics().density) + 0.5f);
    }
}
FlikerProgressBar
 <!--滑动条-->
    <declare-styleable name="FlikerProgressBar">
        <!--背景颜色-->
        <attr name="zpb_backgroundColor" format="color" />
        <!--进度条颜色-->
        <attr name="zpb_progressColor" format="color" />
        <!--背景弧度-->
        <attr name="zpb_radius" format="dimension" />
        <!--动画时长-->
        <attr name="zpb_duration" format="integer" />
    </declare-styleable>

如果能够帮助到你,麻烦帮忙点下载,推荐一下。或者是打赏一瓶可乐也行(当然,这个就是妄想下)

 

本文地址:https://blog.csdn.net/qq_30974087/article/details/107183548

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网