激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android自定義Dialog實現加載對話框效果

Android自定義Dialog實現加載對話框效果

2022-02-19 16:44宿罪 Android

這篇文章將介紹如何定制當今主流的對話框,通過自定義dialog實現加載對話框效果,具體實現代碼大家通過本文學習吧

前言

最近開發中用到許多對話框,之前都是在外面的代碼中創建AlertDialog并設置自定義布局實現常見的對話框,諸如更新提示等含有取消和刪除兩個按鈕的對話框我們可以通過代碼創建一個AlertDialog并通過它暴露的一系列方法設置我們自定義的布局和style,但有時候系統的AlertDialog并不能實現更好的定制,這時,我們就想到了自定義Dialog。通過查看AlertDialog的類結構發現它也是繼承于Dialog,于是我們也可以通過繼承Dialog實現我們自定義的Dialog。這篇文章將介紹如何定制當今主流的對話框,先上效果圖,給大家養養眼。

Android自定義Dialog實現加載對話框效果Android自定義Dialog實現加載對話框效果

代碼實現

1、編寫自定義布局,dialog_loading.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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,覆蓋構造方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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(),初始化控件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@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、其他

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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,如下:

?
1
2
3
4
5
<?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,如下:

?
1
2
3
4
<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)。

最后奉上這篇文章的github:https://github.com/ydxlt/LoadingDialog

總結

以上所述是小編給大家介紹的Android自定義Dialog實現加載對話框效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/ydxlt/article/details/80290053

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人亚洲 | 性欧美极品xxxx欧美一区二区 | 成年人小视频在线观看 | 精品成人免费一区二区在线播放 | 国产69精品久久久久久久久久 | 久久精品79国产精品 | 日韩视频高清 | 91福利国产在线观一区二区 | 免费男女乱淫真视频 | 蜜桃欧美性大片免费视频 | 天天干导航 | 4399一级成人毛片 | 色婷婷综合久色aⅴ | 国产精品自拍啪啪 | 一级做a爱片性色毛片高清 国产精品色在线网站 | 成人免费观看在线 | 国产精品视频一区二区三区四 | 免费一级毛片免费播放 | 久久成人福利 | 欧美日本一 | 国产91久久精品一区二区 | 91精品国产乱码久 | 国产免费一区二区三区网站免费 | 国产高潮好爽受不了了夜色 | 成人电影毛片 | 日朝毛片 | 国产精品视频一区二区三区综合 | 曰韩毛片 | 精品国产一区二区三区四区阿崩 | 成人一区三区 | 国产精品69久久 | 中文字幕11| 亚洲欧美日韩精品久久 | 一区二区三区国产在线 | 成人h视频在线 | 黑人一级片视频 | 免费性爱视频 | 久久精片 | 欧美老逼 | 色a综合| 欧美 中文字幕 |