自定义ProgressDialog

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

  }

 }

}

 

这样就实现了自定义
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值