Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自定義ProgressDialog示例實現

Android 自定義ProgressDialog示例實現

編輯:關於Android編程

閒來無事,總結了兩個自定義的ProgressDialog,大家可以參考下,根據自己需要進行選擇修改:

實現效果:

示例1:

\

示例2:

\

 

 

代碼如下:

\

MainActivity:只是兩個Button點擊事件

 

package com.customwaitdialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private Button btn_customDialog1;
	private Button btn_customDialog2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
	}

	private void initView() {
		btn_customDialog1 = (Button) findViewById(R.id.btn_customDialog1);
		btn_customDialog2 = (Button) findViewById(R.id.btn_customDialog2);

		btn_customDialog1.setOnClickListener(this);
		btn_customDialog2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 示例1
		case R.id.btn_customDialog1:
			Intent intent1 = new Intent(this, DialogActivity1.class);
			startActivity(intent1);
			break;
		// 示例2
		case R.id.btn_customDialog2:
			Intent intent2 = new Intent(this, DialogActivity2.class);
			startActivity(intent2);
			break;

		default:
			break;
		}
	}
}

示例1DialogActivity1:

 

 

package com.customwaitdialog;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import com.dialogutil.CustomWaitDialog1;

public class DialogActivity1 extends Activity {
	private MainFrameTask mMainFrameTask = null;
	private CustomWaitDialog1 progressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity1);

		mMainFrameTask = new MainFrameTask(this);
		mMainFrameTask.execute();
	}

	@Override
	protected void onDestroy() {
		stopProgressDialog();

		if (mMainFrameTask != null && !mMainFrameTask.isCancelled()) {
			mMainFrameTask.cancel(true);
		}

		super.onDestroy();
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomWaitDialog1.createDialog(this);
			progressDialog.setMessage(正在加載中...);
		}

		progressDialog.show();
	}

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

	public class MainFrameTask extends AsyncTask {
		private DialogActivity1 mainFrame = null;

		public MainFrameTask(DialogActivity1 mainFrame) {
			this.mainFrame = mainFrame;
		}

		@Override
		protected void onCancelled() {
			stopProgressDialog();
			super.onCancelled();
		}

		@Override
		protected Integer doInBackground(Integer... params) {

			try {
				Thread.sleep(10 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			startProgressDialog();
		}

		@Override
		protected void onPostExecute(Integer result) {
			stopProgressDialog();
		}

	}
}

 

重點來了CustomWaitDialog1:

 

package com.dialogutil;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.customwaitdialog.R;

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

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

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

	public static CustomWaitDialog1 createDialog(Context context) {
		customProgressDialog = new CustomWaitDialog1(context,
				R.style.CustomProgressDialog);
		customProgressDialog.setCanceledOnTouchOutside(false);
		customProgressDialog.setContentView(R.layout.customprogressdialog);
		customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

		return customProgressDialog;
	}

	public void onWindowFocusChanged(boolean hasFocus) {

		if (customProgressDialog == null) {
			return;
		}

		ImageView imageView = (ImageView) customProgressDialog
				.findViewById(R.id.loadingImageView);
		AnimationDrawable animationDrawable = (AnimationDrawable) imageView
				.getBackground();
		animationDrawable.start();
	}
	
	public CustomWaitDialog1 setTitile(String strTitle){  
        return customProgressDialog;  
    }  
      
	public CustomWaitDialog1 setMessage(String strMessage){  
        TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);  
          
        if (tvMsg != null){  
            tvMsg.setText(strMessage);  
        }  
          
        return customProgressDialog;  
    }  
}

 

style:



    

    


 

 

布局customprogressdialog.xml:

 



    
    


旋轉動畫:progress_round.xml:

 

 



    
    
    
    
    
    
    
    

 

示例2DialogActivity2:

 

package com.customwaitdialog;


import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;


import com.dialogutil.CustomWaitDialog2;


public class DialogActivity2 extends Activity {
	private CustomWaitDialog2 waitDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity2);
		
		 waitDialog = new CustomWaitDialog2(this);
		 waitDialog.show();
		 waitDialog.setOnDismissListener(new OnDismissListener() {
		
		 @Override
		 public void onDismiss(DialogInterface dialog) {
		 }
		 });
	}
}

等待Dialog:CustomWaitDialog2:

 

 

package com.dialogutil;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.customwaitdialog.R;

public class CustomWaitDialog2 {
	private Context mContext;
	private ImageView image;
	private ImageView loading_pic_bigView;
	private Dialog waitDialog;
	private Animation mAnimation;

	public CustomWaitDialog2(Context mContext) {
		this.mContext = mContext;
		waitDialog = new Dialog(mContext, R.style.common_dialog);
		waitDialog.setContentView(R.layout.loading);
		waitDialog.setCanceledOnTouchOutside(false);

		/**
		 * 設置幕布,也就是本dialog的背景層 dimAmount在0.0f和1.0f之間,0.0f完全不暗,即背景是可見的
		 * ,1.0f時候,背景全部變黑暗。
		 * 
		 * 如果要達到背景全部變暗的效果,需要設置
		 * dialog.getWindow().addFlags(WindowManager.LayoutParams
		 * .FLAG_DIM_BEHIND); ,否則,背景無效果。
		 */
		Window window = waitDialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();

		lp.dimAmount = 0.8f;
		window.setAttributes(lp);
		window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

		// waitDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		/**
		 * 設置透明度,主要設置的是dialog自身的透明度
		 */
		loading_pic_bigView = (ImageView) waitDialog
				.findViewById(R.id.loading_pic_bigView);
		loading_pic_bigView.setAlpha(0.6f);

		image = (ImageView) waitDialog.findViewById(R.id.loading_pic_big);
		mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.loading);
	}

	public void show() {
		image.startAnimation(mAnimation);
		waitDialog.show();
	}

	public void dismiss() {
		waitDialog.dismiss();
	}
	
	//用於網絡請求中斷操作
	public void setOnDismissListener(
			DialogInterface.OnDismissListener dismissListener) {
		waitDialog.setOnDismissListener(dismissListener);
	}
}

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved