android_自定义Loading框

代码
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);
        }
    }

}

资源文件

<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>

使用
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);
	}


你可能感兴趣的:(android_自定义Loading框)