自定义ProgressDialog

来源:互联网 发布:投资连结保险 知乎 编辑:程序博客网 时间:2024/06/01 10:10

1.customprogressdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 >
 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/loadingImageView"
  android:background="@anim/progress_round"
  android:layout_marginRight="15dp" />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/id_tv_loadingmsg"
  android:layout_gravity="center_vertical"
  android:textSize="18sp"
  android:text="数据加载中..." />

</LinearLayout>

 

2.customprogressdialog.xml中用到的动画: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/a"
  android:duration="200" />
 <item
  android:drawable="@drawable/b"
  android:duration="200" />
 <item
  android:drawable="@drawable/c"
  android:duration="200" />
 <item
  android:drawable="@drawable/d"
  android:duration="200" />
 <item
  android:drawable="@drawable/e"
  android:duration="200" />
 <item
  android:drawable="@drawable/f"
  android:duration="200" />
 <item
  android:drawable="@drawable/i"
  android:duration="200" />
 <item
  android:drawable="@drawable/g"
  android:duration="200" />
</animation-list>

 

 

3.自定义CustomProgressDialog

package cn.hjw;

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.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 CustomProgressDialog setTitile(String strTitle) {
  return customProgressDialog;
 }

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

 public CustomProgressDialog setMessage(String strMessage) {
  TextView tvMsg = (TextView) customProgressDialog
    .findViewById(R.id.id_tv_loadingmsg);
  if (tvMsg != null) {
   tvMsg.setText(strMessage);
  }
  return customProgressDialog;
 }
}

 

 

4.在另一个类中应用CustomProgressDialog

MainFrame类

package cn.hjw;

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();

  }

 }

}

 

这样就实现了自定义
原创粉丝点击