自定義Loading

 

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>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章