自定义Loading

本文详细介绍了一种自定义LoadingView组件的方法,该组件可在Android应用中实现动态加载效果。文章深入探讨了如何通过ValueAnimator控制加载动画,以及如何利用属性设置加载视图的颜色、线条宽度和长度。此外,还提供了在XML中定义样式属性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

public class LoadingView extends View {

    private int mWidth;
    private int mHeight;
    private int mCenterX;
    private int mCenterY;
    private Paint mPaint;
    // 默认loading的颜色
    private final int mDefaultColor = 0xffffff;
    // 默认loading线条的宽度
    private final float mDefaultLineWidth = 10;
    // 默认loading线条的高度
    private final float mDefaultLineLength = 20;
    private float mLineWidth = mDefaultLineWidth;
    private int mLineColor = mDefaultColor;
    private float mLineLength = mDefaultLineLength;
    private int control = 1;
    ValueAnimator animator;

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

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingView);
        try {
            mLineColor = typedArray.getColor(R.styleable.LoadingView_lineColor, mDefaultColor);
            mLineLength = typedArray.getDimension(R.styleable.LoadingView_lineLength, mDefaultLineLength);
            mLineWidth = typedArray.getDimension(R.styleable.LoadingView_lineWidth, mDefaultLineWidth);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(typedArray != null) {
                typedArray.recycle();
            }
        }
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mLineColor);
        mPaint.setStrokeWidth(mLineWidth);

        animator = ValueAnimator.ofInt(12, 1);
        animator.setDuration(1000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                control = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();

        setBackground(context.getResources().getDrawable(R.drawable.background));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        mCenterX = mWidth / 2;
        mCenterY = mHeight / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < 12; i++) {
            mPaint.setAlpha(((i + 1 + control) % 12 * 255) / 12);
            canvas.drawLine(mCenterX, mCenterY - mLineLength, mCenterX, mCenterY - mLineLength * 2, mPaint);
            canvas.rotate(30, mCenterX, mCenterY);
        }
    }

    public void stopLoading(){
        if (null != animator && animator.isRunning()) {
            animator.cancel();
            this.setVisibility(GONE);
        }
    }
}

 values文件夹下创建attrs.xml

<resources>
    <declare-styleable name="LoadingView">
        <attr name="lineColor" format="color"></attr>
        <attr name="lineLength" format="dimension"></attr>
        <attr name="lineWidth" format="dimension"></attr>
    </declare-styleable>
</resources>

drawable文件夹下创建圆角背景

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#80000000"/>
    <corners
        android:radius="18dp"/>
</shape>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值