Android 自定义ProgressDialog示例实现

闲来无事,总结了两个自定义的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<Integer, String, Integer> {
		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:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="common_dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsFloating">true</item>
 <!-- 设置未浮动窗口 -->
        <item name="android:windowFrame">@null</item>
 <!-- 设置无边框 -->
        <item name="android:windowNoTitle">true</item>
 <!-- 设置无标题 -->
        <item name="android:windowBackground">@android:color/transparent</item>
 <!-- 设置完全透明 -->
        <item name="android:backgroundDimEnabled">false</item>
 <!-- 设置屏幕变暗 -->
    </style>

    <style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    </style>

    <style name="CustomProgressDialog" parent="@style/CustomDialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>

</resources>


布局customprogressdialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal">
    <ImageView
        android:id="@+id/loadingImageView"
       	android:layout_width="wrap_content"
       	android:layout_height="wrap_content"
       	android:background="@anim/progress_round"/>
    <TextView
        android:id="@+id/id_tv_loadingmsg"
        android:layout_width="wrap_content"
       	android:layout_height="wrap_content"
       	android:layout_gravity="center_vertical"
       	android:textSize="20dp"/>
</LinearLayout>

旋转动画:progress_round.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_1" android:duration="200"/>
    <item android:drawable="@drawable/progress_2" android:duration="200"/>
    <item android:drawable="@drawable/progress_3" android:duration="200"/>
    <item android:drawable="@drawable/progress_4" android:duration="200"/>
    <item android:drawable="@drawable/progress_5" android:duration="200"/>
    <item android:drawable="@drawable/progress_6" android:duration="200"/>
    <item android:drawable="@drawable/progress_7" android:duration="200"/>
    <item android:drawable="@drawable/progress_8" android:duration="200"/>
</animation-list>

示例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);
	}
}

布局loading:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="80.0dip"
    android:layout_height="80.0dip" >

    <ImageView
        android:id="@+id/loading_pic_bigView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/loading_gray" />

    <ImageView
        android:id="@+id/loading_pic_big"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/operating" />

</FrameLayout>


动画loading:

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate 
        android:duration="2000" 
        android:fromDegrees="0.0" 
        android:toDegrees="359.0" 
        android:pivotX="50.0%" 
        android:pivotY="50.0%" 
        android:repeatCount="infinite" />
</set>

参考下源码:

点击下载源码




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。