로드 대화 상자(LoadingDialog)

14196 단어

로드 대화 상자(LoadingDialog)


사용법

  • BaseActivity에 다음 코드 추가:
  • private LoadingDialog loadingDialog;
    public LoadingDialog getLoadingDialog() {
        if(loadingDialog == null){
            loadingDialog = new LoadingDialog(this);
        }
        return loadingDialog;
    }
  • BaseActivity 하위 클래스에서 사용:
  •   :getLoadingDialog().show("    ",true); 
      :getLoadingDialog().dismiss();
    //true or false             

    2. 코드 디렉터리


    LoadingDialog.java 클래스 dialogloading.xml 레이아웃loadingdialog_anim.xml 애니메이션shapeloading_dialog.xml 모양 스타일xml 스타일 태그 그림 자원

    3. 코드와 자원

  • LoadingDialog.java 불러오기 대화상자 클래스
  • public class LoadingDialog extends Dialog {
    
        public LoadingDialog(Context context) {
            super(context, R.style.Loading_Dialog);
            setContentView(R.layout.dialog_loading);
        }
    
        public void show(String message,boolean cancelable){
            show(message,cancelable,null);
        }
    
        public void show(String message,boolean cancelable,OnCancelListener cancelListener){
            TextView tv= (TextView)findViewById(R.id.message);
            //      
            if(message == null || message.length() == 0){
                tv.setVisibility(View.GONE);
            }else{
                tv.setText(message);
            }
            //         
            this.setCancelable(cancelable);
            //        
            this.setOnCancelListener(cancelListener);
            //     
            this.getWindow().getAttributes().gravity = Gravity.CENTER;
            WindowManager.LayoutParams lp = this.getWindow().getAttributes();
            //         
            lp.dimAmount = 0.2f;
            this.getWindow().setAttributes(lp);
            //  
            try {
                this.show();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
    
        /**  */
        public void dismiss(){
            try {
                super.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**    ,          */
        public void onWindowFocusChanged(boolean hasFocus) {
            ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
            //   ImageView      
            AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
            //     
            spinner.start();
        }
    
    }
  • dialog_loading.xml 불러오기 대화상자 레이아웃
  • 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/shape_loading_dialog"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="20dp">
    
        <ImageView
            android:id="@+id/spinnerImageView"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@anim/loading_dialog_anim" />
    
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="Message"
            android:textColor="#FFFFFF" />
    
    LinearLayout>
  • loading_dialog_anim.xml 불러오기 대화상자 애니메이션
  • <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
    
        <item
            android:drawable="@drawable/loading_0"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_1"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_2"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_3"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_4"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_5"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_6"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_7"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_8"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_9"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_10"
            android:duration="60" />
        <item
            android:drawable="@drawable/loading_11"
            android:duration="60" />
    animation-list>
  • shape_loading_dialog.xml 대화상자 모양
  • 
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <solid android:color="#ff404040" />
    
        <corners
            android:bottomLeftRadius="8dp"
            android:bottomRightRadius="8dp"
            android:topLeftRadius="8dp"
            android:topRightRadius="8dp" />
    
    shape>
  • styles.xml에 다음 스타일 추가
  • 
    <style name="Loading_Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null
        "android:windowIsFloating">true
        "android:windowContentOverlay">@null
        "android:windowAnimationStyle">@android:style/Animation.Dialog
        "android:windowSoftInputMode">stateUnspecified|adjustPan
        "android:windowBackground">@android:color/transparent
        "android:windowNoTitle">true
    style>
  • 사진 자원 사진 자원 LoadingDialog.rar

  • – the end –

    좋은 웹페이지 즐겨찾기