Android中自定义ProgressDialog实现加载滚动条(加载中…)效果

本文所述是通过自定义ProgressDialog实现弹出“加载中……”,效果如下图:

Android中自定义ProgressDialog实现加载滚动条(加载中…)效果_第1张图片


1、xml布局



    
        
        
    


2、自定义ProgressDialog

public class MyProgressDialog extends ProgressDialog {
    private static ProgressDialog progressDialog;

    public MyProgressDialog(Context context){
        super(context);
    }
    public MyProgressDialog(Context context,int theme){
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        // 得到加载view
        View v = inflater.inflate(R.layout.myprogressdialog, null);
        // 加载布局
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.ll_dialog_view);

        // ImageView控件
        ImageView dialogImage = (ImageView) v.findViewById(R.id.dialog_img);
        // 加载动画
        Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.dialog);
        // 显示动画
        dialogImage.startAnimation(animation);

        //为显示dialog的圆角,不加会出现四个黑棱角
        getWindow().setBackgroundDrawable(new BitmapDrawable());

        setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    public static void showDialog(Context context,String msg){
        progressDialog=new MyProgressDialog(context, R.style.dialogBoxTheme);
        progressDialog.setCancelable(false);// 不可以用“返回键”取消
        progressDialog.setCanceledOnTouchOutside(false);//不可点击进度框外部取消
        progressDialog.setMessage(msg);
        progressDialog.show();
    }

    public static void closeDialog(){
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}

3、进度条样式主题



    
    


4、应用

//显示加载中...
LoadingDialog.showLoading(this,"加载中...");
 在执行你的程序之前加上上面一句,即可显示加载中……对话框。


LoadingDialog.closeLoading();
程序执行完后调用上面一句即可关闭。

5、源码

地址:http://download.csdn.net/detail/coderk2014/9663225

你可能感兴趣的:(Android)