android-個人化進度條

來源:互聯網
上載者:User

android-個人化進度條

1.案例

2.準備素材

progress1.png(78*78) progress2.png(78*78)

3.原理

採用一張圖片作為ProgressBar的背景圖片(一般採用顏色比較淺的)。另一張是捲軸的圖片(一般採用顏色比較深的圖片)。進度在滾動是:滾動圖片逐步顯示,背景圖片逐步隱藏,達到上面的效果。

4.靈感來自Android控制項提供的源碼

4.1 預設帶進度的捲軸,如

 

    

 

注意:關鍵是style屬性在起作用

4.2 找到樣式定義的位置

滑鼠放在style屬性值上,按下Ctrl鍵,出現超連結,點擊超連結跳轉到樣式的定義位置

樣式定義的內容如下

重點研究:

android:progressDrawable:捲軸的樣式

@android:drawable/progress_horizontal:樣式定義的檔案

在android-sdk-windowsplatformsandroid-14data es目下搜尋progress_horizontal.xml檔案,搜尋結果如下:

開啟progress_horizontal.xml檔案,內容如下

 

                        

釋義:

 

定義捲軸的背景樣式

第二捲軸的樣式

捲軸的樣式

思考:如果我想做垂直捲軸,怎麼辦了?

關鍵在clip元素的屬性上做修改

<clip
android:clipOrientation
=vertical 定義滾動的方向 vertical為垂直方向
android:drawable=@drawable/progress1 定義滾動的圖片
android:gravity=bottom > 定義滾動的開始位置

5.定義樣式檔案progress_vertical.xml

progress_vertical.xml檔案代碼如下

 


 

 

6.應用自訂的樣式

 


 

 

7.點擊按鈕類比滾動的效果

 

publicclass ProgressActivity extends Activity {          ProgressBar pb =null;     TextView txtProgress;     Handler handler =new Handler();     @Override     publicvoid onCreate(BundlesavedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          System.out.println(主題= + getTheme() + );          pb = (ProgressBar) findViewById(R.id.pbPic);

 
//按鈕
Button btnStart = (Button) findViewById(R.id.btStart);

 

 
//顯示進度
txtProgress = (TextView) findViewById(R.id.txtProgress);

 

 
//按鈕點擊事件
btnStart.setOnClickListener(new OnClickListener() { publicvoid onClick(Viewv) {

 

 
//建立並啟動線程,使用線程執行類比的任務
new Thread(new Runnable() { publicvoid run() { for (inti = 0; i < 100;i++) {//迴圈100遍 try { handler.post(new Runnable() { //更新介面的資料 publicvoid run() { pb.incrementProgressBy(1);//增加進度

 

 
//顯示完成的進度
txtProgress.setText(pb.getProgress() + %); } }); Thread.sleep(100); } catch (InterruptedExceptione) { } } } }).start(); } }); }}

 

 

聯繫我們

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