自定义progressdialog

首先准备资源图片(自己做的图片比较丑):

1《自定义progressdialog》

2《自定义progressdialog》

3《自定义progressdialog》

4《自定义progressdialog》

一共四幅图片。

1 需要为Dialog定义一个布局文件,我这里定义一个waitting.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center">   
<TextView 
     android:id="@+id/waiting"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"    
     android:layout_centerHorizontal="true" 
     android:text="数据加载中..."
     android:textSize="18sp"
     />
 <ImageView
     android:id="@+id/waiting_iv"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/waiting"
     android:layout_centerHorizontal="true" 
      /></RelativeLayout>

2 dialog里面有个逐帧动画,定义一个animation-lis放于drawable文件夹

 <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false">
    <!-- oneshot false表示循环 -->
    <item android:drawable="@drawable/bg11" android:duration="400"/>
 <item android:drawable="@drawable/bg12" android:duration="400"/>
 <item android:drawable="@drawable/bg13" android:duration="400"/>
 <item android:drawable="@drawable/bg14" android:duration="400"/>
</animation-list>

3 自定义的dialog控件

package com.example.dialogwindows;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class mydialog extends Dialog{
 private TextView tv;  
    private ImageView iv;  
    private AnimationDrawable mAnimationDrawable; 
    public mydialog(Context context, int theme) {  
        super(context, theme);  
        // TODO Auto-generated constructor stub  
    }  
  protected mydialog(Context context, boolean cancelable,  
             OnCancelListener cancelListener) {  
         super(context, cancelable, cancelListener);  
         // TODO Auto-generated constructor stub  
     }   
  public mydialog(Context context) {  
         super(context);  
         // TODO Auto-generated constructor stub  
     }  
       
     @Override  
     protected void onCreate(Bundle savedInstanceState) {  
         // TODO Auto-generated method stub  
         super.onCreate(savedInstanceState);  
           
         View view = LayoutInflater.from(getContext()).inflate(R.layout.waitting, null);  
         tv = (TextView) view.findViewById(R.id.waiting);  
         iv = (ImageView) view.findViewById(R.id.waiting_iv);           
         iv.setImageResource(R.drawable.dialogbg);                    
         setContentView(view);  
     }  
       
     @Override  
     public void show() {  
         // TODO Auto-generated method stub  
         super.show();  
         mAnimationDrawable = (AnimationDrawable) iv.getDrawable();  
         mAnimationDrawable.start();  
     }  
       
     @Override  
     public void dismiss() {  
         // TODO Auto-generated method stub  
         super.dismiss();  
         mAnimationDrawable = (AnimationDrawable) iv.getDrawable();  
         mAnimationDrawable.stop();  
     }  
       
     public void setMsg(String txt) {  
         tv.setText(txt);  
     }  
     
 }

4 为dialog设置外观样式,有没有title啊,边框啊,什么的,不会写就参考Themes.xml这个文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  -->
    <style name="WaitingDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

5 在Activity调用,注意一下 传的是 Activity.this,如果传getBaseContext(),或者getApplicationContext()的话会报windowstoken异常

     private void mydialogTest() {
     Dialog mDialog = new mydialog(this, R.style.WaitingDialog);
     mDialog.show();
    }

 

    原文作者:移动开发
    原文地址: https://my.oschina.net/tomcater/blog/347181
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞