查看: 2228|回复: 0

[Android教程] Android实现自定义加载框的代码示例

发表于 2017-8-6 08:53:09

App在与服务器进行网络交互的时候,需要有一个提示的加载框,如图:

此时我们可以自定义一个加载中的对话框,代码如下:

  1. public class LoadingDialog extends Dialog {
  2. private static final int CHANGE_TITLE_WHAT = 1;
  3. private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
  4. private static final int MAX_SUFFIX_NUMBER = 3;
  5. private static final char SUFFIX = '.';
  6. private ImageView iv_route;
  7. private TextView detail_tv;
  8. private TextView tv_point;
  9. private RotateAnimation mAnim;
  10. private Handler handler = new Handler() {
  11. private int num = 0;
  12. public void handleMessage(android.os.Message msg) {
  13. if (msg.what == CHANGE_TITLE_WHAT) {
  14. StringBuilder builder = new StringBuilder();
  15. if (num >= MAX_SUFFIX_NUMBER) {
  16. num = 0;
  17. }
  18. num++;
  19. for (int i = 0; i < num; i++) {
  20. builder.append(SUFFIX);
  21. }
  22. tv_point.setText(builder.toString());
  23. if (isShowing()) {
  24. handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS);
  25. }
  26. else {
  27. num = 0;
  28. }
  29. }
  30. };
  31. };
  32. public LoadingDialog(Context context) {
  33. super(context, R.style.Dialog_bocop);
  34. init();
  35. }
  36. public LoadingDialog(Context context, boolean isTrans) {
  37. super(context, isTrans ? R.style.Loading_Dialog_trans : R.style.Dialog_bocop);
  38. init();
  39. }
  40. private void init() {
  41. setContentView(R.layout.common_dialog_loading_layout);
  42. iv_route = (ImageView) findViewById(R.id.iv_route);
  43. detail_tv = (TextView) findViewById(R.id.detail_tv);
  44. tv_point = (TextView) findViewById(R.id.tv_point);
  45. initAnim();
  46. getWindow().setWindowAnimations(R.anim.alpha_in);
  47. }
  48. private void initAnim() {
  49. // mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
  50. mAnim = new RotateAnimation(0, 360, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
  51. mAnim.setDuration(2000);
  52. mAnim.setRepeatCount(Animation.INFINITE);
  53. mAnim.setRepeatMode(Animation.RESTART);
  54. mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
  55. }
  56. @Override
  57. public void show() {//在要用到的地方调用这个方法
  58. iv_route.startAnimation(mAnim);
  59. handler.sendEmptyMessage(CHANGE_TITLE_WHAT);
  60. super.show();
  61. }
  62. @Override
  63. public void dismiss() {
  64. mAnim.cancel();
  65. super.dismiss();
  66. }
  67. @Override
  68. public void setTitle(CharSequence title) {
  69. if (TextUtils.isEmpty(title)) {
  70. detail_tv.setText("正在加载");
  71. }
  72. else {
  73. detail_tv.setText(title);
  74. }
  75. }
  76. @Override
  77. public void setTitle(int titleId) {
  78. setTitle(getContext().getString(titleId));
  79. }
  80. public static void dismissDialog(LoadingDialog loadingDialog) {
  81. if (null == loadingDialog) { return; }
  82. loadingDialog.dismiss();
  83. }
  84. }
复制代码

-------------对应的布局如下------------------

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="160dp"
  5. android:layout_height="160dp"
  6. android:layout_gravity="center"
  7. android:background="@drawable/common_show_dialog"
  8. android:orientation="vertical" >
  9. <RelativeLayout
  10. android:layout_width="fill_parent"
  11. android:layout_height="0dp"
  12. android:layout_weight="3"
  13. android:paddingTop="22dp"
  14. android:gravity="center" >
  15. <ImageView
  16. android:id="@+id/iv_route"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_centerHorizontal="true"
  20. android:layout_centerVertical="true"
  21. android:background="@drawable/dialog_bocop_loading_rotate_anim_img" />
  22. </RelativeLayout>
  23. <RelativeLayout
  24. android:layout_width="fill_parent"
  25. android:layout_height="0dp"
  26. android:layout_marginBottom="15dp"
  27. android:layout_marginLeft="10dp"
  28. android:layout_marginRight="10dp"
  29. android:layout_weight="1"
  30. android:gravity="center_horizontal" >
  31. <TextView
  32. android:id="@+id/detail_tv"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_toLeftOf="@+id/tv_point"
  36. android:ellipsize="marquee"
  37. android:gravity="center"
  38. android:singleLine="true"
  39. android:text="正在加载..."
  40. android:textColor="#ffffff"
  41. android:textSize="20sp" />
  42. <TextView
  43. android:id="@+id/tv_point"
  44. android:layout_width="20dp"
  45. android:layout_height="wrap_content"
  46. android:layout_alignParentRight="true"
  47. android:text="..."
  48. android:textColor="#ffffff"
  49. android:textSize="20sp" />
  50. </RelativeLayout>
  51. </LinearLayout>
复制代码

比如在Activity中要实现加载对话框调用 :

  1. LoadingDialog loadingDialog ;
  2. if (null == loadingDialog) {
  3. loadingDialog = new LoadingDialog(aty);
  4. loadingDialog.setOnCancelListener(this);
  5. }
  6. loadingDialog.setTitle(“数据加载中”);
  7. if (!loadingDialog.isShowing()) loadingDialog.show();
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条