Android开发仿抖音底部加载进度条

Android开发仿抖音底部加载进度条

仿抖音底部加载进度条,从中间向两头伸的动画

一、思路:

自定义VideoLoadingBar控件

二、效果图:

在这里插入图片描述

三、关键代码:
// 联系:893151960
public class VideoLoadingBar extends View {

    private int mWidth;
    private RectF mBgRectF;
    private Paint mBgPaint;
    private Paint mFgPaint;
    private RectF mFgRectF;
    private float mRate;
    private boolean mLoading;
    private int mBgColor;//背景色
    private int mFgColor;//前景色

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

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

    public VideoLoadingBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LoadingBar);
        mBgColor = ta.getColor(R.styleable.LoadingBar_lb_bg_color, 0xff000000);
        mFgColor = ta.getColor(R.styleable.LoadingBar_lb_fg_color, 0xffffffff);
        ta.recycle();
        initPaint();
    }

    private void initPaint() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setDither(true);
        mBgPaint.setColor(mBgColor);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgRectF = new RectF();

        mFgPaint = new Paint();
        mFgPaint.setAntiAlias(true);
        mFgPaint.setDither(true);
        mFgPaint.setColor(mFgColor);
        mFgPaint.setStyle(Paint.Style.FILL);
        mFgRectF = new RectF();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getMeasuredWidth();
        int height = getMeasuredHeight();
        mBgRectF.top = 0;
        mBgRectF.bottom = height;
        mFgRectF.top = 0;
        mFgRectF.bottom = height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mBgRectF.left = 0;
        mBgRectF.right = mWidth;
        canvas.drawRect(mBgRectF, mBgPaint);
        if (mLoading) {
            if (mRate > 1) {
                mRate = 1;
            }
            float barWidth = mRate * mWidth;
            float left = (mWidth - barWidth) / 2;
            mFgRectF.left = left;
            mFgRectF.right = left + barWidth;
            canvas.drawRect(mFgRectF, mFgPaint);
            if (mRate < 1) {
                mRate += 0.1f;
                postInvalidateDelayed(20);
            } else {
                mRate = 0;
                postInvalidateDelayed(150);
            }
        }
    }

四、项目demo源码结构图:

在这里插入图片描述有问题或者需要完整源码的私信我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值