1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public
class
CustomLoadingDialog
extends
Dialog {
@ViewInject
(R.id.custom_loading_dialog_indicator)
private
ImageView indicatorIv;
@ViewInject
(R.id.custom_loading_dialog_message)
private
TextView messageTv;
private
CustomLoadingDialog(Context context,
int
theme) {
super
(context, theme);
}
public
static
CustomLoadingDialog createLoading(Context context) {
CustomLoadingDialog dialog =
new
CustomLoadingDialog(context, R.style.CustomDialog);
View content = LayoutInflater.from(context).inflate(
R.layout.custom_loading_dialog,
null
);
ViewUtils.inject(dialog, content);
dialog.addContentView(content,
new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return
dialog;
}
@Override
public
void
onWindowFocusChanged(
boolean
hasFocus) {
if
(hasFocus) {
indicatorIv.startAnimation(AnimationUtils.loadAnimation(
getContext(), R.anim.custom_loading_dialog));
}
}
public
void
setMessage(String message) {
if
(!TextUtils.isEmpty(message)) {
messageTv.setVisibility(View.VISIBLE);
messageTv.setText(message);
}
else
{
messageTv.setVisibility(View.GONE);
}
}
}
|
资源文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<style name=
"CustomDialog"
parent=
"@android:style/Theme.Dialog"
>
<item name=
"android:windowFrame"
>
@null
</item>
<item name=
"android:windowBackground"
>
@android
:color/transparent</item>
<!-- 边框 -->
<item name=
"android:windowIsFloating"
>
true
</item>
<!-- 是否浮现在activity之上 -->
<item name=
"android:windowNoTitle"
>
true
</item>
<!-- 无标题 -->
<item name=
"android:windowIsTranslucent"
>
true
</item>
<!-- 半透明 -->
<item name=
"android:backgroundDimEnabled"
>
true
</item>
<!-- 模糊 -->
</style>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
style=
"@style/CustomDialogBackground"
android:minWidth=
"240dp"
android:padding=
"@dimen/Xlarge"
>
<ImageView
android:id=
"@+id/custom_loading_dialog_indicator"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:src=
"@drawable/loading_indicator"
/>
<TextView
android:id=
"@+id/custom_loading_dialog_message"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:visibility=
"gone"
android:layout_marginTop=
"@dimen/medium"
android:singleLine=
"true"
android:maxWidth=
"200dp"
android:textColor=
"@android:color/black"
android:textSize=
"@dimen/text_noraml"
/>
</LinearLayout>
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
android:shareInterpolator=
"false"
>
<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>
|
使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public
void
showLoadingDialog() {
if
(loadingDialog ==
null
) {
loadingDialog = CustomLoadingDialog.createLoading(
this
);
}
loadingDialog.setMessage(getResources().getString(R.string.loading_message_default));
loadingDialog.show();
}
public
void
showLoadingDialog(String message) {
if
(loadingDialog ==
null
) {
loadingDialog = CustomLoadingDialog.createLoading(
this
);
}
if
(!TextUtils.isEmpty(message)) {
loadingDialog.setMessage(message);
}
else
{
loadingDialog.setMessage(getResources().getString(R.string.loading_message_default));
}
loadingDialog.show();
}
public
void
hideLoadingDialog() {
if
(loadingDialog !=
null
&& loadingDialog.isShowing()) {
loadingDialog.dismiss();
}
}
public
void
setLoadingDialogCancel(
boolean
canCancel) {
loadingDialog.setCancelable(canCancel);
}
|