2

有两种实现效果,一种是系统默认效果,一种是自定义效果;默认效果比较简单但效果也比较单调,自定义可以做出比较好看的效果来;

第一种:通过创建一个ProgressDialog对象并设置属性来显示

public class Util {
    
    
    private static ProgressDialog processDia;

    /**
     * 显示加载中对话框
     * 
     * @param context
     */
    public static void showLoadingDialog(Context context,String message,boolean isCancelable) {
        if (processDia == null) {
            processDia= new ProgressDialog(context,R.style.dialog);
            //点击提示框外面是否取消提示框
            processDia.setCanceledOnTouchOutside(false);
            //点击返回键是否取消提示框
            processDia.setCancelable(isCancelable);
            processDia.setIndeterminate(true);
            processDia.setMessage(message);
            processDia.show();  
        }
    }

    /**
     * 关闭加载对话框
     */
    public static void closeLoadingDialog() {
        if (processDia != null) {
            if (processDia.isShowing()) {
                processDia.cancel();
            }
            processDia = null;
        }
    }
}

其中style.xml中需要设置dialog的背景、字体等属性

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textSize">25sp</item>
</style>

第二种:首先要准备至少两张图片,图片在切换的过程可以形成动画效果即可;

定义动画切换特效,anim/loading.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" >
    <item android:drawable="@drawable/progress_loading_image_01" android:duration="150"/>
    <item android:drawable="@drawable/progress_loading_image_02" android:duration="150"/>
</animation-list>

自定义dialog页面progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/loadingIv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/loading"/>

    <TextView
        android:id="@+id/loadingTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/loadingIv"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:text="正在加载中.." />

</RelativeLayout>

创建diglog类MyProgressDialog

public class MyProgressDialog extends ProgressDialog {

    private AnimationDrawable mAnimation;
    private ImageView mImageView;
    private TextView mTextView;
    private String loadingTip;
    private int resid;

    /**
     * 
     * @param context 上下文对象
     * @param content 显示文字提示信息内容
     * @param id 动画id
     */
    public MyProgressDialog(Context context, String content, int resid) {
        super(context);
        this.loadingTip = content;
        this.resid = resid;
        //点击提示框外面是否取消提示框
        setCanceledOnTouchOutside(false);
        //点击返回键是否取消提示框
        setCancelable(false);
        setIndeterminate(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_dialog);
        
        mTextView = (TextView) findViewById(R.id.loadingTv);
        mImageView = (ImageView) findViewById(R.id.loadingIv);
        
        mImageView.setBackgroundResource(resid);
        // 通过ImageView对象拿到背景显示的AnimationDrawable
        mAnimation = (AnimationDrawable) mImageView.getBackground();
        
        mImageView.post(new Runnable() {
            @Override
            public void run() {
                mAnimation.start();
            }
        });
        mTextView.setText(loadingTip);
    }
}

最后在activity中进行调用,这样就完成了一个自定义的dialog提示框

public class MainActivity extends Activity {
    private MyProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.btn_start);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showMyDialog(view);
            }
        });
    }
    /**
     * 显示对话框
     * @param v
     */
    public void showMyDialog(View v){
        dialog =new MyProgressDialog(this, "正在加载中",R.anim.loading);
        dialog.show();
        Handler handler =new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 3000);
    }
}

司马老牛
477 声望8 粉丝

一个普通而执着的程序员