Android自定義Dialog實(shí)現(xiàn)加載對話框效果
前言
最近開發(fā)中用到許多對話框,之前都是在外面的代碼中創(chuàng)建AlertDialog并設(shè)置自定義布局實(shí)現(xiàn)常見的對話框,諸如更新提示等含有取消和刪除兩個(gè)按鈕的對話框我們可以通過代碼創(chuàng)建一個(gè)AlertDialog并通過它暴露的一系列方法設(shè)置我們自定義的布局和style,但有時(shí)候系統(tǒng)的AlertDialog并不能實(shí)現(xiàn)更好的定制,這時(shí),我們就想到了自定義Dialog。通過查看AlertDialog的類結(jié)構(gòu)發(fā)現(xiàn)它也是繼承于Dialog,于是我們也可以通過繼承Dialog實(shí)現(xiàn)我們自定義的Dialog。這篇文章將介紹如何定制當(dāng)今主流的對話框,先上效果圖,給大家養(yǎng)養(yǎng)眼。
代碼實(shí)現(xiàn)
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,覆蓋構(gòu)造方法
public class LoadingDialog extends Dialog { private static final String TAG = "LoadingDialog"; private String mMessage; // 加載中文字 private int mImageId; // 旋轉(zhuǎn)圖片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); // 設(shè)置窗口大小 WindowManager windowManager = getWindow().getWindowManager(); int screenWidth = windowManager.getDefaultDisplay().getWidth(); WindowManager.LayoutParams attributes = getWindow().getAttributes(); // 設(shè)置窗口背景透明度 attributes.alpha = 0.3f; // 設(shè)置窗口寬高為屏幕的三分之一(為了更好地適配,請別直接寫死) 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進(jìn)行測量,以便拿到它的寬高(否則getMeasuredWidth為0) iv_loading.measure(0,0); // 設(shè)置選擇動(dòng)畫 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); }
以上代碼需要注意設(shè)置動(dòng)畫旋轉(zhuǎn)中心坐標(biāo)為我們imageView的中心點(diǎn),需要先對imageView進(jìn)行測量,同時(shí)初始化布局的操作請放在onCreate()方法中(別直接在構(gòu)造方法中初始化布局,這樣可以在Dialog要顯示的時(shí)候才初始化,即調(diào)用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在顯示的時(shí)候就會(huì)無限重復(fù)(setRepeatCount(-1))執(zhí)行旋轉(zhuǎn)動(dòng)畫,因此在Dialog消失的時(shí)候我們要取消動(dòng)畫,而屏蔽返回鍵則是為了更好地讓窗口的關(guān)閉被我們的mCancelable控制。
看到這里你或許想知道我們設(shè)置的布局背景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>
你可以自己設(shè)置你想要的圓角大小,也可以設(shè)置背景顏色(會(huì)被透明處理,根據(jù)我們?yōu)榇翱谠O(shè)置的透明度)。
當(dāng)然,仔細(xì)的你會(huì)發(fā)現(xiàn)我們還少了一些必要的配置,那就是窗口的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:設(shè)置窗口的背景,這里設(shè)為透明;
•android:backgroundDimEnabled:設(shè)置窗口是否變暗(true變暗,false不變暗,見效果圖1和2)。
最后奉上這篇文章的github:https://github.com/ydxlt/LoadingDialog
總結(jié)
以上所述是小編給大家介紹的Android自定義Dialog實(shí)現(xiàn)加載對話框效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android自定義Dialog的2種常見方法
- Android自定義Dialog框樣式
- Android自定義Dialog原理實(shí)例解析
- Android 自定義加載動(dòng)畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- android自定義Dialog彈框和背景陰影顯示效果
- Android自定義Dialog實(shí)現(xiàn)通用圓角對話框
- Android自定義dialog 自下往上彈出的實(shí)例代碼
- Android 自定義Dialog去除title導(dǎo)航欄的解決方法
- Android編程自定義AlertDialog樣式的方法詳解
- 解決Android中自定義DialogFragment解決寬度和高度問題
- Android?自定義?Dialog?實(shí)現(xiàn)列表?單選、多選、搜索功能
相關(guān)文章
Android內(nèi)存泄漏排查利器LeakCanary
這篇文章主要為大家詳細(xì)介紹了Android內(nèi)存泄漏排查利器LeakCanary的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄(Translucent Bars)
這篇文章主要為大家詳細(xì)介紹了Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄,對狀態(tài)欄(Status Bar)和下方導(dǎo)航欄(Navigation Bar)進(jìn)行半透明處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09點(diǎn)擊微信內(nèi)網(wǎng)頁a標(biāo)簽直接跳轉(zhuǎn)打開淘寶APP的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何實(shí)現(xiàn)點(diǎn)擊微信內(nèi)網(wǎng)頁a標(biāo)簽直接跳轉(zhuǎn)打開淘寶APP的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-11-11分析Android 11.0Settings源碼之主界面加載
這篇文章主要介紹了分析Android 11.0Settings源碼之主界面加載,對Android源碼感興趣的同學(xué),可以著重看一下2021-04-04android9.0 默認(rèn)apk權(quán)限添加方法
本文給大家分享android9.0 默認(rèn)apk權(quán)限添加方法,默認(rèn)賦予全部權(quán)限,根據(jù)包名賦予權(quán)限,通過default-permissions-google.xml的方式實(shí)現(xiàn),文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-06-06關(guān)于Android添加fragment后版本不兼容問題
這篇文章主要介紹了Android添加fragment后版本不兼容問題的解決方法,需要的朋友可以參考下2017-12-12