Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

載入網絡圖片時自訂圓形進度條

請求網絡圖片時,若能在圖片尚未完全顯示之前先給一個簡潔的圓形進度條,體驗會人性化許多。下面這個 LoadingCircleView 就是這樣一個自訂 View。

/**
 * 圓形加載進度條
 * @author way
 */
public class LoadingCircleView extends View {

    private final Paint paint;
    private final Context context;
    private Resources res;
    private int max = 100;
    private int progress = 0;
    private int ringWidth;      // 圓環寬度
    private int ringColor;      // 圓環的顏色
    private int progressColor;  // 進度條顏色
    private int textColor;      // 字體顏色
    private int textSize;       // 字的大小
    private String textProgress;

    public LoadingCircleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        this.paint = new Paint();
        this.res = context.getResources();
        this.paint.setAntiAlias(true);              // 消除鋸齒
        this.ringWidth = dip2px(context, 3);        // 設置圓環寬度
        this.ringColor = Color.BLACK;               // 黑色進度條背景
        this.progressColor = Color.WHITE;           // 白色進度條
        this.textColor = Color.BLACK;               // 黑色數字顯示進度
        this.textSize = 15;                         // 默認字體大小
    }

    public LoadingCircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    public synchronized void setMax(int max) {
        if (max < 0) max = 0;
        if (progress > max) progress = max;
        this.max = max;
    }

    public synchronized int getMax() {
        return max;
    }

    /** 設置加載進度,取值範圍 0 ~ max */
    public synchronized void setProgress(int progress) {
        if (progress >= 0 && progress <= max) {
            this.progress = progress;
            invalidate();
        }
    }

    public synchronized int getProgress() {
        return progress;
    }

    public void setRingColor(int ringColor) {
        this.ringColor = res.getColor(ringColor);
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = res.getColor(progressColor);
    }

    public void setTextColor(int textColor) {
        this.textColor = res.getColor(textColor);
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setRingWidthDip(int ringWidth) {
        this.ringWidth = dip2px(context, ringWidth);
    }

    /** 通過不斷畫弧的方式更新界面,實現進度增加 */
    @Override
    protected void onDraw(Canvas canvas) {
        int center = getWidth() / 2;
        int radios = center - ringWidth / 2;

        // 繪制圓環
        this.paint.setStyle(Paint.Style.STROKE);   // 空心圓
        this.paint.setColor(ringColor);
        this.paint.setStrokeWidth(ringWidth);
        canvas.drawCircle(center, center, radios, this.paint);

        RectF oval = new RectF(center - radios, center - radios,
                               center + radios, center + radios);
        this.paint.setColor(progressColor);
        canvas.drawArc(oval, 90, 360 * progress / max, false, paint);

        this.paint.setStyle(Paint.Style.FILL);
        this.paint.setColor(textColor);
        this.paint.setStrokeWidth(0);
        this.paint.setTextSize(textSize);
        this.paint.setTypeface(Typeface.DEFAULT_BOLD);
        textProgress = (int) (1000 * (progress / (10.0 * max))) + "%";
        float textWidth = paint.measureText(textProgress);
        canvas.drawText(textProgress, center - textWidth / 2,
                        center + textSize / 2, paint);

        super.onDraw(canvas);
    }

    /** 根據手機的分辨率從 dp 轉為 px */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

用法與一般的 ProgressBar 幾乎相同,在 layout 中放入這個 view,取得實例後呼叫 setProgress 即可,需要更動配色或環寬也有對應的 setter。核心畫法在 onDraw:先用 STROKE 模式畫出灰色底環,再用 drawArc 依進度比例畫出白色弧段,最後置中繪製百分比文字。

原始碼下載:https://linux.linuxidc.com/,帳號與密碼皆為 www.linuxidc.com,目錄為 /2013年資料/5月/14日/Android之加載圖片時自定義進度條。

Copyright © Linux教程網 All Rights Reserved