欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義Dialog實(shí)現(xiàn)文字動(dòng)態(tài)加載效果

 更新時(shí)間:2016年08月29日 15:14:21   作者:lv_fq  
這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog實(shí)現(xiàn)文字動(dòng)態(tài)加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

之前在技術(shù)問(wèn)答上面看到一個(gè)提問(wèn) “加載中…” 后面三個(gè)點(diǎn)是動(dòng)態(tài)的,這么一個(gè)效果實(shí)現(xiàn)。想來(lái)想去,好像沒(méi)想到好的處理方式。
嘗試了一下,以一個(gè)最笨的方式實(shí)現(xiàn)了。先來(lái)看一下效果 :

我是通過(guò)自定義一個(gè)Dialog,加載中的效果,是在Dialog內(nèi)部實(shí)現(xiàn)的,進(jìn)度還是從Activity里面控制的。
下面是Dialog實(shí)現(xiàn)類(lèi):

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);

  // 設(shè)置Dialog顯示的寬度,
  Display d = getWindow().getWindowManager().getDefaultDisplay();
  WindowManager.LayoutParams lp = getWindow().getAttributes();
  //這里設(shè)置為屏幕寬度的百分之八十
  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();
  }
 }

}

布局文件就一個(gè)TextView,一個(gè)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>

因?yàn)闆](méi)想到其他的思路,所以,只能通過(guò)Timer 來(lái)計(jì)時(shí)改變TextView的顯示。。(這里也希望各位大神能指點(diǎn)一下,目前確實(shí)想不到其他思路)
ProgressBar的樣式,上一篇Android 自定義水平進(jìn)度條的圓角進(jìn)度里面有詳細(xì)介紹,這里就不重復(fù)了。
Dialog就是這樣。然后就是調(diào)用了:
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來(lái)模擬加載進(jìn)度,(寫(xiě)的過(guò)程中感覺(jué)Timer的定時(shí)操作比其他兩種方式用起來(lái)方便多了)。
點(diǎn)擊事件我是通過(guò)在xml里面直接調(diào)用的。

<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="點(diǎn)擊彈框" />

clickable屬性不加上的話(huà),有些手機(jī)系統(tǒng)默認(rèn)是沒(méi)法調(diào)用的(之前遇到過(guò)小米的,不加這個(gè)屬性,不觸發(fā)click事件)
另外,這種click事件的寫(xiě)法在Fragment是不可用的,只能通過(guò)setOnClickListener來(lái)觸發(fā)。

更新一種實(shí)現(xiàn)方式:
感謝 IT-hero ,又 get 一個(gè) 屬性動(dòng)畫(huà)的用法。
下面是 自定義Dialog 里的一些調(diào)整 :

private String[] scoreText = {". ", ".. ", "..."};
ValueAnimator valueAnimator;

@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);

  // 設(shè)置Dialog顯示的寬度,
  Display d = getWindow().getWindowManager().getDefaultDisplay();
  WindowManager.LayoutParams lp = getWindow().getAttributes();
  //這里設(shè)置為屏幕寬度的百分之八十
  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();
}
//代碼省略...

因?yàn)闆](méi)找到 CSDN編輯上傳資源 的方式,所以這里 Demo 里面就沒(méi)有添加這個(gè)屬性動(dòng)畫(huà)的代碼,有需要的朋友可以直接從這里copy。

點(diǎn)擊下載:源碼

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論