Android自訂Dialog實現文字動態載入效果_Android

來源:互聯網
上載者:User

之前在技術問答上面看到一個提問 “載入中…” 後面三個點是動態,這麼一個效果實現。想來想去,好像沒想到好的處理方式。
嘗試了一下,以一個最笨的方式實現了。先來看一下效果 :

我是通過自訂一個Dialog,載入中的效果,是在Dialog內部實現的,進度還是從Activity裡面控制的。
下面是Dialog實作類別:

public class CustomDialog extends AlertDialog { public CustomDialog(Context context) {  super(context); } private TextView tv_loading; private ProgressBar progressBar; private Timer timer; private int count = 1; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.dialog_progress);  tv_loading = (TextView) findViewById(R.id.tv_loading);  progressBar = (ProgressBar) findViewById(R.id.pb);  // 設定Dialog顯示的寬度,  Display d = getWindow().getWindowManager().getDefaultDisplay();  WindowManager.LayoutParams lp = getWindow().getAttributes();  //這裡設定為螢幕寬度的百分之八十  lp.width = (int) (d.getWidth() * 0.8);  getWindow().setAttributes(lp);  timer = new Timer();  timer.schedule(new TimerTask() {   @Override   public void run() {    handler.sendEmptyMessage(0);   }  }, 300, 300);  setOnDismissListener(new OnDismissListener() {   @Override   public void onDismiss(DialogInterface dialog) {    if (timer != null) {     timer.cancel();    }   }  }); } Handler handler = new Handler() {  @Override  public void handleMessage(Message msg) {   count++;   if (count > 3) {    count = 1;   }   switch (count) {    case 1:     tv_loading.setText("載入中.");     break;    case 2:     tv_loading.setText("載入中..");     break;    case 3:     tv_loading.setText("載入中...");     break;   }  } }; public void setProgress(int progress) {  progressBar.setProgress(progress);  if (progress == 100) {   this.dismiss();  } }}

布局檔案就一個TextView,一個ProgressBar,
dialog_progress.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shape_dialog_bg" android:orientation="vertical" android:padding="10dp"> <TextView  android:id="@+id/tv_loading"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_margin="20dp"  android:text="載入中..."  android:textSize="16sp" /> <ProgressBar  android:id="@+id/pb"  style="@android:style/Widget.ProgressBar.Horizontal"  android:layout_width="match_parent"  android:layout_height="10dp"  android:max="100"  android:progressDrawable="@drawable/layer_list_progress_drawable" /></LinearLayout>

因為沒想到其他的思路,所以,只能通過Timer 來計時改變TextView的顯示。。(這裡也希望各位大神能指點一下,目前確實想不到其他思路)
ProgressBar的樣式,上一篇Android 自訂水平進度條的圓角進度裡面有詳細介紹,這裡就不重複了。
Dialog就是這樣。然後就是調用了:
MainActivity.class

public class MainActivity extends FragmentActivity { private CustomDialog customDialog; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  customDialog = new CustomDialog(this); } private int count = 0; public void tvClick(View view) {  customDialog.show();  final Timer timer = new Timer();  timer.schedule(new TimerTask() {   @Override   public void run() {    count += 10;    runOnUiThread(new Runnable() {     @Override     public void run() {      if (customDialog != null && customDialog.isShowing()) {       customDialog.setProgress(count);      }     }    });    if (count >= 100) {     timer.cancel();    }   }  }, 0, 500);  customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {   @Override   public void onDismiss(DialogInterface dialog) {    if (timer != null) timer.cancel();    count = 0;   }  }); }}

這裡也是用的Timer來類比載入進度,(寫的過程中感覺Timer的定時操作比其他兩種方式用起來方便多了)。
點擊事件我是通過在xml裡面直接調用的。

<TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_gravity="center_horizontal"  android:clickable="true"  android:onClick="tvClick"  android:padding="10dp"  android:text="點擊彈框" />

clickable屬性不加上的話,有些手機系統預設是沒法調用的(之前遇到過小米的,不加這個屬性,不觸發click事件)
另外,這種click事件的寫法在Fragment是停用,只能通過setOnClickListener來觸發。

更新一種實現方式:
感謝 IT-hero ,又 get 一個 屬性動畫的用法。
下面是 自訂Dialog 裡的一些調整 :

private String[] scoreText = {". ", ".. ", "..."};ValueAnimator valueAnimator;@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.dialog_progress);  tv_loading = (TextView) findViewById(R.id.tv_loading);  progressBar = (ProgressBar) findViewById(R.id.pb);  // 設定Dialog顯示的寬度,  Display d = getWindow().getWindowManager().getDefaultDisplay();  WindowManager.LayoutParams lp = getWindow().getAttributes();  //這裡設定為螢幕寬度的百分之八十  lp.width = (int) (d.getWidth() * 0.8);  getWindow().setAttributes(lp);  if (valueAnimator == null) {   valueAnimator = ValueAnimator.ofInt(0, 3).setDuration(1000);   valueAnimator.setRepeatCount(ValueAnimator.INFINITE);   valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {    @Override    public void onAnimationUpdate(ValueAnimator animation) {     int i = (int) animation.getAnimatedValue();     tv_loading.setText("載入中" + scoreText[i % scoreText.length]);    }   });  }  valueAnimator.start();}//代碼省略...

因為沒找到 CSDN編輯上傳資源 的方式,所以這裡 Demo 裡面就沒有添加這個屬性動畫的代碼,有需要的朋友可以直接從這裡copy。

點擊下載:源碼

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

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