圓角矩形精度條

來源:互聯網
上載者:User

標籤:ogre   nullable   else   init   simple   內容   false   EDA   null   

public class DownProgressBar extends View {  /**     * 注意:     * 1,整個精度條的寬度 MaxProgress;     * 2,當前精度條的寬度 currentProgress;     * 4,背景顏色     * 5,進度顏色     * 6,文字,文字顏色,大小     *     * @param context     */private int mMaxProgress;    private int mProgress;    private int mBackgroundColor;    private int mProgressColor;    private String mProgressText;    private int mProgressTextColor;    private int mProgressTextSize;    private Paint mBackgroundPaint;    private Paint mProgressPaint;    private Paint mProgressTextPaint;    private static final int DEFAULT_MAX_PROGRESS = 100private static final int DEFAULT_PROGRESS = 0;    private static final int DEFAULT_BACKGROUND_COLOR = ParseStringColor("#000000");    private static final int DEFAULT_PROGRESS_COLOR = ParseStringColor("#6FCADF");    private static final int DEFAULT_PROGRESS_TEXT_COLOR = ParseStringColor("#000000");    private static final int DEFAULT_PROGRESS_TEXT_SIZE = 10;    private static final float DEFAULT_STORK_WIDTH = 5f;    private static final String TAG = DownProgressBar.class.getSimpleName(); public DownProgressBar(Context context) {        this(context, null, 0);    }    public DownProgressBar(Context context, @Nullable AttributeSet attrs) {        this(context, attrs, 0);    }public DownProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DownProgressBar);        mMaxProgress = a.getInt(R.styleable.DownProgressBar_down_progress_bar_max, DEFAULT_MAX_PROGRESS);        mProgress = a.getInt(R.styleable.DownProgressBar_down_progress_bar_cur, DEFAULT_PROGRESS);        mBackgroundColor = a.getColor(R.styleable.DownProgressBar_down_progress_bar_background_color, DEFAULT_BACKGROUND_COLOR);        mProgressColor = a.getColor(R.styleable.DownProgressBar_down_progress_bar_progress_color, DEFAULT_PROGRESS_COLOR);        mProgressText = a.getString(R.styleable.DownProgressBar_down_progress_bar_text);        mProgressTextColor = a.getColor(R.styleable.DownProgressBar_down_progress_bar_text_color, DEFAULT_PROGRESS_TEXT_COLOR);        mProgressTextSize = a.getDimensionPixelSize(R.styleable.DownProgressBar_down_progress_bar_text_size, DEFAULT_PROGRESS_TEXT_SIZE);        a.recycle();        initView();    } private void initView() {        mBackgroundPaint = new Paint();        mBackgroundPaint.setStrokeWidth(DEFAULT_STORK_WIDTH);        mBackgroundPaint.setColor(mBackgroundColor);        mBackgroundPaint.setStyle(Paint.Style.FILL);        mBackgroundPaint.setAntiAlias(true);        mProgressPaint = new Paint();        mProgressPaint.setStrokeWidth(DEFAULT_STORK_WIDTH);        mProgressPaint.setAntiAlias(true);        mProgressPaint.setStyle(Paint.Style.FILL);        mProgressPaint.setColor(mProgressColor);        mProgressTextPaint = new Paint();        mProgressTextPaint.setStrokeWidth(DEFAULT_STORK_WIDTH);        mProgressTextPaint.setAntiAlias(true);        mProgressTextPaint.setStyle(Paint.Style.FILL);        mProgressTextPaint.setColor(mProgressTextColor);        mProgressTextPaint.setTextSize(mProgressTextSize);        mProgressTextPaint.setTextAlign(Paint.Align.CENTER);    }  @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int cWidth = canvas.getWidth();        int cHeight = canvas.getHeight();        //畫背景圖形,圓角的        //mBackgroundPaint        RectF backGroundRectf = new RectF(0, 0, cWidth, cHeight);        float Ry = cHeight / 2;        float Rx = cHeight / 2;        canvas.drawRoundRect(backGroundRectf, Rx, Ry, mBackgroundPaint);        int progress = (int) ((mProgress * 1.0 / mMaxProgress) * cWidth);        LogUtils.LOGE(TAG, "downLoadProgress   >>>>>>>>" + progress);        //剛開始的時候         if (progress > 0) {            if (progress < cWidth - (cHeight / 2)) {                RectF rectF = new RectF(0, 0, cHeight, cHeight);                if (180 - (progress * 5) > 90) {                    canvas.drawArc(rectF, 180 - (progress * 5), 2 * (progress * 5), false, mProgressPaint);                } else {                    canvas.drawArc(rectF, 90, 180, true, mProgressPaint);                    RectF progressRectf = new RectF(cHeight / 2, 0, progress, cHeight);                    canvas.drawRect(progressRectf, mProgressPaint);                }           } else if (progress >= cWidth - (cHeight / 2)) {                RectF rectF = new RectF(0, 0, cHeight, cHeight);                canvas.drawArc(rectF, 90, 180, true, mProgressPaint);                RectF progressRectf = new RectF(cHeight / 2, 0, cWidth - (cHeight / 2), cHeight);                canvas.drawRect(progressRectf, mProgressPaint);                if (progress < cWidth) {                    int rightPro = cWidth - progress;                    RectF rectFr = new RectF(cWidth - cHeight, 0, cWidth, cHeight);                    Log.d("gxl", "rightPro   >>>>" + rightPro);                    canvas.drawArc(rectFr, 270, 180, false, mProgressPaint);                    canvas.drawArc(rectFr, 360 - (rightPro * 5), 2 * (rightPro * 5), false, mBackgroundPaint);                 } else {                    RectF rectFr = new RectF(cWidth - cHeight, 0, cWidth, cHeight);                    canvas.drawArc(rectFr, 270, 180, false, mProgressPaint);                }            }        }        //文字        canvas.drawText(mProgressText, cWidth / 2, (cHeight + mProgressTextSize - DEFAULT_STORK_WIDTH) / 2, mProgressTextPaint);    }  private static int ParseStringColor(String colorStr) {        return Color.parseColor(colorStr);    } /**     * 設定最大進度     *     * @param mMaxProgress     */    public void setmMaxProgress(int mMaxProgress) {        this.mMaxProgress = mMaxProgress;    } /**     * 設定當前的進度     *     * @param mProgress     */    public void setmProgress(int mProgress) {        this.mProgress = mProgress;        invalidate();    } /**     * 設定當前文本     *     * @param mProgressText     */    public void setmProgressText(String mProgressText) {        this.mProgressText = mProgressText;        invalidate();    } public String getmProgressText() {        return mProgressText;    }public void setmProgressTextColor(int mProgressTextColor) {        this.mProgressTextColor = mProgressTextColor;        mProgressTextPaint.setColor(mProgressTextColor);        invalidate();    } public void setmBackgroundColor(int mBackgroundColor) {        this.mBackgroundColor = mBackgroundColor;        mBackgroundPaint.setColor(mBackgroundColor);        invalidate();    }}



attrs檔案內容

<!-- /**
* 注意:
* 1,整個精度條的寬度 MaxProgress;
* 2,當前精度條的寬度 currentProgress;
* 4,背景顏色
* 5,進度顏色
* 6,文字,文字顏色,大小
*
* @param context
*/-->

<declare-styleable name="DownProgressBar">
<attr name="down_progress_bar_max" format="integer"></attr>
<attr name="down_progress_bar_cur" format="integer"></attr>
<attr name="down_progress_bar_background_color" format="color"></attr>
<attr name="down_progress_bar_progress_color" format="color"></attr>

<attr name="down_progress_bar_text" format="string"></attr>

<attr name="down_progress_bar_text_color" format="color"></attr>
<attr name="down_progress_bar_text_size" format="dimension"></attr>
</declare-styleable>

 

 

圓角矩形精度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.