效果预览
简要说明
现在android程序网络请求操作是必不可少的,然而拥有好的交互体验的程序对网络耗时操作的处理尤为重要。
代码说明:
dialog_loading.xml
这个布局就是我们自定义的显示布局,比较简单明了,最外层一个垂直排列的线性布局,里面依次是一个imageview和textview。
loading_animation.xml
这个就是我们设置的旋转的属性动画的基本属性操作,这个xml存在于res下的anim文件夹下(手动创建文件夹)
CustomProgressDialog.class
package com.cc.customprogressdialog.util;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.cc.customprogressdialog.R;
public class CustomProgressDialog extends Dialog {
Context context;
private ImageView spaceshipImage;
private Animation hyperspaceJumpAnimation;
public CustomProgressDialog(Context context) {
super(context);
this.context = context;
}
public CustomProgressDialog(Context context, int theme) {
super(context, theme);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
// main.xml中的ImageView
spaceshipImage = (ImageView) v.findViewById(R.id.img);
// 加载动画
hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
// 使用ImageView显示动画
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
setCancelable(false);// 不可以用“返回键”取消
setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
}
}
这个类就是自定义的ProgressDialog,代码的关键步骤我都写了注释。
使用
package com.cc.customprogressdialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.cc.customprogressdialog.util.CustomProgressDialog;
public class MainActivity extends AppCompatActivity {
private Button btn;
private CustomProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View view) {
new AsyncTask() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new CustomProgressDialog(MainActivity.this, R.style.loading_dialog);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
SystemClock.sleep(2000);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mProgressDialog.dismiss();
}
}.execute();
}
});
}
}
上述代码我们看到我在主activity里面添加一个按钮,实现其点击事件,在点击事件中我创建了一个异步操作,模拟网络耗时。
注意一点我在创建CustomProgressDialog的时候传入了一个style,系统默认的不给力,所以只能自己写了一个。
- @null
- true
- #00000000
- @android:color/transparent
- true
- @null
属性的参数意思有兴趣的自行百度,在这里不一一介绍了。
实现的代码就这么简单但很实用,希望对各位读者有所帮助。最后附上完整的代码:
http://xiazai.jb51.net/201702/yuanma/CustomProgressDialog
以上所述是小编给大家介绍的Android实现网络加载时的对话框功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!



