进度条的实现原理其实很简单,就是设置一个动画,使图片在对话框中旋转。
private Dialog progressDialog;
Button buttonProgressDialog = (Button) findViewById(R.id.button3);
buttonProgressDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = DialogUtil.createLoadingDialog(MainActivity.this, "正在加载中...");
progressDialog.show();
// 这里你可以进行一些等待时的操作,我这里用8秒后显示Toast代理等待操作
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
}
}, 8000);
}
});
/** * 得到自定义的progressDialog * * @param context * @param msg * @return */
public static Dialog createLoadingDialog(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
// main.xml中的ImageView
ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
// 加载动画
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
context, R.anim.load_animation);
// 使用ImageView显示动画
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
tipTextView.setText(msg);// 设置加载信息
Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
loadingDialog.setCancelable(false);// 不可以用“返回键”取消
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
return loadingDialog;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_view" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:minHeight="60dp" android:minWidth="180dp" android:gravity="center" android:padding="10dp" android:background="@drawable/loading_bg">
<ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading" />
<TextView android:id="@+id/tipTextView" android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="数据加载中……" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="+360" android:duration="1500" android:startOffset="-1" android:repeatMode="restart" android:repeatCount="-1" />
</set>
此文件放在res文件夹下的anim文件夹中
<!-- 自定义loading dialog -->
<style name="loading_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/loading_bg</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
此文件放在values文件夹的style.xml中
这样效果基本上就做成了!下篇文章中完成第四个按钮“多选框”!