Android 自定义progressDialog实现

  我们在项目中经常会遇到这样一个应用场景:执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在android中让大家最 容易想到的就是progressbar或者progressDialog,区别在于前者是一个控件,后者是对话框。由于一些需求在弹出进度条时不希望用户 能够操作其他控件,所以只能使用progressDialog,这个时候有遇到了一个问题,我不想要progressDialog的黑色框框,感觉这样跟 应用的整体风格不协调,这个时候就考虑了写一个自定义的progressDialog。
         在网上搜过很多自定义progressDialog的例子,对着写了下,但是没有任何效果,不知道是自己使用的方法不对还是什么地方出错了。通过不断的查找资料,写了一个简单的自定义progressDialog。先上图看下效果:

 

Android 自定义progressDialog实现

 

 

1.String.xml 文件,progressDialog是继承与Dialog,先设置一下progressDialog的风格,设置背景透明色。

 <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>


 

 

 

2.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>


 

 

 

3.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/load_01"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_02"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_03"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_04"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_05"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_06"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_07"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_08"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_09"
        android:duration="100"/>
    <item
        android:drawable="@drawable/load_10"
        android:duration="100"/>

</animation-list>


 

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

import com.jusyo.R;

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;

public class CustomProgressDialog extends Dialog {

	private Context context = null;

	private static CustomProgressDialog customProgressDialog = null;

	public CustomProgressDialog(Context context) {
		super(context);

		this.context = context;

	}

	public CustomProgressDialog(Context context, int theme) {

		super(context, theme);

	}

	public static CustomProgressDialog createDialog(Context context) {

		customProgressDialog = new CustomProgressDialog(context,
				R.style.CustomProgressDialog);
		customProgressDialog.setContentView(R.layout.load_dialog);
		customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		customProgressDialog.setCancelable(false);
		customProgressDialog.setMessage("正在加载中...");
		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();
	}

	/**
	 * 
	 * [Summary] setTitile 标题
	 * 
	 * @param strTitle
	 * @return
	 * 
	 */
	public CustomProgressDialog setTitile(String strTitle) {
		return customProgressDialog;

	}

	/** 
	 * setMessage 提示内容
	 * 
	 * @param strMessage
	 * 
	 * @return
	 * 
	 * 
	 */

	public CustomProgressDialog setMessage(String strMessage) {

		TextView tvMsg = (TextView) customProgressDialog
				.findViewById(R.id.id_tv_loadingmsg);

		if (tvMsg != null) {

			tvMsg.setText(strMessage);

		}

		return customProgressDialog;

	}
}


 

5.接下来就是写一个测试activity调用我们的progressDialog了。

 

   

 import com.lxd.widgets.CustomProgressDialog;  

    

 import android.app.Activity;  

 import android.os.AsyncTask;  

 import android.os.Bundle;  

 import android.view.Window;  
 import android.view.WindowManager;  

    
 public class MainFrame extends Activity {  
    private MainFrameTask mMainFrameTask = null;  
    private CustomProgressDialog progressDialog = null;  

       

    @Override 

     public void onCreate(Bundle savedInstanceState) {  

         super.onCreate(savedInstanceState);  

         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  

         WindowManager.LayoutParams.FLAG_FULLSCREEN);  

            

         setContentView(R.layout.main);  

            

         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 = CustomProgressDialog.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 MainFrame mainFrame = null;  
         public MainFrameTask(MainFrame 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();  
        }  
     }  
 } 


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值