怎么在Android中通过自定义Dialog实现一个加载对话框效果

发布时间:2021-05-14 17:24:12 作者:Leah
来源:亿速云 阅读:216

怎么在Android中通过自定义Dialog实现一个加载对话框效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1、编写自定义布局,dialog_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:gravity="center"
       android:background="@drawable/bg_loading_dialog"
       android:layout_height="match_parent">
  <ImageView
    android:id="@+id/iv_loading"
    android:layout_width="wrap_content"
    android:src="@mipmap/ic_dialog_loading"
    android:layout_height="wrap_content"/>
  <TextView
    android:id="@+id/tv_loading"
    android:layout_width="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/loading"
    android:textSize="16sp"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"/>
</LinearLayout>

2、继承Dialog,覆盖构造方法

public class LoadingDialog extends Dialog {
  private static final String TAG = "LoadingDialog";
  private String mMessage; // 加载中文字
  private int mImageId; // 旋转图片id
  private boolean mCancelable;
  private RotateAnimation mRotateAnimation;
  public LoadingDialog(@NonNull Context context,String message,int imageId) {
    this(context,R.style.LoadingDialog,message,imageId,false);
  }
  public LoadingDialog(@NonNull Context context, int themeResId,String message,int imageId,boolean cancelable) {
    super(context, themeResId);
    mMessage = message;
    mImageId = imageId;
    mCancelable = cancelable;
  }
}

3、覆盖onCreate(),初始化控件

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  initView();
}
private void initView() {
  setContentView(R.layout.dialog_loading);
  // 设置窗口大小
  WindowManager windowManager = getWindow().getWindowManager();
  int screenWidth = windowManager.getDefaultDisplay().getWidth();
  WindowManager.LayoutParams attributes = getWindow().getAttributes();
  // 设置窗口背景透明度
  attributes.alpha = 0.3f;
  // 设置窗口宽高为屏幕的三分之一(为了更好地适配,请别直接写死)
  attributes.width = screenWidth/3;
  attributes.height = attributes.width;
  getWindow().setAttributes(attributes);
  setCancelable(mCancelable);
  TextView tv_loading = findViewById(R.id.tv_loading);
  ImageView iv_loading = findViewById(R.id.iv_loading);
  tv_loading.setText(mMessage);
  iv_loading.setImageResource(mImageId);
  // 先对imageView进行测量,以便拿到它的宽高(否则getMeasuredWidth为0)
  iv_loading.measure(0,0);
  // 设置选择动画
  mRotateAnimation = new RotateAnimation(0,360,iv_loading.getMeasuredWidth()/2,iv_loading.getMeasuredHeight()/2);
  mRotateAnimation.setInterpolator(new LinearInterpolator());
  mRotateAnimation.setDuration(1000);
  mRotateAnimation.setRepeatCount(-1);
  iv_loading.startAnimation(mRotateAnimation);
}

以上代码需要注意设置动画旋转中心坐标为我们imageView的中心点,需要先对imageView进行测量,同时初始化布局的操作请放在onCreate()方法中(别直接在构造方法中初始化布局,这样可以在Dialog要显示的时候才初始化,即调用show方法)。

4、其他

@Override
public void dismiss() {
  mRotateAnimation.cancel();
  super.dismiss();
}
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
  if(keyCode == KeyEvent.KEYCODE_BACK){
    // 屏蔽返回键
    return mCancelable;
  }
  return super.onKeyDown(keyCode, event);
}

这一步需要注意的是我们Dialog在显示的时候就会无限重复(setRepeatCount(-1))执行旋转动画,因此在Dialog消失的时候我们要取消动画,而屏蔽返回键则是为了更好地让窗口的关闭被我们的mCancelable控制。

看到这里你或许想知道我们设置的布局背景drawable,如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="10dp"></corners>
  <solid android:color="@android:color/black"/>
</shape>

你可以自己设置你想要的圆角大小,也可以设置背景颜色(会被透明处理,根据我们为窗口设置的透明度)。

当然,仔细的你会发现我们还少了一些必要的配置,那就是窗口的style,如下:

<style name="LoadingDialog" parent="@android:style/Theme.Holo.Dialog.NoActionBar">
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:backgroundDimEnabled">false</item>
</style>

•android:windowBackground:设置窗口的背景,这里设为透明;
•android:backgroundDimEnabled:设置窗口是否变暗(true变暗,false不变暗,见效果图1和2)。

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 如何用Android实现加载效果
  2. Android实现加载对话框的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android dialog

上一篇:怎么在java中利用多线程下载图片并压缩

下一篇:怎么在Android中动态替换Application

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》