怎么在Android中通過自定義Dialog實(shí)現(xiàn)一個(gè)加載對話框效果-創(chuàng)新互聯(lián)

怎么在Android中通過自定義Dialog實(shí)現(xiàn)一個(gè)加載對話框效果?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過十多年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對網(wǎng)站進(jìn)行成都網(wǎng)站建設(shè)、做網(wǎng)站、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。

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

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

名稱欄目:怎么在Android中通過自定義Dialog實(shí)現(xiàn)一個(gè)加載對話框效果-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://bm7419.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)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)