android 加载数据或提交数据时显示转圈的提示页面

android 加载数据或提交数据时显示转圈的提示页面android

       提早声明一下,本博客全是本身的理解,若是内容中有理解错误的地方,欢迎指正。另外,博客内容有参考其余博客,本博客只用来学习。ide

       当咱们进入到一个页面时,一般先会出现一个转圈的dialog,这是由于这个页面须要加载数据,为了防止数据加载完成前空白的页面,一般会先显示转圈的dialog,直到数据加载完成,圈消失。那么,这个转圈的dialog是怎么实现的呢?函数

      首先,先写 显示转圈的layout:progress_hud.xml
        
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/progress_hud_bg"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="20dp" >
 
    <ImageView
        android:id="@+id/spinnerImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/spinner" />
 
    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Message"
        android:textColor="#FFFFFF" />
 
</LinearLayout>学习

         图片的背景是动画spinner,具体实现以下:
         
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
 
    <item
        android:drawable="@drawable/spinner_0"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_1"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_2"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_3"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_4"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_5"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_6"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_7"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_8"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_9"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_10"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_11"
        android:duration="60"/>
</animation-list>
          这里提一下安卓的动画。android支持3种动画模式,tween animation,frame animation和property animation,这三种动画模式在SDK中被称为view animation,drawable animation和property animation。
       tween animation(view animation):补间动画。
       frame animation(view animation):逐帧动画。
       property animation:属性动画,这个是在Android 3.0中才引进的。
      
        这里咱们只介绍一下用到的逐帧动画。就像GIF图片,经过设置一系列图片(Drawable)依次显示来模拟动画的效果。必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。上面的xml文件中:android:oneshot若是为true,则动画只播放一遍,false,则一直播放,直到设置它中止。android:duration为每张图片播放时间。
        
        接下来就是咱们的ProgressHUD类。
1. 首先是两个构造函数:
<span style="white-space:pre">    </span>public ProgressHUD(Context context) {
        super(context);
    }
 
    public ProgressHUD(Context context, int theme) {
        super(context, theme);
    }
        int theme:设置progressHUD的style,一下子讲。
   2. 而后是onWindowFocusChanged方法。
<span style="white-space:pre">    </span>public void onWindowFocusChanged(boolean hasFocus){
        ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
        <span style="white-space:pre">    </span>AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
        <span style="white-space:pre">    </span>spinner.start();
    onWindowFocusChanged()方法是在和用户交互时调用的,activity获取到焦点或者失去焦点时都会调用,它在activity的生命周期中是这样的:
            进入时:onStart---->onResume---->onWindowFocusChanged(true)
           退出时:onPause---->onStop---->onWindowFocusChanged(false) 
          可见,只有在用户真正获取到屏幕焦点后会调用。因此,在onWindowFocusChanged方法中去启动动画spinner。
       3. setMessage方法用来设置 转圈时,显示的文字,如“正在加载数据。。。”等。  
<span style="white-space:pre">    </span>public void setMessage(CharSequence message) {
        if(message != null && message.length() > 0) {
            findViewById(R.id.message).setVisibility(View.VISIBLE);            
            TextView txt = (TextView)findViewById(R.id.message);
            txt.setText(message);
            txt.invalidate();
        }
invalidate()方法的做用时请求重绘view树。事实上,在setVisibility()由可见变为不可见或者由不可见变为可见状态时,会自动调用invalidate()方法,我也不知道为何此处又调用一次,我试了一下林注释掉这行代码,也彻底能够正常运行。
4. 最后是 show()方法。
<span style="white-space:pre">    </span>public static ProgressHUD show(Context context, CharSequence message, boolean indeterminate, boolean cancelable,
            OnCancelListener cancelListener) {
        ProgressHUD dialog = new ProgressHUD(context,R.style.ProgressHUD);
        dialog.setTitle("");
        dialog.setContentView(R.layout.progress_hud);
        if(message == null || message.length() == 0) {
            dialog.findViewById(R.id.message).setVisibility(View.GONE);            
        } else {
            TextView txt = (TextView)dialog.findViewById(R.id.message);
            txt.setText(message);
        }
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.getWindow().getAttributes().gravity=Gravity.CENTER;//设置dialog显示在屏幕中央
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
        lp.dimAmount=0.2f; //设置控件的黑暗度,值为0-1。0.0f彻底不暗,1.0f彻底黑暗。也能够设置控件的透明度,如:<span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 16px; line-height: 24px; background-color: rgb(230, 232, 231);">lp.alpha=1.0f;</span>
        dialog.getWindow().setAttributes(lp); 
        //dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialog.show();
        return dialog;
    }    
ProgressHUD类已完毕,咱们再说一下R.style.ProgressHUD。
styles.xml:
       <style name="ProgressHUD" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
   </style>
大体说一下。windowFrame:Dialog的windowFrame框为无
windowIsFloating:是否浮如今activity之上
windowContentOverlay:设置窗体内容背景
windowAnimationStyle:设置动画
windowSoftInputMode:
活动的主窗口如何与包含屏幕上的软键盘窗口交互。这个属性的设置将会影响两件事情:动画

1>     软键盘的状态——是否它是隐藏或显示——当活动(Activity)成为用户关注的焦点。this

2>     活动的主窗口调整——否减小活动主窗口大小以便腾出空间放软键盘或是否当活动窗口的部分被软键盘覆盖时它的内容的当前焦点是可见的。spa

"stateUnspecified" 软键盘的状态(是否它是隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。这个是为了软件盘行为默认的设置。
"adjustPan" 该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能老是看到输入内容的部分。这个一般是不指望比调整大小,由于用户可能关闭软键盘以便得到与被覆盖内容的交互操做。
windowBackground:设置dialog的背景
windowNoTitle:是否显示title.net

至此,自定义的progressdialog实现已经完成,最后就是调用它的activity了,以下:
public class MainActivity extends ActionBarActivity {
    
    private Context context;
    public ProgressHUD mProgressHUD;
    
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
    }
 
    public void loadDataStyle1(View view){
        showMyDialog(true);
    }
    
    public void showMyDialog( boolean isCancelable) {
    mProgressHUD = ProgressHUD.show(context, "加载中", true, isCancelable, null);}
activity_main.xm:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.loaddatastyle.MainActivity" >
 
    <Button
        android:id="@+id/style1Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="45dp"
        android:onClick="loadDataStyle1"
        android:text="点击按钮,开始加载数据" />
 
 
</RelativeLayout>
 
源代码地址:http://download.csdn.net/detail/u010127250/9023275xml


————————————————
版权声明:本文为CSDN博主「imutlxy」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处连接及本声明。
原文连接:https://blog.csdn.net/u010127250/article/details/47776789blog

 

 

https://blog.csdn.net/u010127250/article/details/47776789

相关文章
相关标签/搜索