Android Dialog文字动态加载效果 Android自定义Dialog实现文字动态加载效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android Dialog文字动态加载效果 Android自定义Dialog实现文字动态加载效果

lv_fq   2021-03-22 我要评论
想了解Android自定义Dialog实现文字动态加载效果的相关内容吗,lv_fq在本文为您仔细讲解Android Dialog文字动态加载效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,dialog,文字动态加载,下面大家一起来学习吧。

之前在技术问答上面看到一个提问 “加载中…” 后面三个点是动态的,这么一个效果实现。想来想去,好像没想到好的处理方式。
尝试了一下,以一个最笨的方式实现了。先来看一下效果 :

我是通过自定义一个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;

@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。

点击下载:源码

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们