寫一個自訂進度顏色和圓形轉動的ProgressBar(詳細介紹)

來源:互聯網
上載者:User

標籤:progressbar   自訂   

先:


我們得自訂ProgressBar的樣式

<span style="white-space:pre"></span><style name="self_define_ProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal"> //繼承了android橫向的ProgressBar的樣式      <item name="android:indeterminateOnly">false</item>      <item name="android:progressDrawable">@drawable/self_define_progress</item>  </style>

<?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@android:id/background" >//定義的是背景色<shape><corners android:radius="5dip" /><gradient android:angle="270" android:centerColor="#efefef"android:centerY="0.75" android:endColor="#a3a3a3"android:startColor="#c7c7c7" /></shape></item><item android:id="@android:id/secondaryProgress">   //如果有2級進度,需要定義這一項,gradient定義的是漸層色,clip如果沒有的話,進度條會充滿整個Progessbar<clip><shape><corners android:radius="5dip" /><gradient android:angle="270" android:centerColor="#99CC99"android:centerY="0.75" android:endColor="#99CC99"android:startColor="#99CC99" /></shape></clip></item><item android:id="@android:id/progress">  //這個是定義1級進度,如果1級進度比2級進度大且1級進度不透明,就可以覆蓋掉2級進度<clip><shape><corners android:radius="5dip" /><gradient android:angle="270" android:centerColor="#4abbfd"android:centerY="0.75" android:endColor="#4abbfd"android:startColor="#4abbfd" /></shape></clip></item></layer-list>


在代碼裡這樣使用:

private ProgressBar mProgressBar;private Task mTask = new Task();private int mCount = 0;private Handler mHander = new MyHandler();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);mProgressBar = (ProgressBar)findViewById(R.id.progress);Timer timer = new Timer();timer.schedule(mTask, 1000, 1000);}private class MyHandler extends Handler{@Overridepublic void handleMessage(Message msg) {mCount += 10;mProgressBar.setProgress(mCount);super.handleMessage(msg);}}private class Task extends TimerTask{@Overridepublic void run() {mHander.sendEmptyMessage(10);}}



對於圓形並有轉圈動畫的進度條:shape的值(rectagle矩形,oval橢圓,line水平直線,ring環形) tpye的值:漸層的樣式 liner線性漸層 radial環形漸層 sweep

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"        android:pivotX="50%" android:pivotY="50%"        android:fromDegrees="0" android:toDegrees="360">            <shape            android:shape="ring"<span style="font-family:verdana, ms song, 宋體, Arial, 微軟雅黑, Helvetica, sans-serif;color:#666666;"><span style="font-size: 14px; line-height: 18px;">  </span></span>            android:innerRadiusRatio="3"            android:thicknessRatio="8"            android:useLevel="false">        <size                android:width="37dip"                android:height="37dip"        />                <gradient                android:type="sweep"//type為sweep為環形轉圈                android:useLevel="false"                android:startColor="#F7F7F7"                android:centerColor="#F7F7F7"                android:centerY="0.50"                android:endColor="#B3B4B4"        />            </shape>    </rotate>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.