計(jì)步用進(jìn)度條效果很棒

效果圖:

https://img1.mukewang.com/5c4281b90001eb9d01380138.jpg

要實(shí)現(xiàn)這樣一個(gè)進(jìn)度條档押,我們要?jiǎng)?chuàng)建一個(gè)SportStepCountView,里面要有計(jì)算:

<pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

final int restore = canvas.save();

final int cx = getMeasuredWidth() / 2;
final int cy = getMeasuredHeight() / 2;
final int radius = getMeasuredWidth() / 2 - strokeWidth / 2;

float drawPercent = percent;
if (drawPercent > 0.97 && drawPercent < 1) {
    drawPercent = 0.97f;
}

// 畫(huà)漸變圓
canvas.save();
canvas.rotate(-90, cx, cy);
int[] colors;
float[] positions;

if (drawPercent < 1 && drawPercent > 0) {
    calculatePercentEndColor(drawPercent);
    customColors[1] = percentEndColor;
    colors = customColors;
    customPositions[1] = drawPercent;
    customPositions[2] = drawPercent;
    positions = customPositions;
} else if (drawPercent == 1) {
    colors = fullColors;
    positions = extremePositions;
} else {
    colors = emptyColors;
    positions = extremePositions;
}

//這個(gè)是灰色的大圓環(huán)
canvas.drawCircle(cx, cy, radius, mBackgroundCirclePaint);

final SweepGradient sweepGradient = new SweepGradient(getMeasuredWidth() / 2, getMeasuredHeight() / 2, colors, positions);
paint.setShader(sweepGradient);
//paint.setShadowLayer(13.5f,0,0.5f,getResources().getColor(R.color.circle_shadow_color));
canvas.drawCircle(cx, cy, radius, paint);

canvas.restore();

//中間的步數(shù)寫在這里
mValueOffset = cy + getBaselineOffsetFromY(mValuePaint)- DensityUtil.dp2px(mContext,20);
//這個(gè)是寫步數(shù)
canvas.drawText(String.valueOf(mFootStep), cx, mValueOffset, mValuePaint);

mHintOffset = cy - radius * mTextOffsetPercentInRadius + getBaselineOffsetFromY(mUnitPaint);
mUnitOffset = cy + radius * mTextOffsetPercentInRadius + getBaselineOffsetFromY(mUnitPaint) -DensityUtil.dp2px(mContext,30);

if (mHint != null) {
    canvas.drawText(mHint.toString(), cx, mHintOffset-10, mUnitPaint);
}

if (mUnit != null) {
    canvas.drawText(mUnit.toString(), cx, mUnitOffset+10, mUnitPaint);
}

if (drawPercent > 0) {
    // 繪制結(jié)束的半圓
    if (drawPercent < 1) {
        canvas.save();
        endPaint.setColor(percentEndColor);
        canvas.rotate((int) Math.floor(360.0f * drawPercent) - 1, cx, cy);
        canvas.drawArc(rectF, -90f, 180f, true, endPaint);
        canvas.restore();
    }

    canvas.save();
    // 繪制開(kāi)始的半圓
    canvas.drawArc(rectF, 90f, 180f, true, startPaint);
    canvas.restore();
}

//這個(gè)是y頂部非常小的白色小圓,像扣子一樣
canvas.drawCircle(cx, cy - radius, mLittleCircleSize, mLittleCirclePaint);
canvas.restoreToCount(restore);

}
</pre>

然后在MainActivity中調(diào)用:

<pre class="brush:java;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">private SportStepCountView sportStepCountView;
private Handler mHandler = new MyHandler();
private int mProgress = 0;
private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
sportStepCountView.setValueDuringRefresh(mProgress,100);

    }
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sportStepCountView = (SportStepCountView)findViewById(R.id.circleProgress);

// sportStepCountView.setValue(50,100);

    new Thread(new Runnable() {
        @Override
        public void run() {
            while(mProgress<100) {
                mProgress++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessage(0);
            }
        }
    }).start();
}

</pre>

xml文件為:

<pre class="brush:xml;toolbar:false" style="margin: 0.5em 0px; padding: 0.4em 0.6em; line-height: 1.5; border-radius: 8px; background: rgb(248, 248, 248); color: rgb(28, 31, 33); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="231dp"
android:layout_marginTop="20dp">

<nickgao.com.viewpagerswitchexample.view.SportStepCountView
    android:id="@+id/circleProgress"
    android:layout_width="210dp"
    android:layout_height="210dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

<Button
    android:id="@+id/checkHistoryStepCount"
    android:layout_marginTop="165dp"
    android:layout_width="76dp"
    android:layout_height="26dp"
    android:layout_centerHorizontal="true"
    android:textColor="@color/pregnancy_color_888888"
    android:textSize="14sp"
    android:text="查看歷史"
    android:background="@drawable/check_step_history_rectangle"
    />

<Button
    android:id="@+id/backToToday"
    android:layout_width="71dp"
    android:layout_height="26dp"
    android:layout_alignParentRight="true"
    android:background="@drawable/pedometer_back_to_today_background"
    android:text="回到今天"
    android:textColor="@color/pregnancy_color_888888"
    android:visibility="gone" />

</RelativeLayout>
</pre>

?gitHub地址為:https://github.com/nickgao1986/ProgressWithAnimation

如果喜歡功偿,可以star一下淘邻,謝謝

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末篮昧,一起剝皮案震驚了整個(gè)濱河市榄笙,隨后出現(xiàn)的幾起案子柳刮,更是在濱河造成了極大的恐慌搭盾,老刑警劉巖咳秉,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異鸯隅,居然都是意外死亡澜建,警方通過(guò)查閱死者的電腦和手機(jī)向挖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)炕舵,“玉大人何之,你說(shuō)我怎么就攤上這事⊙式睿” “怎么了溶推?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)奸攻。 經(jīng)常有香客問(wèn)我蒜危,道長(zhǎng),這世上最難降的妖魔是什么睹耐? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任辐赞,我火速辦了婚禮,結(jié)果婚禮上硝训,老公的妹妹穿的比我還像新娘响委。我一直安慰自己,他們只是感情好窖梁,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布赘风。 她就那樣靜靜地躺著,像睡著了一般窄绒。 火紅的嫁衣襯著肌膚如雪贝次。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,736評(píng)論 1 312
  • 那天彰导,我揣著相機(jī)與錄音蛔翅,去河邊找鬼。 笑死位谋,一個(gè)胖子當(dāng)著我的面吹牛山析,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掏父,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼笋轨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了赊淑?” 一聲冷哼從身側(cè)響起爵政,我...
    開(kāi)封第一講書(shū)人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎陶缺,沒(méi)想到半個(gè)月后钾挟,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡饱岸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年掺出,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了徽千。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡汤锨,死狀恐怖双抽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情闲礼,我是刑警寧澤牍汹,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站位仁,受9級(jí)特大地震影響柑贞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜聂抢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一钧嘶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧琳疏,春花似錦有决、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至揽趾,卻和暖如春台汇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背篱瞎。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工苟呐, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人俐筋。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓牵素,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親澄者。 傳聞我的和親對(duì)象是個(gè)殘疾皇子笆呆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容