Android自定義Dialog實現(xiàn)文字動態(tài)加載效果
之前在技術(shù)問答上面看到一個提問 “加載中…” 后面三個點是動態(tài)的,這么一個效果實現(xiàn)。想來想去,好像沒想到好的處理方式。
嘗試了一下,以一個最笨的方式實現(xiàn)了。先來看一下效果 :

我是通過自定義一個Dialog,加載中的效果,是在Dialog內(nèi)部實現(xiàn)的,進度還是從Activity里面控制的。
下面是Dialog實現(xiàn)類:
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就是這樣。然后就是調(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來模擬加載進度,(寫的過程中感覺Timer的定時操作比其他兩種方式用起來方便多了)。
點擊事件我是通過在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="點擊彈框" />
clickable屬性不加上的話,有些手機系統(tǒng)默認是沒法調(diào)用的(之前遇到過小米的,不加這個屬性,不觸發(fā)click事件)
另外,這種click事件的寫法在Fragment是不可用的,只能通過setOnClickListener來觸發(fā)。
更新一種實現(xiàn)方式:
感謝 IT-hero ,又 get 一個 屬性動畫的用法。
下面是 自定義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);
// 設置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。
點擊下載:源碼
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 加載頁面遮擋耗時操作任務頁面--第三方開源之AndroidProgressLayout
- Android開發(fā)中如何解決Fragment +Viewpager滑動頁面重復加載的問題
- Android中替換WebView加載網(wǎng)頁失敗時的頁面
- Android中自定義加載樣式圖片的具體實現(xiàn)
- Android自定義加載loading view動畫組件
- Android自定義加載控件實現(xiàn)數(shù)據(jù)加載動畫
- Android自定義view實現(xiàn)阻尼效果的加載動畫
- Android自定義下拉刷新上拉加載
- Android自定義View實現(xiàn)loading動畫加載效果
- Android實現(xiàn)自定義加載框的代碼示例
- Android開發(fā)實現(xiàn)自定義新聞加載頁面功能實例
相關(guān)文章
Android自定義View實現(xiàn)箭頭沿圓轉(zhuǎn)動實例代碼
這篇文章主要介紹了Android自定義View實現(xiàn)箭頭沿圓轉(zhuǎn)動實例代碼,需要的朋友可以參考下2017-09-09
解決webview內(nèi)的iframe中的事件不可用的問題
這篇文章主要介紹了解決webview內(nèi)的iframe中的事件不可用的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android利用EditText如何實現(xiàn)搜索框詳解
EditText 在開發(fā)中也是經(jīng)常用到的控件,也是一個比較必要的組件,下面這篇文章主要給大家介紹了關(guān)于Android利用EditText如何實現(xiàn)搜索框的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07
詳解RecyclerView設置背景圖片長寬一樣(以GridLayoutManager為例)
這篇文章主要介紹了詳解RecyclerView設置背景圖片長寬一樣(以GridLayoutManager為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Kotlin使用協(xié)程實現(xiàn)高效并發(fā)程序流程詳解
這篇文章主要介紹了Kotlin使用協(xié)程實現(xiàn)高效并發(fā)程序流程,協(xié)程屬于Kotlin中非常有特色的一項技術(shù),因為大部分編程語言中是沒有協(xié)程這個概念的。那么什么是協(xié)程呢?它其實和線程有點相似,可以簡單地將它理解成一種輕量級的線程2023-01-01
Android仿蘋果關(guān)機界面實現(xiàn)代碼
這篇文章主要為大家詳細介紹了Android仿蘋果關(guān)機界面的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
android BottomSheetDialog新控件解析實現(xiàn)知乎評論列表效果(實例代碼)
BottomSheetDialog是一個自定義的從底部滑入的對話框,這篇文章主要介紹了android BottomSheetDialog新控件解析實現(xiàn)知乎評論列表效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

