溫馨提示×

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

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

Android實(shí)現(xiàn)下載進(jìn)度條效果的方法

發(fā)布時(shí)間:2021-06-16 16:44:07 來源:億速云 閱讀:419 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Android實(shí)現(xiàn)下載進(jìn)度條效果的方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Android實(shí)現(xiàn)下載進(jìn)度條效果的方法”吧!

目錄
  • 最終效果和對(duì)比vivo商店效果

  • 分析1 - 計(jì)算進(jìn)度

  • 分析2 - 繪制圓角矩形

  • 解決方案

  • 分析3 - 繪制文字和交匯

  • 手勢(shì)拓展

最終效果和對(duì)比vivo商店效果

vivo應(yīng)用商店下載效果:

Android實(shí)現(xiàn)下載進(jìn)度條效果的方法

最終實(shí)現(xiàn)效果:

Android實(shí)現(xiàn)下載進(jìn)度條效果的方法

分析1 - 計(jì)算進(jìn)度

進(jìn)度計(jì)算就比較簡(jiǎn)單了,我們通過復(fù)寫onSizeChanged()方法,獲取到控件的寬后,先計(jì)算當(dāng)前進(jìn)度百分比,再將百分比乘以寬度,就可以得到應(yīng)該繪制的寬度了。

繪制圓角矩形需要傳一個(gè)Rect,Rect的構(gòu)造方法需要傳4個(gè)位置,分別是left、top、right、bottom,我們主要是計(jì)算不斷變化的right值。在drawProgress()方法中,right值為最大的寬度 * 進(jìn)度百分比值。

/**
 * 控件寬
 */
private int mViewWidth;
/**
 * 控件高
 */
private int mViewHeight;
/**
 * 圓角弧度
 */
private float mRadius;
/**
 * 當(dāng)前進(jìn)度
 */
private int mProgress;
/**
 * 最大進(jìn)度值
 */
private int mMaxProgress;

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //畫進(jìn)度條
    drawProgress(canvas);
}

/**
 * 畫進(jìn)度
 */
private void drawProgress(Canvas canvas) {
    RectF rect = new RectF(getFrameLeft(), getFrameTop(), getFrameRight() * getProgressRatio(), getFrameBottom());
    canvas.drawRect(rect, mProgressPaint);
}

/**
 * 獲取當(dāng)前進(jìn)度值比值
 */
private float getProgressRatio() {
    return (mProgress / (mMaxProgress * 1.0f));
}

//------------ getFrameXxx()方法都是處理padding ------------

private float getFrameLeft() {
    return getPaddingStart();
}

private float getFrameRight() {
    return mViewWidth - getPaddingEnd();
}

private float getFrameTop() {
    return getPaddingTop();
}

private float getFrameBottom() {
    return mViewHeight - getPaddingBottom();
}

//------------ getFrameXxx()方法都是處理padding ------------

分析2 - 繪制圓角矩形

背景和進(jìn)度條,都是圓角矩形,我們可以使用canvas.drawRoundRect()的API來繪制。但是使用canvas.drawRoundRect()會(huì)有個(gè)問題,當(dāng)進(jìn)度比較小的時(shí)候,例如1%,計(jì)算出來的矩形長(zhǎng)度比較小,會(huì)導(dǎo)致圓角不是滿的,缺陷效果見動(dòng)圖(注意看剛從左側(cè)出來時(shí)圓角的大?。?/p>

Android實(shí)現(xiàn)下載進(jìn)度條效果的方法

public class DownloadProgressView extends View {
    ...省略其他代碼

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = w;
        mViewHeight = h;
        //計(jì)算出圓角半徑
        mRadius = Math.min(mViewWidth, mViewHeight) / 2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //畫背景
        drawBg(canvas);
        //畫進(jìn)度條
        drawProgress(canvas);
    }

    /**
     * 畫背景
     */
    private void drawBg(Canvas canvas) {
        canvas.drawRoundRect(new RectF(getFrameLeft(), getPaddingTop(), getFrameRight(), getFrameBottom()), mRadius, mRadius, mBgPaint);
    }

    /**
     * 畫進(jìn)度
     */
    private void drawProgress(Canvas canvas) {
        RectF rect = new RectF(getFrameLeft(), getFrameTop(), getFrameRight() * getProgressRatio(), getFrameBottom());
        canvas.drawRoundRect(rect, mRadius, mRadius, mProgressPaint);
    }

    //------------ getFrameXxx()方法都是處理padding ------------

    private float getFrameLeft() {
        return getPaddingStart();
    }

    private float getFrameRight() {
        return mViewWidth - getPaddingEnd();
    }

    private float getFrameTop() {
        return getPaddingTop();
    }

    private float getFrameBottom() {
        return mViewHeight - getPaddingBottom();
    }

    //------------ getFrameXxx()方法都是處理padding ------------
}

解決方案

我的方案是這樣的,不使用drawRoundRect()方法來繪制圓角矩形,而是先使用canvas.clipPath()方法,添加一個(gè)有圓角矩形的Path,來裁切畫布,再drawRect()方法來繪制一個(gè)長(zhǎng)矩形,就保持了圓角。

但clipPath()也有一個(gè)缺點(diǎn),就是裁切出來的圓角會(huì)有鋸齒,Paint設(shè)置了setAntiAlias(true)也沒有用,大家有好的方法,互相學(xué)習(xí)一下!

public class DownloadProgressView extends View {
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //裁剪圓角
        clipRound(canvas);
        //畫背景
        drawBg(canvas);
        //畫進(jìn)度條
        drawProgress(canvas);
    }

    /**
     * 裁剪圓角
     */
    private void clipRound(Canvas canvas) {
        Path path = new Path();
        RectF roundRect = new RectF(getFrameLeft(), getFrameTop(), getFrameRight(), getFrameBottom());
        path.addRoundRect(roundRect, mRadius, mRadius, Path.Direction.CW);
        canvas.clipPath(path);
    }

    /**
     * 畫背景
     */
    private void drawBg(Canvas canvas) {
        canvas.drawRect(new RectF(getFrameLeft(), getPaddingTop(), getFrameRight(), getFrameBottom()), mBgPaint);
    }

    /**
     * 畫進(jìn)度
     */
    private void drawProgress(Canvas canvas) {
        RectF rect = new RectF(getFrameLeft(), getFrameTop(), getFrameRight() * getProgressRatio(), getFrameBottom());
        canvas.drawRect(rect, mProgressPaint);
    }
}

分析3 - 繪制文字和交匯

要讓文字和進(jìn)度交匯時(shí),讓藍(lán)色變?yōu)榘咨枰褂玫絇orterDuffXfermode。PorterDuffXfermode可以將2個(gè)圖像進(jìn)行合成,或者簡(jiǎn)單說為相交模式,而合成方式有16種,見下圖。(Src藍(lán)色方塊為上層圖層,Dst黃色圓形為下層圖層。)

Android實(shí)現(xiàn)下載進(jìn)度條效果的方法

查閱文檔,我們發(fā)現(xiàn)SrcATop模式比較符合我們的需求,在SrcATop模式下,Src圖層不覆蓋Dst圖層的像素會(huì)被拋棄,只保留Src圖層覆蓋Dst圖層的圖層像素。

我們的思路是這樣的:畫4個(gè)圖層,最底部的灰色背景圖層,再上一層藍(lán)色進(jìn)度圖層,接著是文字圖層,最上層是一層白色的進(jìn)度圖層,重點(diǎn)在畫文字和白色進(jìn)度圖層是添加了PorterDuffXfermode(SrcATop模式)。

白色進(jìn)度和藍(lán)色進(jìn)度的大小一直是同步的,但是因?yàn)镻orterDuffXfermode的原因,白色圖層未覆蓋到文字時(shí),一直都是被拋棄圖層像素,相當(dāng)于是透明的,只有當(dāng)和文字相交時(shí),才會(huì)繪制出白色圖層,就顯示出了一半白色文字,一半藍(lán)色文字的效果,而其他部分都沒有和文字相交都被拋棄,都為透明。

繪制文字、白色進(jìn)度圖層代碼,見drawText()方法。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //裁剪圓角
    clipRound(canvas);
    //畫背景
    drawBg(canvas);
    //畫進(jìn)度條
    drawProgress(canvas);
    //畫文字圖層
    drawText(canvas);
}

/**
 * 畫文字
 */
private void drawText(Canvas canvas) {
    mTextPaint.setColor(mPercentageTextColor);
    //創(chuàng)建文字圖層
    Bitmap textBitmap = Bitmap.createBitmap(mViewWidth, mViewHeight, Bitmap.Config.ARGB_8888);
    Canvas textCanvas = new Canvas(textBitmap);
    String textContent = mProgress + "%";
    //計(jì)算文字Y軸坐標(biāo)
    float textY = mViewHeight / 2.0f - (mTextPaint.getFontMetricsInt().descent / 2.0f + mTextPaint.getFontMetricsInt().ascent / 2.0f);
    textCanvas.drawText(textContent, mViewWidth / 2.0f, textY, mTextPaint);
    //畫最上層的白色圖層,未相交時(shí)不會(huì)顯示出來
    mTextPaint.setColor(mPercentageTextColor2);
    mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    textCanvas.drawRect(new RectF(getFrameLeft(), getFrameTop(), getFrameRight() * getProgressRatio(), getFrameBottom()), mTextPaint);
    //畫結(jié)合后的圖層
    canvas.drawBitmap(textBitmap, getFrameLeft(), getFrameTop(), mTextPaint);
    mTextPaint.setXfermode(null);
    textBitmap.recycle();
}

手勢(shì)拓展

經(jīng)過上面的進(jìn)度、文字、交匯處理,進(jìn)度條算是完整了,接下來拓展一下沒有的東西,例如手勢(shì)拖動(dòng),就像SeekBar一樣,當(dāng)我們觸摸進(jìn)度條時(shí),進(jìn)度會(huì)隨著移動(dòng)而改變進(jìn)度。

我們先重寫dispatchTouchEvent()方法,當(dāng)down事件來到時(shí),讓父控件不攔截,我們進(jìn)行攔截。接著重寫onTouchEvent()方法,計(jì)算Move、Up時(shí),移動(dòng)的距離,計(jì)算出百分比和應(yīng)有的進(jìn)度值,然后不斷調(diào)用setProgress()來更新進(jìn)度。

public class DownloadProgressView extends View {
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        //攔截事件,然后讓父類不進(jìn)行攔截
        if (mControlMode == MODE_TOUCH && action == MotionEvent.ACTION_DOWN) {
            getParent().requestDisallowInterceptTouchEvent(true);
            return true;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //拽托模式下才起效果
        if (mControlMode == MODE_TOUCH) {
            int action = event.getAction();
            //包裹Down事件時(shí)的x坐標(biāo)
            if (action == MotionEvent.ACTION_DOWN) {
                mTouchDownX = event.getX();
                return true;
            } else if (action == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_UP) {
                //Move或Up的時(shí)候,計(jì)算拽托進(jìn)度
                float endX = event.getX();
                //計(jì)算公式:百分比值 = 移動(dòng)距離 / 總長(zhǎng)度
                float distanceX = Math.abs(endX - mTouchDownX);
                float ratio = (distanceX * 1.0f) / (getFrameRight() - getFrameLeft());
                //計(jì)算百分比應(yīng)該有的進(jìn)度:進(jìn)度 = 總進(jìn)度 * 進(jìn)度百分比值
                float progress = mMaxProgress * ratio;
                setProgress((int) progress);
                return true;
            }
            return super.onTouchEvent(event);
        } else {
            return super.onTouchEvent(event);
        }
    }
}

完整代碼

自定義屬性

<resources>
    <declare-styleable name="DownloadProgressView">
        <!-- 進(jìn)度條背景 -->
        <attr name="dpv_bg" format="color" />
        <!-- 已有進(jìn)度條背景 -->
        <attr name="dpv_progress_bg" format="color" />
        <!-- 百分比字體顏色 -->
        <attr name="dpv_percentage_text_color" format="color" />
        <!-- 上層百分比字體顏色 -->
        <attr name="dpv_percentage_text_color2" format="color" />
        <attr name="dpv_percentage_text_size" format="integer|float|dimension|reference" />
        <!-- 當(dāng)前進(jìn)度值 -->
        <attr name="dpv_progress" format="integer" />
        <!-- 最大進(jìn)度值 -->
        <attr name="dpv_max_progress" format="integer" />
        <!-- 控制模式 -->
        <attr name="dpv_control_mode" format="enum">
            <enum name="normal" value="1" />
            <enum name="touch" value="2" />
        </attr>
    </declare-styleable>
</resources>

自定義View

public class DownloadProgressView extends View {
    /**
     * 控制模式-普通
     */
    private static final int MODE_NORMAL = 1;
    /**
     * 控制模式-觸摸
     */
    private static final int MODE_TOUCH = 2;

    /**
     * View默認(rèn)最小寬度
     */
    private int mDefaultMinWidth;
    /**
     * View默認(rèn)最小高度
     */
    private int mDefaultMinHeight;

    /**
     * 控件寬
     */
    private int mViewWidth;
    /**
     * 控件高
     */
    private int mViewHeight;

    /**
     * 圓角弧度
     */
    private float mRadius;
    /**
     * 背景畫筆
     */
    private Paint mBgPaint;
    /**
     * 進(jìn)度畫筆
     */
    private Paint mProgressPaint;
    /**
     * 文字畫筆
     */
    private Paint mTextPaint;
    /**
     * 當(dāng)前進(jìn)度
     */
    private int mProgress;
    /**
     * 背景顏色
     */
    private int mBgColor;
    /**
     * 進(jìn)度背景顏色
     */
    private int mProgressBgColor;
    /**
     * 進(jìn)度百分比文字的顏色
     */
    private int mPercentageTextColor;
    /**
     * 第二層進(jìn)度百分比文字的顏色
     */
    private int mPercentageTextColor2;
    /**
     * 進(jìn)度百分比文字的字體大小
     */
    private float mPercentageTextSize;
    /**
     * 最大進(jìn)度值
     */
    private int mMaxProgress;
    /**
     * 進(jìn)度更新監(jiān)聽
     */
    private OnProgressUpdateListener mOnProgressUpdateListener;
    /**
     * 控制模式
     */
    private int mControlMode;
    /**
     * 按下時(shí)Down事件的x坐標(biāo)
     */
    private float mTouchDownX;

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

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

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

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        initAttr(context, attrs, defStyleAttr);
        //取消硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        //背景畫筆
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setColor(mBgColor);
        //進(jìn)度畫筆
        mProgressPaint = new Paint();
        mProgressPaint.setColor(mProgressBgColor);
        mProgressPaint.setAntiAlias(true);
        //文字畫筆
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mPercentageTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        //計(jì)算默認(rèn)寬、高
        mDefaultMinWidth = dip2px(context, 180f);
        mDefaultMinHeight = dip2px(context, 40f);
    }

    private void initAttr(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DownloadProgressView, defStyleAttr, 0);
        mBgColor = array.getColor(R.styleable.DownloadProgressView_dpv_bg, Color.argb(100, 169, 169, 169));
        mProgressBgColor = array.getColor(R.styleable.DownloadProgressView_dpv_progress_bg, Color.GRAY);
        //進(jìn)度百分比文字的顏色,默認(rèn)和進(jìn)度背景顏色一致
        mPercentageTextColor = array.getColor(R.styleable.DownloadProgressView_dpv_percentage_text_color, mProgressBgColor);
        //第二層,進(jìn)度百分比文字的顏色
        mPercentageTextColor2 = array.getColor(R.styleable.DownloadProgressView_dpv_percentage_text_color2, Color.WHITE);
        //進(jìn)度百分比文字的字體顏色
        mPercentageTextSize = array.getDimension(R.styleable.DownloadProgressView_dpv_percentage_text_size, sp2px(context, 15f));
        //當(dāng)前進(jìn)度值
        mProgress = array.getInt(R.styleable.DownloadProgressView_dpv_progress, 0);
        //最大進(jìn)度值
        mMaxProgress = array.getInteger(R.styleable.DownloadProgressView_dpv_max_progress, 100);
        //控制模式
        mControlMode = array.getInt(R.styleable.DownloadProgressView_dpv_control_mode, MODE_NORMAL);
        array.recycle();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = w;
        mViewHeight = h;
        //計(jì)算出圓角半徑
        mRadius = Math.min(mViewWidth, mViewHeight) / 2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //裁剪圓角
        clipRound(canvas);
        //畫背景
        drawBg(canvas);
        //畫進(jìn)度條
        drawProgress(canvas);
        //畫文字圖層
        drawText(canvas);
    }

    //------------ getFrameXxx()方法都是處理padding ------------

    private float getFrameLeft() {
        return getPaddingStart();
    }

    private float getFrameRight() {
        return mViewWidth - getPaddingEnd();
    }

    private float getFrameTop() {
        return getPaddingTop();
    }

    private float getFrameBottom() {
        return mViewHeight - getPaddingBottom();
    }

    //------------ getFrameXxx()方法都是處理padding ------------

    /**
     * 裁剪圓角
     */
    private void clipRound(Canvas canvas) {
        Path path = new Path();
        RectF roundRect = new RectF(getFrameLeft(), getFrameTop(), getFrameRight(), getFrameBottom());
        path.addRoundRect(roundRect, mRadius, mRadius, Path.Direction.CW);
        canvas.clipPath(path);
    }

    /**
     * 畫背景
     */
    private void drawBg(Canvas canvas) {
        canvas.drawRect(new RectF(getFrameLeft(), getPaddingTop(), getFrameRight(), getFrameBottom()), mBgPaint);
    }

    /**
     * 畫進(jìn)度
     */
    private void drawProgress(Canvas canvas) {
        RectF rect = new RectF(getFrameLeft(), getFrameTop(), getFrameRight() * getProgressRatio(), getFrameBottom());
        canvas.drawRect(rect, mProgressPaint);
    }

    /**
     * 畫文字
     */
    private void drawText(Canvas canvas) {
        mTextPaint.setColor(mPercentageTextColor);
        //創(chuàng)建文字圖層
        Bitmap textBitmap = Bitmap.createBitmap(mViewWidth, mViewHeight, Bitmap.Config.ARGB_8888);
        Canvas textCanvas = new Canvas(textBitmap);
        String textContent = mProgress + "%";
        //計(jì)算文字Y軸坐標(biāo)
        float textY = mViewHeight / 2.0f - (mTextPaint.getFontMetricsInt().descent / 2.0f + mTextPaint.getFontMetricsInt().ascent / 2.0f);
        textCanvas.drawText(textContent, mViewWidth / 2.0f, textY, mTextPaint);
        //畫最上層的白色圖層,未相交時(shí)不會(huì)顯示出來
        mTextPaint.setColor(mPercentageTextColor2);
        mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        textCanvas.drawRect(new RectF(getFrameLeft(), getFrameTop(), getFrameRight() * getProgressRatio(), getFrameBottom()), mTextPaint);
        //畫結(jié)合后的圖層
        canvas.drawBitmap(textBitmap, getFrameLeft(), getFrameTop(), mTextPaint);
        mTextPaint.setXfermode(null);
        textBitmap.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(handleMeasure(widthMeasureSpec, true), handleMeasure(heightMeasureSpec, false));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        //攔截事件,然后讓父類不進(jìn)行攔截
        if (mControlMode == MODE_TOUCH && action == MotionEvent.ACTION_DOWN) {
            getParent().requestDisallowInterceptTouchEvent(true);
            return true;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //拽托模式下才起效果
        if (mControlMode == MODE_TOUCH) {
            int action = event.getAction();
            //包裹Down事件時(shí)的x坐標(biāo)
            if (action == MotionEvent.ACTION_DOWN) {
                mTouchDownX = event.getX();
                return true;
            } else if (action == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_UP) {
                //Move或Up的時(shí)候,計(jì)算拽托進(jìn)度
                float endX = event.getX();
                //計(jì)算公式:百分比值 = 移動(dòng)距離 / 總長(zhǎng)度
                float distanceX = Math.abs(endX - mTouchDownX);
                float ratio = (distanceX * 1.0f) / (getFrameRight() - getFrameLeft());
                //計(jì)算百分比應(yīng)該有的進(jìn)度:進(jìn)度 = 總進(jìn)度 * 進(jìn)度百分比值
                float progress = mMaxProgress * ratio;
                setProgress((int) progress);
                return true;
            }
            return super.onTouchEvent(event);
        } else {
            return super.onTouchEvent(event);
        }
    }

    /**
     * 處理MeasureSpec
     */
    private int handleMeasure(int measureSpec, boolean isWidth) {
        int result;
        if (isWidth) {
            result = mDefaultMinWidth;
        } else {
            result = mDefaultMinHeight;
        }
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            //處理wrap_content的情況
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    /**
     * 設(shè)置進(jìn)度最大值
     */
    public DownloadProgressView setMaxProgress(int maxProgress) {
        mMaxProgress = maxProgress;
        invalidate();
        return this;
    }

    /**
     * 設(shè)置進(jìn)度
     */
    public void setProgress(int progress) {
        if (progress >= 0 && progress <= 100) {
            mProgress = progress;
            invalidate();
            if (mOnProgressUpdateListener != null) {
                mOnProgressUpdateListener.onProgressUpdate(progress);
            }
        }
    }

    /**
     * 獲取當(dāng)前進(jìn)度
     */
    public int getProgress() {
        return mProgress;
    }

    /**
     * 獲取最大進(jìn)度
     */
    public int getMaxProgress() {
        return mMaxProgress;
    }

    public interface OnProgressUpdateListener {
        /**
         * 進(jìn)度更新時(shí)回調(diào)
         *
         * @param progress 當(dāng)前進(jìn)度
         */
        void onProgressUpdate(int progress);
    }

    public void setOnProgressUpdateListener(OnProgressUpdateListener onProgressUpdateListener) {
        mOnProgressUpdateListener = onProgressUpdateListener;
    }

    /**
     * 獲取當(dāng)前進(jìn)度值比值
     */
    private float getProgressRatio() {
        return (mProgress / (mMaxProgress * 1.0f));
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}

具體使用

在布局中添加控件

<com.zh.cavas.sample.widget.DownloadProgressView
        android:id="@+id/download_progress"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        app:dpv_bg="#E6EAFB"
        app:dpv_max_progress="100"
        app:dpv_percentage_text_color2="@color/white"
        app:dpv_percentage_text_size="14sp"
        app:dpv_progress="0"
        app:dpv_progress_bg="#3548FE"
        tools:dpv_progress="50" />

Java代碼

private DownloadProgressView vDownloadProgressView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        vDownloadProgressView = view.findViewById(R.id.download_progress);
        //設(shè)置進(jìn)度監(jiān)聽
        vDownloadProgressView.setOnProgressUpdateListener(new DownloadProgressView.OnProgressUpdateListener() {
            @Override
            public void onProgressUpdate(int progress) {
                vProgressIndicator.setText("當(dāng)前進(jìn)度:" + progress);
            }
        });
        //手動(dòng)設(shè)置當(dāng)前進(jìn)度
        vDownloadProgressView.setProgress(0);
        //設(shè)置最大值
        vDownloadProgressView.setMaxProgress(100);
    }
}

感謝各位的閱讀,以上就是“Android實(shí)現(xiàn)下載進(jìn)度條效果的方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Android實(shí)現(xiàn)下載進(jìn)度條效果的方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向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