Android自定义类似ProgressDialog效果的Dialog
2016-07-20 11:20
411 查看
Android自定义类似ProgressDialog效果的Dialog.
1.首先准备一张自己要定义成哪样子的效果的图片,背景可以设为透明的(这里我们自定义主题)。效果:
定义loading.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView):
<ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading_data" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" />
定义一个loadingDialog中imageView转动的动画:loading_animation.xml
<?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>
自定义主题:dialog的style.
<!-- 自定义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">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
Dialog的代码,你可以自己封装成一个方法。
/** * 得到自定义的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, 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.rotate); // 使用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.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));// 设置布局 return loadingDialog; }
效果:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories