Android實現(xiàn)自定義加載框的代碼示例
更新時間:2017年02月07日 08:23:22 作者:涼城舊巷等舊人
本篇文章主要介紹了Android實現(xiàn)自定義加載框的代碼示例,App在與服務器進行網(wǎng)絡交互的時候,有個提示加載框,有興趣的可以了解一下。
App在與服務器進行網(wǎng)絡交互的時候,需要有一個提示的加載框,如圖:
此時我們可以自定義一個加載中的對話框,代碼如下:
public class LoadingDialog extends Dialog { private static final int CHANGE_TITLE_WHAT = 1; private static final int CHNAGE_TITLE_DELAYMILLIS = 300; private static final int MAX_SUFFIX_NUMBER = 3; private static final char SUFFIX = '.'; private ImageView iv_route; private TextView detail_tv; private TextView tv_point; private RotateAnimation mAnim; private Handler handler = new Handler() { private int num = 0; public void handleMessage(android.os.Message msg) { if (msg.what == CHANGE_TITLE_WHAT) { StringBuilder builder = new StringBuilder(); if (num >= MAX_SUFFIX_NUMBER) { num = 0; } num++; for (int i = 0; i < num; i++) { builder.append(SUFFIX); } tv_point.setText(builder.toString()); if (isShowing()) { handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS); } else { num = 0; } } }; }; public LoadingDialog(Context context) { super(context, R.style.Dialog_bocop); init(); } public LoadingDialog(Context context, boolean isTrans) { super(context, isTrans ? R.style.Loading_Dialog_trans : R.style.Dialog_bocop); init(); } private void init() { setContentView(R.layout.common_dialog_loading_layout); iv_route = (ImageView) findViewById(R.id.iv_route); detail_tv = (TextView) findViewById(R.id.detail_tv); tv_point = (TextView) findViewById(R.id.tv_point); initAnim(); getWindow().setWindowAnimations(R.anim.alpha_in); } private void initAnim() { // mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f); mAnim = new RotateAnimation(0, 360, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f); mAnim.setDuration(2000); mAnim.setRepeatCount(Animation.INFINITE); mAnim.setRepeatMode(Animation.RESTART); mAnim.setStartTime(Animation.START_ON_FIRST_FRAME); } @Override public void show() {//在要用到的地方調用這個方法 iv_route.startAnimation(mAnim); handler.sendEmptyMessage(CHANGE_TITLE_WHAT); super.show(); } @Override public void dismiss() { mAnim.cancel(); super.dismiss(); } @Override public void setTitle(CharSequence title) { if (TextUtils.isEmpty(title)) { detail_tv.setText("正在加載"); } else { detail_tv.setText(title); } } @Override public void setTitle(int titleId) { setTitle(getContext().getString(titleId)); } public static void dismissDialog(LoadingDialog loadingDialog) { if (null == loadingDialog) { return; } loadingDialog.dismiss(); } }
-------------對應的布局如下------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="160dp" android:layout_height="160dp" android:layout_gravity="center" android:background="@drawable/common_show_dialog" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="3" android:paddingTop="22dp" android:gravity="center" > <ImageView android:id="@+id/iv_route" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/dialog_bocop_loading_rotate_anim_img" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_marginBottom="15dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:gravity="center_horizontal" > <TextView android:id="@+id/detail_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/tv_point" android:ellipsize="marquee" android:gravity="center" android:singleLine="true" android:text="正在加載..." android:textColor="#ffffff" android:textSize="20sp" /> <TextView android:id="@+id/tv_point" android:layout_width="20dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="..." android:textColor="#ffffff" android:textSize="20sp" /> </RelativeLayout> </LinearLayout>
比如在Activity中要實現(xiàn)加載對話框調用 :
LoadingDialog loadingDialog ; if (null == loadingDialog) { loadingDialog = new LoadingDialog(aty); loadingDialog.setOnCancelListener(this); } loadingDialog.setTitle(“數(shù)據(jù)加載中”); if (!loadingDialog.isShowing()) loadingDialog.show();
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上
這篇文章主要介紹了Android Studio獲取SQLite數(shù)據(jù)并顯示到ListView上,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android?MaterialAlertDialogBuilder修改按鈕屬性
這篇文章主要介紹了Android?MaterialAlertDialogBuilder修改按鈕屬性實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Android利用廣播接收器實現(xiàn)自動填充短信驗證碼
這篇文章主要為大家詳細介紹了Android利用廣播接收器實現(xiàn)自動填充短信驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android中Handler、Thread、HandlerThread三者的區(qū)別
本文主要介紹了Android中Handler、Thread、HandlerThread三者的區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10ToolBar中menu無法同時顯示圖標和文字問題的解決方法
這篇文章主要為大家詳細介紹了ToolBar中menu無法同時顯示圖標和文字問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09