展示效果如图。
整体思路是,布局中放一张加载图,实现该图片绕自身中心点循环做自转动画。
1.动画效果的实现, 在anim文件下创建loadingrotate.xml,
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0" //从0度开始
android:toDegrees="360" //旋转到360度
android:pivotX="50%" //x中心点
android:pivotY="50%"
android:duration="1500" //旋转一周所需时间
android:repeatMode="restart" //重复模式
android:repeatCount="-1" //-1代表无限循环
/>
</set>
2.新建一类 继承DialogFragment
实现oncreateView方法,声明view。
loadingIv = (ImageView) getView().findViewById(R.id.iv_loading);
java代码加载动画,实现匀速旋转。
Animation animation = AnimationUtils.loadAnimation(getActivity(),R.anim.loadingrotate);
LinearInterpolator lInterpolator = new LinearInterpolator();
animation.setInterpolator(lInterpolator);
loadingIv.setAnimation(animation);
3.在onStart方法中实现设置dialog大小,背景色透明效果。
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
dialog.getWindow().setLayout((int) (dm.widthPixels*0.3), (int) (dm.widthPixels*0.3));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.dimAmount = 0f;
dialog.getWindow().setAttributes(params);