自定義loading(轉(zhuǎn)圈的圓點(diǎn))

網(wǎng)上例子不少 當(dāng)然我也是看了那個(gè)AV什么什么的里面寫法 我自己這個(gè)就是簡(jiǎn)單罷了 正好項(xiàng)目用 我就弄出來(lái)了 順便寫這上面留作備用吧


效果圖!!水印擋的嚴(yán)嚴(yán)實(shí)實(shí) 破軟件
/**
 * @ProjectName: BigDemo
 * @Package: com.example.bigdemo
 * @ClassName: CustomLoadingView
 * @Description: 自定義loadingView
 * @Author: 鄭偉
 * @CreateDate: 2019/8/23 18:38
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/23 18:38
 * @UpdateRemark: 更新說(shuō)明
 * @Version: 1.0
 */
public class CustomLoadingView extends View {

    public static final float SCALE = 1.0f;

    public static final int ALPHA = 255;

    private Paint mPaint;

    private ArrayList<ValueAnimator> mAnimators;

    private HashMap<ValueAnimator, ValueAnimator.AnimatorUpdateListener> mUpdateListeners = new HashMap<>();

    private float[] scaleFloats = new float[]{SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE};

    private int[] alphas = new int[]{ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA};

    public CustomLoadingView(Context context) {
        super(context);
        init(context);
    }

    public CustomLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(0x642700);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        onCreateAnimators();
        startAnimators();
    }


    /**
     * 啟動(dòng)動(dòng)畫
     */
    public void startAnimators() {
        for (int i = 0; i < mAnimators.size(); i++) {
            ValueAnimator animator = mAnimators.get(i);
            ValueAnimator.AnimatorUpdateListener updateListener = mUpdateListeners.get(animator);
            if (updateListener != null) {
                animator.addUpdateListener(updateListener);
            }
            animator.start();
        }
    }

    /**
     * 停止動(dòng)畫
     */
    public void stopAnimators() {
        if (mAnimators != null) {
            for (ValueAnimator animator : mAnimators) {
                if (animator != null && animator.isStarted()) {
                    animator.removeAllUpdateListeners();
                    animator.end();
                }
            }
        }
    }

    public void setColor(int color){
        mPaint.setColor(color);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST) {
            widthSize = 50;
        }

        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = 50;
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float radius = getWidth() / 10;
        for (int i = 0; i < 8; i++) {
            canvas.save();
            Point point = circleAt(getWidth(), getHeight(), getWidth() / 2 - radius, i * (Math.PI / 4));
            canvas.translate(point.x, point.y);
            canvas.scale(scaleFloats[i], scaleFloats[i]);
            mPaint.setAlpha(alphas[i]);
            canvas.drawCircle(0, 0, radius, mPaint);
            canvas.restore();
        }
    }


    private Point circleAt(int width, int height, float radius, double angle) {
        float x = (float) (width / 2 + radius * (Math.cos(angle)));
        float y = (float) (height / 2 + radius * (Math.sin(angle)));
        return new Point(x, y);
    }

    private final class Point {
        private float x;
        private float y;

        private Point(float x, float y) {
            this.x = x;
            this.y = y;
        }
    }

    private void addUpdateListener(ValueAnimator animator, ValueAnimator.AnimatorUpdateListener updateListener) {
        mUpdateListeners.put(animator, updateListener);
    }

    private void onCreateAnimators() {
        ArrayList<ValueAnimator> animators = new ArrayList<>();
        int[] delays = {0, 120, 240, 360, 480, 600, 720, 780, 840};
        for (int i = 0; i < 8; i++) {
            final int index = i;
            ValueAnimator scaleAnim = ValueAnimator.ofFloat(1, 0.4f, 1);
            scaleAnim.setDuration(1000);
            scaleAnim.setRepeatCount(-1);
            scaleAnim.setStartDelay(delays[i]);
            addUpdateListener(scaleAnim, new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    scaleFloats[index] = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });

            ValueAnimator alphaAnim = ValueAnimator.ofInt(255, 77, 255);
            alphaAnim.setDuration(1000);
            alphaAnim.setRepeatCount(-1);
            alphaAnim.setStartDelay(delays[i]);
            addUpdateListener(alphaAnim, new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    alphas[index] = (int) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            animators.add(scaleAnim);
            animators.add(alphaAnim);
        }
        mAnimators = animators;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2011年10月20日為什么糊涂 回家的路上,曉曉打了個(gè)噴嚏,媽媽有點(diǎn)緊張:“怎么了?”卻說(shuō)成了:“怎么辦?”說(shuō)完...
    羊羊羊羊汪閱讀 11,266評(píng)論 2 14
  • 文/楊柳君 你的企業(yè)互聯(lián)網(wǎng)+了嗎? 各類信息渠道經(jīng)常見到這樣的提問(wèn),企業(yè)界朋友們交流時(shí),也常有此類探討。 什么是互...
    楊柳君閱讀 166評(píng)論 0 1
  • 一縷暖陽(yáng),將我擁入懷中。 我被擠于狹隘的縫隙中,扭扭已經(jīng)僵硬的身體,頭頂曾引以為傲的白色小茸毛已經(jīng)破爛不堪。絲縷陽(yáng)...
    蔣小敬閱讀 789評(píng)論 0 3
  • 《給K的詩(shī)》之 《送可愛的樹兒出嫁》 有沒(méi)有一棵可愛的樹 四季都開著淡綠的花 新芽在枝頭吟唱 年長(zhǎng)的樹葉為她擋著陽(yáng)...
    朝歸閱讀 163評(píng)論 0 1
  • 你總在降低自己的標(biāo)準(zhǔn),總是退而求其次,以為這樣會(huì)活得更好,但其實(shí)低配的生活,反而會(huì)讓你失去更多。 2018新年伊始...
    纏者曹閱讀 440評(píng)論 0 0

友情鏈接更多精彩內(nèi)容