Android 自定義View - ProgressViewDemo(帶文本)

效果:

image

ProgressView

public class ProgressView extends View {

    private Paint mBackGroundPaint;//背景色畫(huà)筆
    private Paint mArcPaint;//進(jìn)度畫(huà)筆
    private Paint mTextPaint;//中間文本的畫(huà)筆

    private int mProgress = 0;

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

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

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

    /**
     * 初始化
     */
    public void initPaint() {
        mBackGroundPaint = new Paint();
        mBackGroundPaint.setColor(Color.BLACK);
        mBackGroundPaint.setAntiAlias(true);
        mBackGroundPaint.setStrokeWidth(5);
        mBackGroundPaint.setStyle(Paint.Style.STROKE);

        mArcPaint = new Paint();
        mArcPaint.setColor(Color.RED);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStrokeWidth(10);
        mArcPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.RED);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(30);
        mTextPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //繪制底部圓圈
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, 150, mBackGroundPaint);
        //繪制進(jìn)度的圓弧
        canvas.drawArc(new RectF(getMeasuredWidth() / 2 - 150, getMeasuredHeight() / 2 - 150,
                        getMeasuredWidth() / 2 + 150, getMeasuredHeight() / 2 + 150), 0,
                360 * mProgress / 100, false, mArcPaint);
        //繪制中間進(jìn)度文本
        String text = mProgress + " %";
        //使文本居中擺放
        Rect txtRect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), txtRect);
        canvas.drawText(text, getMeasuredWidth() / 2 - txtRect.width() / 2, getMeasuredHeight() / 2 + txtRect.height() / 2, mTextPaint);
    }

    /**
     * 開(kāi)始進(jìn)度
     */
    public void startProgress() {
        mProgress = 0;
        mHandler.sendEmptyMessageDelayed(0, 10);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (mProgress < 100) {
                    mProgress++;
                    invalidate();
                    sendEmptyMessageDelayed(0, 10);
                }
            }
        }
    };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.progressviewdemo.MainActivity">

    <com.example.progressviewdemo.ProgressView
        android:id="@+id/progressView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        progressView.setOnClickListener({ progressView.startProgress() })
    }
}

傳送門(mén):
https://github.com/Urwateryi/ProgressViewDemo.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子缸兔,更是在濱河造成了極大的恐慌恨溜,老刑警劉巖玉掸,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摆寄,死亡現(xiàn)場(chǎng)離奇詭異垃环,居然都是意外死亡颂碘,警方通過(guò)查閱死者的電腦和手機(jī)异赫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)头岔,“玉大人塔拳,你說(shuō)我怎么就攤上這事∠靠ⅲ” “怎么了靠抑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)适掰。 經(jīng)常有香客問(wèn)我颂碧,道長(zhǎng),這世上最難降的妖魔是什么类浪? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任载城,我火速辦了婚禮,結(jié)果婚禮上戚宦,老公的妹妹穿的比我還像新娘个曙。我一直安慰自己,他們只是感情好受楼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布垦搬。 她就那樣靜靜地躺著,像睡著了一般艳汽。 火紅的嫁衣襯著肌膚如雪猴贰。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,604評(píng)論 1 305
  • 那天河狐,我揣著相機(jī)與錄音米绕,去河邊找鬼瑟捣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛栅干,可吹牛的內(nèi)容都是我干的迈套。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼碱鳞,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼桑李!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起窿给,我...
    開(kāi)封第一講書(shū)人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤贵白,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后崩泡,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體禁荒,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年角撞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了呛伴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡靴寂,死狀恐怖磷蜀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情百炬,我是刑警寧澤褐隆,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站剖踊,受9級(jí)特大地震影響庶弃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜德澈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一歇攻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧梆造,春花似錦缴守、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至忽肛,卻和暖如春村砂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背屹逛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工础废, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留汛骂,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓评腺,卻偏偏與公主長(zhǎng)得像帘瞭,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蒿讥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355