Android實現加載圈

開發過程中經常用到加載圈,特別是車機開發由於外設不同很多操作響應的等待時長經常要用到不同的加載圈。

首先,直接上菊花效果圖,這是我直接從項目裡面截取下來的。

核心代碼

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
 
public class CircularLoading {
/**
* 顯示Dialog
* @param context 上下文對象
* @param msg 提示內容
* @param isCancelable 是否可以點擊取消
* @return
*/
public static Dialog showLoadDialog(Context context, String msg, boolean isCancelable) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.circular_loading, null);
RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.dialog_bg);
 
// main.xml中的ImageView
ImageView loadImage = (ImageView) v.findViewById(R.id.load_iv);
TextView pointTextView = (TextView) v.findViewById(R.id.point_tv);
// 加載動畫
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.rotating_animation);
// 使用ImageView顯示動畫
loadImage.startAnimation(hyperspaceJumpAnimation);
pointTextView.setText(msg);
Dialog loadingDialog = new Dialog(context, R.style.TransDialogStyle);
loadingDialog.setContentView(layout);
loadingDialog.setCancelable(isCancelable);
loadingDialog.setCanceledOnTouchOutside(false);
 
 
Window window = loadingDialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setGravity(Gravity.CENTER);
window.setAttributes(lp);
window.setWindowAnimations(R.style.PopWindowAnimStyle);
loadingDialog.show();
return loadingDialog;
}
 
/**
* 關閉dialog
*/
public static void closeDialog(Dialog mCircularLoading) {
if (mCircularLoading != null && mCircularLoading.isShowing()) {
mCircularLoading.dismiss();
}
}
 
}

佈局文件

circular_loading.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:minHeight="60dp"
    android:minWidth="150dp"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/load_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:src="@drawable/dialog_loading_img" />
    <TextView
        android:id="@+id/point_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="0dp"
        android:layout_toRightOf="@+id/load_iv"
        android:ellipsize="middle"
        android:singleLine="true"
        android:textSize="16sp" />
</RelativeLayout>

動畫

rotating_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toDegrees="360" />

dialog_show.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0"
        android:toAlpha="1.0"/>
 
</set>

dialog_hide.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
    android:toAlpha="0"/>
</set>

Style

</style>
        <style name="TransDialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:windowFullscreen">true</item>
 
    </style>
 
    </style>
        <style name="PopWindowAnimStyle">
        <item name="android:windowShowAnimation">@anim/dialog_show</item>
        <item name="android:windowHideAnimation">@anim/dialog_hide</item>
</style>

使用方法

//顯示
mCircularLoading = CircularLoading.showLoadDialog(Dvr_Activity_Main.this, "加載中...", true);
 
//關閉
CircularLoading.closeDialog(mCircularLoading);

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: