溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

一個(gè)很酷的加載loading效果

發(fā)布時(shí)間:2020-08-10 12:11:05 來源:網(wǎng)絡(luò) 閱讀:491 作者:楊光成 欄目:移動(dòng)開發(fā)

一個(gè)很酷的加載loading效果,自定義LeafLoadingView實(shí)現(xiàn),LeafLoadingView繼承view,
本例子主要由以下幾點(diǎn)構(gòu)成
(1):RotateAnimation實(shí)現(xiàn)葉子旋轉(zhuǎn)
(2):葉子飄動(dòng)
(3):當(dāng)前進(jìn)度繪制當(dāng)前進(jìn)度條
大體實(shí)現(xiàn):
 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 繪制進(jìn)度條和葉子
        // 之所以把葉子放在進(jìn)度條里繪制,主要是層級(jí)原因
        drawProgressAndLeafs(canvas);
        // drawLeafs(canvas);

        canvas.drawBitmap(mOuterBitmap, mOuterSrcRect, mOuterDestRect, mBitmapPaint);

        postInvalidate();
    }

    private void drawProgressAndLeafs(Canvas canvas) {

        if (mProgress >= TOTAL_PROGRESS) {
            mProgress = 0;
        }
        // mProgressWidth為進(jìn)度條的寬度,根據(jù)當(dāng)前進(jìn)度算出進(jìn)度條的位置
        mCurrentProgressPosition = mProgressWidth * mProgress / TOTAL_PROGRESS;
        // 即當(dāng)前位置在圖中所示1范圍內(nèi)
        if (mCurrentProgressPosition < mArcRadius) {
            Log.i(TAG, "mProgress = " + mProgress + "---mCurrentProgressPosition = "
                    + mCurrentProgressPosition
                    + "--mArcProgressWidth" + mArcRadius);
            // 1.繪制白色ARC,繪制orange ARC
            // 2.繪制白色矩形

            // 1.繪制白色ARC
            canvas.drawArc(mArcRectF, 90, 180, false, mWhitePaint);

            // 2.繪制白色矩形
            mWhiteRectF.left = mArcRightLocation;
            canvas.drawRect(mWhiteRectF, mWhitePaint);

            // 繪制葉子
            drawLeafs(canvas);

            // 3.繪制棕色 ARC
            // 單邊角度
            int angle = (int) Math.toDegrees(Math.acos((mArcRadius - mCurrentProgressPosition)
                    / (float) mArcRadius));
            // 起始的位置
            int startAngle = 180 - angle;
            // 掃過的角度
            int sweepAngle = 2 * angle;
            Log.i(TAG, "startAngle = " + startAngle);
            canvas.drawArc(mArcRectF, startAngle, sweepAngle, false, mOrangePaint);
        } else {
            Log.i(TAG, "mProgress = " + mProgress + "---transfer-----mCurrentProgressPosition = "
                    + mCurrentProgressPosition
                    + "--mArcProgressWidth" + mArcRadius);
            // 1.繪制white RECT
            // 2.繪制Orange ARC
            // 3.繪制orange RECT
            // 這個(gè)層級(jí)進(jìn)行繪制能讓葉子感覺是融入棕色進(jìn)度條中

            // 1.繪制white RECT
            mWhiteRectF.left = mCurrentProgressPosition;
            canvas.drawRect(mWhiteRectF, mWhitePaint);
            // 繪制葉子
            drawLeafs(canvas);
            // 2.繪制Orange ARC
            canvas.drawArc(mArcRectF, 90, 180, false, mOrangePaint);
            // 3.繪制orange RECT
            mOrangeRectF.left = mArcRightLocation;
            mOrangeRectF.right = mCurrentProgressPosition;
            canvas.drawRect(mOrangeRectF, mOrangePaint);

        }

    }

    /**
     * 繪制葉子
     *
     * @param canvas
     */
    private void drawLeafs(Canvas canvas) {
        mLeafRotateTime = mLeafRotateTime <= 0 ? LEAF_ROTATE_TIME : mLeafRotateTime;
        long currentTime = System.currentTimeMillis();
        for (int i = 0; i < mLeafInfos.size(); i++) {
            Leaf leaf = mLeafInfos.get(i);
            if (currentTime > leaf.startTime && leaf.startTime != 0) {
                // 繪制葉子--根據(jù)葉子的類型和當(dāng)前時(shí)間得出葉子的(x,y)
                getLeafLocation(leaf, currentTime);
                // 根據(jù)時(shí)間計(jì)算旋轉(zhuǎn)角度
                canvas.save();
                // 通過Matrix控制葉子旋轉(zhuǎn)
                Matrix matrix = new Matrix();
                float transX = mLeftMargin + leaf.x;
                float transY = mLeftMargin + leaf.y;
                Log.i(TAG, "left.x = " + leaf.x + "--leaf.y=" + leaf.y);
                matrix.postTranslate(transX, transY);
                // 通過時(shí)間關(guān)聯(lián)旋轉(zhuǎn)角度,則可以直接通過修改LEAF_ROTATE_TIME調(diào)節(jié)葉子旋轉(zhuǎn)快慢
                float rotateFraction = ((currentTime - leaf.startTime) % mLeafRotateTime)
                        / (float) mLeafRotateTime;
                int angle = (int) (rotateFraction * 360);
                // 根據(jù)葉子旋轉(zhuǎn)方向確定葉子旋轉(zhuǎn)角度
                int rotate = leaf.rotateDirection == 0 ? angle + leaf.rotateAngle : -angle
                        + leaf.rotateAngle;
                matrix.postRotate(rotate, transX
                        + mLeafWidth / 2, transY + mLeafHeight / 2);
                canvas.drawBitmap(mLeafBitmap, matrix, mBitmapPaint);
                canvas.restore();
            } else {
                continue;
            }
        }
    }

 

運(yùn)行效果:

  • 一個(gè)很酷的加載loading效果

一個(gè)很酷的加載loading效果


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI