怎么在Android中通過自定義Dialog實(shí)現(xiàn)一個(gè)加載對(duì)話框效果?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
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è)置窗口寬高為屏幕的三分之一(為了更好地適配,請(qǐng)別直接寫死) 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); // 先對(duì)imageView進(jìn)行測(cè)量,以便拿到它的寬高(否則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),需要先對(duì)imageView進(jìn)行測(cè)量,同時(shí)初始化布局的操作請(qǐng)放在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控制。
看到這里你或許想知道我們?cè)O(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)。
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
名稱欄目:怎么在Android中通過自定義Dialog實(shí)現(xiàn)一個(gè)加載對(duì)話框效果-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://aaarwkj.com/article42/iphec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站改版、網(wǎng)站營銷、用戶體驗(yàn)、網(wǎng)站維護(hù)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容