加载中的菊花,背景透明,不会有阴影遮盖层
class LoadingDialog(context: Context) : Dialog(context, R.style.LoadingDialogStyle) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setCancelable(false)
setContentView(layoutInflater.inflate(R.layout.dialog_loading_layout, null))
window!!.run {
val attr = attributes
//如果设置背景透明无效,可以加上这行代码
attr.format = PixelFormat.TRANSPARENT
//设置背景透明
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
attributes = attr
}
}
}
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) 是设置Dialog的背景为透明。不设置这个会有黑色背景和阴影,就是下图这样:
样式
<style name="LoadingDialogStyle" parent="android:style/Theme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">false</item>
</style>
android:backgroundDimEnabled 是设置整个屏幕是否变暗,设置false就不会变暗,效果如下:
布局
dialog_loading_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="38dp"
android:layout_height="38dp"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/anim"/>
</LinearLayout>
Drawable
drawable/anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/load"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="360.0" />
使用 animated-rotate 和 rotate 的效果是不一样的,animated-rotate效果更真实。rotate 的效果就是一张图片在旋转,比较蠢的效果。