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. Struts2獲取參數(shù)的三種方法總結(jié)2. JSP中Servlet的Request與Response的用法與區(qū)別3. IntelliJ IDEA刪除類的方法步驟4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. vue cli4下環(huán)境變量和模式示例詳解7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. Django視圖類型總結(jié)9. IntelliJ IDEA導(dǎo)入jar包的方法10. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
