Android实现加载圈

 更新时间:2022年06月21日 17:24:32   作者:阿毅同学  
这篇文章主要为大家详细介绍了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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android自定义view实现多色进度条GradientProgressView的绘制

    Android自定义view实现多色进度条GradientProgressView的绘制

    我们常使用shape实现渐变色,但是shape的极限却只有三色,如果有超过三种颜色的View的要求,那么我们就不得不去自定义View来实现这个需求,所以下面我们就来看看如何自定义view实现多色进度条的绘制吧
    2023-08-08
  • android照相、相册获取图片剪裁报错的解决方法

    android照相、相册获取图片剪裁报错的解决方法

    最近在项目中用到了照相和相册取图剪裁上传头像,就在网上逛了逛,基本都是千篇一律,就弄下来用了用,没想到的是各种各样的奇葩问题就出现了。先给大家看看代码问题慢慢来解决
    2014-11-11
  • Android基于wheelView实现自定义日期选择器

    Android基于wheelView实现自定义日期选择器

    这篇文章主要为大家详细介绍了Android基于wheelView实现自定义日期选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • android RecyclerView实现条目Item拖拽排序与滑动删除

    android RecyclerView实现条目Item拖拽排序与滑动删除

    本篇文章主要介绍了android RecyclerView实现条目Item拖拽排序与滑动删除,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • Android 实现桌面未读角标

    Android 实现桌面未读角标

    本文主要介绍了Android实现桌面未读角标的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04
  • Android Lottie实现中秋月饼变明月动画特效实例

    Android Lottie实现中秋月饼变明月动画特效实例

    Lottie是Airbnb开源的一个支持 Android、iOS 以及 ReactNative,利用json文件的方式快速实现动画效果的库,下面这篇文章主要给大家介绍了关于Android Lottie实现中秋月饼变明月动画特效的相关资料,需要的朋友可以参考下
    2021-09-09
  • Android自定义View弧线进度控件

    Android自定义View弧线进度控件

    这篇文章主要为大家详细介绍了Android自定义View弧线进度控件,点击开始按钮时,逐渐的出现进度,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android OpenGLES2.0绘制三角形(二)

    Android OpenGLES2.0绘制三角形(二)

    这篇文章主要为大家详细介绍了Android OpenGLES2.0绘制三角形的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android使用Opengl录像时添加水印

    Android使用Opengl录像时添加水印

    这篇文章主要为大家详细介绍了Android使用Opengl录像时添加水印,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • Android简单实用的可拖拽GridView组件分享

    Android简单实用的可拖拽GridView组件分享

    在我们日常开发中,使用 GridView 这种网格视图的场合还是不少的,本篇我们来介绍一个支持拖拽的 GridView 组件,可以轻松搞定网格视图的拖拽排序,需要的可以参考一下
    2023-06-06

最新评论