Android自定义类似ProgressDialog效果的Dialog
2016-07-27 15:47
465 查看
Android自定义类似ProgressDialog效果的Dialog.
方法如下:
1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景)。
如我要的效果:
2.定义loading_dialog.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView):
[html] view plaincopy
<?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/publicloading"
/>
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="数据加载中……" />
</LinearLayout>
3.定义一个loadingDialog中imageView转动的动画:loading_animation.xml
[html] view plaincopy
<?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>
4.定义dialog的style.
[java] view plaincopy
<!-- 自定义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>
5.写点创建Dialog的代码,你可以自己封装成一个方法。
[java] view plaincopy
/**
* 得到自定义的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.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
return loadingDialog;
}
最后来张整体的效果图:
方法如下:
1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景)。
如我要的效果:
2.定义loading_dialog.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView):
[html] view plaincopy
<?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/publicloading"
/>
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="数据加载中……" />
</LinearLayout>
3.定义一个loadingDialog中imageView转动的动画:loading_animation.xml
[html] view plaincopy
<?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>
4.定义dialog的style.
[java] view plaincopy
<!-- 自定义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>
5.写点创建Dialog的代码,你可以自己封装成一个方法。
[java] view plaincopy
/**
* 得到自定义的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.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
return loadingDialog;
}
最后来张整体的效果图:
相关文章推荐
- Android二维码扫码--精简版zxing使用教程
- Android无障碍设计简介
- Android之百度云推送(一)如何导入手机apk
- Android自定义属性:format选项之reference
- Android开源工具项目集合
- Rxandroid与MVP的结合(demo)
- Android 创建目录
- Android PendingIntent的理解
- Android PendingIntent的理解
- Android PendingIntent的理解
- Android PendingIntent的理解
- RxJava两步打造华丽的Android引导页
- 【Android】使用Gradle打包时,获取svn的版本号,删除unalign.apk文件,获得版本号,并设定在打包的文件名称中
- android 文件存储
- 浅谈Android中MVP设计模式
- Android提醒Dialog、Toast和Snackbar ---站在巨人的肩膀上学习总结
- Android Chart
- 【Android】使用Jenkins+Gradle+FTP,实现自动打包,自动上传文件至FTP
- Android Listview滑动及渐变动画
- android adb启动失败问题