Android——加载中的dialog

来源:互联网 发布:锐捷网络 编辑:程序博客网 时间:2024/06/04 18:33

1、自定义dialog

       java:

public class CustomProgressBar extends Dialog {    private Context context ;    private String progressText ;    public CustomProgressBar(Context context) {        super(context , R.style.dialog_theme) ;        this.context = context ;    }    public CustomProgressBar(Context context, String progressText) {        super(context, R.style.dialog_theme) ;        this.context = context ;        this.progressText = progressText ;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.custom_progressbar) ;        TextView title =  (TextView) findViewById(R.id.custom_imageview_progress_title);        title.setText(progressText == null ? "加载数据中,请稍后..." : progressText) ;    }    /**     * @see android.app.Dialog#show()     */    @Override    public void show() {        try{            if(!isShowing()){                super.show();            }        }catch (Exception e){            e.printStackTrace();        }        ImageView im =  (ImageView) findViewById(R.id.custom_imageview_progress_bar);        im.startAnimation(AnimationUtils.loadAnimation(context, R.anim.round_loading));    }}
   xml:custom_progressbar.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:padding="15dip"              android:gravity="center"              android:background="@drawable/shen"              android:orientation="vertical" >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/custom_imageview_progress_bar"        android:src="@drawable/diaog" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="5dip"        android:id="@+id/custom_imageview_progress_title"        android:textColor="@android:color/white"        android:textSize="12sp"        /></LinearLayout>
2、在res文件夹下新建一个动画round_londing.xml
  
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"        android:duration="1000"        android:fromDegrees="0"        android:interpolator="@android:anim/linear_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:repeatCount="infinite"        android:toDegrees="+360"></rotate>
3、在style里加入
   
<style name="dialog_theme">    <item name="android:windowFrame">@null</item>    <item name="android:windowContentOverlay">@null</item>    <item name="android:windowIsFloating">true</item>    <item name="android:windowIsTranslucent">true</item>    <item name="android:windowNoTitle">true</item>    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>    <item name="android:windowBackground">@color/transparent</item>    <!-- 背景透明 -->    <!-- 激活背景变暗遮罩 -->    <item name="android:backgroundDimEnabled">true</item>    <item name="android:backgroundDimAmount">0.6</item></style>

4、在要调用dialog的java中
CustomProgressBar cd;
//dialog开始调用此方法public void createProgressBar() {    this.createProgressBar(null);}//dialog中public void createProgressBar(String text) {    if (cd== null) {        if (text == null) {            cd= new CustomProgressBar(this);        } else {            cd= new CustomProgressBar(this, text);        }        cd.setCancelable(true);    }    if (!cd.isShowing()) {        cd.show();    }}//dialog结束调用此方法public void disMissProgress() {    if (cd!= null && cd.isShowing()) {        cd.dismiss();    }}

5、这样就可以了测试一下吧,图片我就不贴了,自己找两张就ok;

2 0