自定义ProgressDialog

最近工作中需要用到progressDialog,可是系统自带的黑色progressDialog又是其丑无比,无奈只能自己自定义了,在网上查看别人的例子,并自己整理了一份Demo:

先上图:

MyProgressDialog:

package com.example.myprogressdialog_zzw;

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

/**
 * @author 鹭岛猥琐男
 *
 */
public class MyProgressDialog extends Dialog
{
    private Context context;
    private static MyProgressDialog myProgressDialog = null;

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

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

public static MyProgressDialog createDialog(Context context)
    {
        myProgressDialog = new MyProgressDialog(context,
                R.style.myprogressDialog);
        myProgressDialog.setContentView(R.layout.dialog_layout);
        myProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        return myProgressDialog;

}

/*
     * (non-Javadoc)
     *
     * @see android.app.Dialog#onWindowFocusChanged(boolean) 设置动画
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        ImageView image_loadingimage = (ImageView) myProgressDialog
                .findViewById(R.id.image_loadingimage);
        AnimationDrawable animation = (AnimationDrawable) image_loadingimage
                .getBackground();
        animation.start();
    }

public MyProgressDialog setTitle(String strTitle)
    {
        return myProgressDialog;
    }

/**
     * @param strMessage
     * @return 设置progressDialog的消息内容
     */
    public MyProgressDialog setMessage(String strMessage)
    {
        TextView tv_loadingmsg = (TextView) myProgressDialog
                .findViewById(R.id.tv_loadingmsg);
        if (tv_loadingmsg != null)
        {
            tv_loadingmsg.setText(strMessage);
        }
        return myProgressDialog;
    }
}

在MainActivity中对MyProgressDialog进行调用,为了模仿网络访问结束后,关闭ProgressDialog的过程,采用了线程的sleep,运行5秒后关闭ProgressDialog,上代码:

package com.example.myprogressdialog_zzw;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * @author 鹭岛猥琐男
 * 采用线程的的sleep模拟下载结束后关闭progressDialog
 *
 */
public class MainActivity extends Activity
{
    MyProgressDialog myProgressDialog = null;
    Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            if (msg.what == 1)
            {
                Log.e("接收到消息", "" + msg.what);
                if (myProgressDialog != null)
                {
                    myProgressDialog.dismiss();
                    myProgressDialog = null;
                }
                Toast.makeText(MainActivity.this, "加载完成!", Toast.LENGTH_SHORT)
                        .show();
            }
        };
    };

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_go = (Button) findViewById(R.id.button1);
        btn_go.setOnClickListener(new OnClickListener()
        {

@Override
            public void onClick(View v)
            {
                if (myProgressDialog == null)
                {
                    myProgressDialog = MyProgressDialog
                            .createDialog(MainActivity.this);
                    myProgressDialog.setMessage("努力加载中...");

}
                myProgressDialog.show();
                new Thread()
                {
                    @Override
                    public void run()
                    {
                        Log.e("线程", "进入线程!");
                        try
                        {
                            Thread.sleep(5000);
                            Message msg = new Message();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }
                        catch (InterruptedException e)
                        {
                            Log.e("异常", "失败!异常");
                        }

}
                }.start();

}
        });
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

下载地址:http://download.csdn.net/detail/zzw0221/7609051

自定义ProgressDialog,布布扣,bubuko.com

时间: 2024-10-24 19:52:43

自定义ProgressDialog的相关文章

自定义progressdialog,改善用户体验

自定义progressdialog,改善用户体验

android自定义progressdialog

http://blog.sina.com.cn/s/blog_3e333c4a010133ze.html 1.建立myprogress.xml在 res/anim中 <?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res

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

自定义ProgressDialog加载图片

使用系统加载框 mDialog = new ProgressDialog(this); mDialog.setCancelable(true);//是否可以被取消 mDialog.setMessage("loading...");//加载显示的信息 mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//圆环风格 mDialog.show(); 更换系统加载图片 mDialog = new ProgressDialog(this

自定义progressdialog加载动画,这里还有旋转的加载条,美团,多个图片动画

下载Demo:http://download.csdn.net/detail/menglele1314/8775497 public class MainActivity extends Activity { private Button submit; private AnimationDrawable fightnimation, fightnimationab; private ImageView pb, net, netab; private Dialog mLoading; priva

Android 自定义progressDialog实现

先上图看下效果: 1.String.xml 文件,progressDialog是继承与Dialog,先设置一下progressDialog的风格,设置背景透明色. <style name="CustomDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name=

自定义ProgressDialog实现暂时隐藏进度值并显示等待状态(附源码下载)

有时,我们需要访问网络才能获取到需要操作的任务数(例如下载的文件数),而在服务器返回任务数之前要想隐藏进度百分比和进度数值,就需要我们自己重写ProgressDialog.等到获取到任务数后再把进度值和百分比显示出来.先上效果图: 关键代码: public class CustomProgressDialog extends ProgressDialog { private final String TAG = this.getClass().getSimpleName(); public Cu

Android 自定义progressDialog

1. 导入CustomProgressDialog.java 2. styles.xml: <style name="CustomProgressDialog" parent="@android :style/Theme.Dialog">         <item name="android:windowBackground">@android :color/transparent</item>       

Android自定义类似ProgressDialog效果的Dialog

Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loading_dialog.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView): [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <LinearLayout