亲宝软件园·资讯

展开

Android加载圈

阿毅同学 人气:0

开发过程中经常用到加载圈,特别是车机开发由于外设不同很多操作响应的等待时长经常要用到不同的加载圈。

首先,直接上菊花效果图,这是我直接从项目里面截取下来的。

核心代码

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

加载全部内容

相关教程
猜你喜欢
用户评论