Android實(shí)現(xiàn)圓線按鈕進(jìn)度效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圓線按鈕進(jìn)度效果的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖:
這是一個(gè)在github上的開源控件按鈕View(點(diǎn)擊此處查看),同時(shí)帶有進(jìn)度。
使用方法:把該項(xiàng)目從github上下載下來導(dǎo)入到eclipse,然后作為庫,接下來在其他項(xiàng)目中直接引用即可。然而,我感覺原生項(xiàng)目中的個(gè)別細(xì)節(jié)代碼不是太完善,我在它的MasterLayout.java類增加了一些字段和方法:
// 增加的值,by Phil public static final int START = 1, PAUSE = 2, COMPLETE = 3; // 增加的方法,by Phil public int getState() { return flg_frmwrk_mode; }
新增加的值和方法主要用于判斷當(dāng)前View的狀態(tài)。
現(xiàn)在給出一個(gè)經(jīng)過我改進(jìn)后的使用實(shí)例:
package zhangphil.progressbutton; import com.thbs.progressbutton.MasterLayout; import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.TextView;import android.widget.Toast;import android.os.AsyncTask;import android.os.Bundle;import android.os.SystemClock; public class MainActivity extends ActionBarActivity { private MasterLayout masterLayout; private LongTimeOperationTask mTask; // 顯示進(jìn)度文字 private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); masterLayout = (MasterLayout) findViewById(R.id.progress); masterLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 必須有該方法,該方法是動(dòng)畫進(jìn)度的開始。 // 當(dāng)用戶點(diǎn)擊該按鈕后立即執(zhí)行。 masterLayout.animation(); // 此處的判斷代碼是根據(jù)當(dāng)前的View類型判斷的。 // 如果當(dāng)前View是開始的那個(gè)icon,并且用戶點(diǎn)擊了,那么就開始。 // 在次完成用戶的耗時(shí)操作,比如下載任務(wù)等。 if (masterLayout.getState() == MasterLayout.START) { Toast.makeText(MainActivity.this, '開始...', Toast.LENGTH_SHORT).show(); mTask = new LongTimeOperationTask(); mTask.execute(); } // 用戶點(diǎn)擊了 停止 按鈕。取消任務(wù)。 if (masterLayout.getState() == MasterLayout.PAUSE) { if (mTask != null && mTask.getStatus() == AsyncTask.Status.RUNNING) mTask.cancel(true); // reset()是將該空間復(fù)位到最初始化的階段。 masterLayout.reset(); Toast.makeText(MainActivity.this, '停止!', Toast.LENGTH_SHORT) .show(); } // 此處的View控件顯示是一個(gè) 對(duì)號(hào) icon。 if (masterLayout.getState() == MasterLayout.COMPLETE) { Toast.makeText(MainActivity.this, '完成!', Toast.LENGTH_SHORT) .show(); } } }); tv = (TextView) findViewById(R.id.tv); } private class LongTimeOperationTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { } @Override protected String doInBackground(final String... args) { // 進(jìn)度以百分制標(biāo)識(shí)。 for (int i = 0; i <= 100; i++) { SystemClock.sleep(100); publishProgress(i); } return null; } @Override protected void onProgressUpdate(Integer... progress) { // 此處的 setupprogress 更新圓形按鈕的進(jìn)度。 masterLayout.cusview.setupprogress(progress[0]); // 額外的一個(gè)TextView顯示進(jìn)度。 tv.setText(progress[0] + ' %'); } }}
activity_main.xml文件:
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' > <com.thbs.progressbutton.MasterLayout android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:clickable='true' /> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:gravity='center' android:text='10%' /> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP編碼必備的8條原則2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. 一文帶你搞懂JavaScript中的進(jìn)制與進(jìn)制轉(zhuǎn)換4. 匹配模式 - XSL教程 - 45. 詳解JS前端使用迭代器和生成器原理及示例6. 得到XML文檔大小的方法7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. ASP基礎(chǔ)知識(shí)Command對(duì)象講解9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
