Android 自定义旋转进度框(单帧)

来源:互联网 发布:matlab数组写入excel 编辑:程序博客网 时间:2024/06/14 10:20

1.对话框布局文件

<?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:orientation="vertical" ><ImageView     android:id="@+id/loadingDialog_bg"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/loding_dialog"/></LinearLayout>

 

2.工具类创建对话框

public class LoadingDialog{public static Dialog createLoadingDialog(Context context){LayoutInflater mInflater = LayoutInflater.from(context);LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.loading_dialog, null);ImageView image = (ImageView) layout.findViewById(R.id.loadingDialog_bg);//创建动画Animation animation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);LinearInterpolator interpolator = new LinearInterpolator(); //匀速旋转animation.setInterpolator(interpolator);animation.setDuration(2000); //一次动画耗时2000msanimation.setRepeatCount(-1); //重复播放动画//显示动画image.startAnimation(animation);//创建对话框Dialog loadingDialog = new Dialog(context,R.style.dialog);loadingDialog.setContentView(layout);return loadingDialog;}}

 

说明:创建旋转动画时,将动画结束时的角度设置为359度,以防止动画停顿现象

 

3.对话框样式

<style name="dialog" parent="@android:style/Theme.Dialog">   <item name="android:windowFrame">@null</item><!--边框-->   <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->   <item name="android:windowIsTranslucent">false</item><!--半透明-->   <item name="android:windowNoTitle">true</item><!--无标题-->   <item name="android:windowBackground">@color/transparent</item><!--背景透明-->   <item name="android:backgroundDimEnabled">false</item><!--模糊--></style>


4.使用对话框

此时当需要使用对话框时,只需通过工具类实例化一个对话框即可:

Dialog dialog = LoadingDialog.createLoadingDialog(LCCXActivity.this);dialog.show();