豎直進度條的實現方法

VerticalProgressBar.java:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class VerticalProgressBar extends View {
    private Paint paint;    // 畫筆
    private int progress;   // 進度值
    private int width;      // 寬度值
    private int height;     // 高度值

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

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

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

    private void init() {
        paint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth() - 1;// 寬度值
        height = getMeasuredHeight() - 1;// 高度值
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.rgb(10, 73, 160));// 設置畫筆顏色, blue

        canvas.drawLine(0, 0, width, 0, paint);// 畫頂邊
        canvas.drawLine(0, 0, 0, height, paint);// 畫左邊
        canvas.drawLine(width, 0, width, height, paint);// 畫右邊
        canvas.drawLine(0, height, width, height, paint);// 畫底邊

        float count_height = height;
        float valid_height = progress / 100f * height;
        float one_setp_height = height / 50;
        float interval_height = 7;
        float drawed_height = 0;

        Log.d("wujiang", "onDraw: valid_height = " + valid_height);
        Log.d("wujiang", "onDraw: one_setp_height = " + one_setp_height);

        if (valid_height > 0) {
            while (drawed_height <= valid_height) {
                paint.setColor(Color.rgb(10, 73, 160));// 設置畫筆顏色, blue

                canvas.drawRect(1, height - (drawed_height + one_setp_height), width, height - drawed_height, paint);// 畫矩形
                drawed_height += one_setp_height;

                if (drawed_height + one_setp_height >= valid_height) {
                    break;
                } else {
                    paint.setColor(Color.TRANSPARENT); // 設置畫筆顏色,設置間隔的顏色
                    canvas.drawRect(1, height - (drawed_height + interval_height), width, height - drawed_height, paint);// 畫矩形
                    drawed_height += interval_height;
                }
            }
        } else {
            canvas.drawRect(0, height - progress / 100f * height, width, height, paint);// 畫矩形
        }

        paint.setColor(Color.GREEN);
        paint.setTextSize(width / 3);// 設置文字大小
        canvas.drawText(String.valueOf(progress), (width - getTextWidth(String.valueOf(progress))) / 2, height / 2, paint);// 畫文字
        super.onDraw(canvas);
    }

    /**
     * 拿到文字寬度
     * @param str 傳進來的字符串
     * return 寬度
     */
    private int getTextWidth(String str) {
        // 計算文字所在矩形,可以得到寬高
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        return rect.width();
    }

    /** 設置progressbar進度 */
    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public int getProgress() {
        return this.progress;
    }
}

xml裏的定義:

<com.amlogic.dvb_view.VerticalProgressBar
    android:id="@+id/progress_quality"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/px_20.0" />

activity裏的使用:

VerticalProgressBar mProgressQuality = findViewById(R.id.progress_quality);
mProgressQuality.setProgress(68);

                                                                               THE            END

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