Android自定义progressDialog实现载入动画

         Android开发中,某些耗时操作一般会使用进度条之类的来等待加载,一般有两种:progressbar和progressDialog,区别在于前者是一个控件,后者是对话框。由于一些需求在弹出进度条时不希望用户 能够操作其他控件,所以只能使用progressDialog,这个时候有遇到了一个问题,我不想要系统progressDialog的黑色框框,这就需要我们自定义的progressDialog。

        下面就是我自己写的一个自定义progressDialog。亲测通过!!!

        1. 要在Android开发工程的res-drawable下放置动画的loading图片,比如:

Android自定义progressDialog实现载入动画_第1张图片

       2. 定义progressDialog的风格,设置风格为背景透明transparent,在工程values目录下的styles.xml中编写



    

    
     3. layout目录下编写loadingprogress.xml文件,定义自己的布局,一个进度条和一串显示的内容




    

    


    4.在res-anim目录下编写loading.xml动画,需要我们事先放入到drawable中的动画图片

 



    
    
    
    
    
    
    
    

   5.LoadingProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。

/**
 * 
 * 页面加载动画类
 * 
 * @author WTY
 *
 */

public class LoadingProgressDialog extends Dialog {
	private Context context = null;
	private static LoadingProgressDialog customProgressDialog = null;

	public LoadingProgressDialog(Context context) {
		super(context);
		this.context = context;
	}

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

	public static LoadingProgressDialog createDialog(Context context) {
		customProgressDialog = new LoadingProgressDialog(context,R.style.myProgressDialog);
		customProgressDialog.setContentView(R.layout.loadingprogress);
		customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

		return customProgressDialog;
	}

	public void onWindowFocusChanged(boolean hasFocus) {

		if (customProgressDialog == null) {
			return;
		}
		ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loading_iv);
		AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
		animationDrawable.start();
	}

	/**
	 * 
	 * setTitile 标题
	 * 
	 * @param strTitle
	 * @return
	 * 
	 */
	public LoadingProgressDialog setTitile(String strTitle) {
		return customProgressDialog;
	}

	/**
	 * 
	 * setMessage 提示内容
	 * 
	 * @param strMessage
	 * @return
	 * 
	 */
	public LoadingProgressDialog setMessage(String strMessage) {
		TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.progress_tv);
		if (tvMsg != null) {
			tvMsg.setText(strMessage);
		}
		return customProgressDialog;
	}
}
  6. 编写加载页面的java文件Frgment_MainSearch.java,当搜索时在页面数据加载成功前需要显示上述编写的自定义加载动画

public class Fragment_MainSearch extends Fragment {

        private View mSearchView;
        private ImageButton mSearchBtn;
        private LoadingProgressDialog mProgress = null;
        private AsyncSearchDishTask mTask;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null)	return null;
		mSearchView = (View) inflater.inflate(R.layout.fragment_main_search,container, false);
		initView();
                setListener();
		return mSearchView;
	}

	private void initView() {
		mSearchBtn = (ImageButton) mSearchView.findViewById(R.id.search_submit);
	}

	private void setListener() {		
		mSearchBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new AsyncSearchDishTask().execute();			
			}
		});
	}

	public class AsyncSearchDishTask extends AsyncTask {
		protected Object doInBackground(String... params) {
                        //后续代码 
                }

		protected void onPostExecute(String result) {
			stopProgressDialog();
			//后续代码
		}

		protected void onPreExecute() {
			super.onPreExecute();
			startProgressDialog();
		}

	}

	private void startProgressDialog() {
		if (mProgress == null) {
			mProgress = LoadingProgressDialog.createDialog(getActivity());
			mProgress.setMessage("努力加载中...");
		}
		mProgress.show();
	}

	private void stopProgressDialog() {
		if (mProgress != null) {
			mProgress.dismiss();
			mProgress = null;
		}
	}

}

至此,自定义的ProgressDialog就已经完成了,可以改变自己的loading图片来达到自己喜欢的加载动画的效果。

     

你可能感兴趣的:(Android学习)