Android原生控件只有橫向進度條一種,並且無法變換樣式,好比原生rom的樣子android
很醜是吧,當偉大的產品設計要求更換前背景,甚至縱向,甚至圓弧狀的,咋辦,好比canvas
ok,咱們開始吧:ide
一)變換前背景佈局
先來看看progressbar的屬性:spa
- <ProgressBar
- android:id="@+id/progressBar"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="5dip"
- android:layout_toRightOf="@+id/progressBarV"
- android:indeterminate="false"
- android:padding="2dip"
- android:progress="50" />
根據style="?android:attr/progressBarStyleHorizontal",咱們找到源碼中的style.xml設計
- <style name="Widget.ProgressBar.Horizontal">
- <item name="android:indeterminateOnly">false</item>
- <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
- <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
- <item name="android:minHeight">20dip</item>
- <item name="android:maxHeight">20dip</item>
- </style>
看到orm
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item> xml
木有,繼續發掘源碼,找到drawable下面的progress_horizontal.xml,這就是咱們今天的主角了:
blog
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@android:id/background">
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#ff9d9e9d"
- android:centerColor="#ff5a5d5a"
- android:centerY="0.75"
- android:endColor="#ff747674"
- android:angle="270"
- />
- </shape>
- </item>
- <item android:id="@android:id/secondaryProgress">
- <clip>
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#80ffd300"
- android:centerColor="#80ffb600"
- android:centerY="0.75"
- android:endColor="#a0ffcb00"
- android:angle="270"
- />
- </shape>
- </clip>
- </item>
- <item android:id="@android:id/progress">
- <clip>
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#ffffd300"
- android:centerColor="#ffffb600"
- android:centerY="0.75"
- android:endColor="#ffffcb00"
- android:angle="270"
- />
- </shape>
- </clip>
- </item>
- </layer-list>
看到android:id="@android:id/progress"木有,看到android:id="@android:id/secondaryProgress"木有圖片
把這個文件複製到本身工程下的drawable,就能夠爲所欲爲的修改shape的屬性,漸變,圓角等等
那麼怎麼放一個圖片進去呢,ok,新建progress_horizontal1.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@android:id/progress" android:drawable="@drawable/progressbar" />
- </layer-list>
在android:drawable中指定你處理好的圖片
而後回到佈局中
- <ProgressBar
- android:id="@+id/progressBar1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/progressBar"
- android:layout_margin="5dip"
- android:layout_toRightOf="@+id/progressBarV"
- android:background="@drawable/progress_bg"
- android:indeterminate="false"
- android:indeterminateOnly="false"
- android:maxHeight="20dip"
- android:minHeight="20dip"
- android:padding="2dip"
- android:progress="50"
- android:progressDrawable="@drawable/progress_horizontal1" />
android:background="@drawable/progress_bg"指定背景
android:progressDrawable="@drawable/progress_horizontal1"前景使用上面的progress_horizontal1
ok,搞定
注意看,四角仍是有圓倒角的,貌似是系統本身加上去的,總之個人圖片裏面是沒有作這個倒角處理的
二)縱向進度條
仍是得從源碼入手,看回progress_horizontal.xml
- <item android:id="@android:id/progress">
- <clip>
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#ffffd300"
- android:centerColor="#ffffb600"
- android:centerY="0.75"
- android:endColor="#ffffcb00"
- android:angle="270"
- />
- </shape>
- </clip>
- </item>
爲何shape外面要包一層clip呢,官方文檔解釋是clipdrawable是能夠自我複製的,來看看定義
- <?xml version="1.0" encoding="utf-8"?>
- <clip
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:drawable="@drawable/drawable_resource"
- android:clipOrientation=["horizontal" | "vertical"]
- android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
- "fill_vertical" | "center_horizontal" | "fill_horizontal" |
- "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
android:clipOrientation有兩個屬性,默認爲horizontal
android:gravity有兩個屬性,默認爲left
那咱們試試改爲vertical和bottom會有什麼效果,新建一個progress_vertical.xml,把源碼progress_horizontal.xml的內容複製過來,這裏去掉了secondaryProgress,修改了clip,shape的漸變中心centerY改成centerX
- <item android:id="@android:id/progress">
- <clip
- android:clipOrientation="vertical"
- android:gravity = "bottom">
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#ffffd300"
- android:centerColor="#ffffb600"
- android:centerX="0.75"
- android:endColor="#ffffcb00"
- android:angle="90"
- />
- </shape>
- </clip>
- </item>
佈局中android:progressDrawable="@drawable/progress_vertical"
ok,搞定,就是這麼簡單:
三)弧形bar
這個也許算不上是進度條,用的也很少,最多也就儀表盤用用,否則誰會把進度條整成圓弧的呢。好吧這個可不是改改源碼就能搞定的,看代碼
- public class Arcs extends View {
- private Paint mArcPaint;
- private Paint mArcBGPaint;
- private RectF mOval;
- private float mSweep = 0;
- private int mSpeedMax = 200;
- private int mThreshold = 100;
- private int mIncSpeedValue = 0;
- private int mCurrentSpeedValue = 0;
- private float mCenterX;
- private float mCenterY;
- private float mSpeedArcWidth;
- private final float SPEED_VALUE_INC = 2;
- ..........
- }
首先是一堆成員變量,兩個Paint用來畫圓弧一個前景一個背景,一個RectF圓弧就畫在上面,而後是一些控制參數好比sweep圓弧掃過的角度,xy座標等等
- mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mArcPaint.setStyle(Paint.Style.STROKE);
- mArcPaint.setStrokeWidth(mSpeedArcWidth);
- // mPaint.setStrokeCap(Paint.Cap.ROUND);
- mArcPaint.setColor(0xff81ccd6);
- BlurMaskFilter mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.INNER);
- mArcPaint.setMaskFilter(mBlur);
- mArcBGPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mArcBGPaint.setStyle(Paint.Style.STROKE);
- mArcBGPaint.setStrokeWidth(mSpeedArcWidth+8);
- mArcBGPaint.setColor(0xff171717);
- BlurMaskFilter mBGBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.INNER);
- mArcBGPaint.setMaskFilter(mBGBlur);
設置兩個畫筆,顏色,寬度,樣式等等,BlurMaskFilter筆是邊緣模糊效果,有幾種,能夠本身嘗試
- @Override
- protected void onSizeChanged(int w, int h, int ow, int oh) {
- super.onSizeChanged(w, h, ow, oh);
- Log.i("onSizeChanged w", w+"");
- Log.i("onSizeChanged h", h+"");
- mCenterX = w * 0.5f; // remember the center of the screen
- mCenterY = h - mSpeedArcWidth;
- mOval = new RectF(mCenterX - mCenterY, mSpeedArcWidth, mCenterX + mCenterY, mCenterY * 2);
- }
重寫父類View的onSizeChanged,爲的是本身根據佈局中的大小作居中處理
- @Override
- protected void onDraw(Canvas canvas) {
- drawSpeed(canvas);
- calcSpeed();
- }
- private void drawSpeed(Canvas canvas) {
- canvas.drawArc(mOval, 179, 181, false, mArcBGPaint);
- mSweep = (float) mIncSpeedValue / mSpeedMax * 180;
- if (mIncSpeedValue > mThreshold) {
- mArcPaint.setColor(0xFFFF0000);
- }
- else {
- mArcPaint.setColor(0xFF00B0F0);
- }
- canvas.drawArc(mOval, 180, mSweep, false, mArcPaint);
- }
- private void calcSpeed() {
- if (mIncSpeedValue < mCurrentSpeedValue) {
- mIncSpeedValue += SPEED_VALUE_INC;
- if (mIncSpeedValue > mCurrentSpeedValue) {
- mIncSpeedValue = mCurrentSpeedValue;
- }
- invalidate();
- }
- else if (mIncSpeedValue > mCurrentSpeedValue) {
- mIncSpeedValue -= SPEED_VALUE_INC;
- if (mIncSpeedValue < mCurrentSpeedValue) {
- mIncSpeedValue = mCurrentSpeedValue;
- }
- invalidate();
- }
- }
重寫onDraw以便重繪canvas
drawSpeed裏面
經過計算mSweep = (float) mIncSpeedValue / mSpeedMax * 180;
而後canvas.drawArc(mOval, 180, mSweep, false, mArcPaint);
會根據mSweep的變化,畫出相應長度的弧度來
根據與閾值的對比,還能夠設定不一樣的 顏色:
if (mIncSpeedValue > mThreshold) {
mArcPaint.setColor(0xFFFF0000);
}
else {
mArcPaint.setColor(0xFF00B0F0);
}
calcSpeed經過一個步進來控制增量或減量,以使弧度天然過渡,減小跳躍
ok,大功告成