Android仿ios加载loading菊花图效果

所属分类: 软件编程 / Android 阅读数: 54
收藏 0 赞 0 分享

项目中经常会用到加载数据的loading显示图,除了设计根据app自身设计的动画loading,一般用的比较多的是仿照ios 的菊花加载loading 图,当然一些条件下还会涉及到加载成功/ 失败情况的显示,还有显示文字。

 

使用ProgressBar 来加载动画转圈,这里使用drawable文件 定义转圈动画, indeterminateDrawable 属性进行加载。

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
  android:drawable="@mipmap/load"
  android:pivotX="50%"
  android:pivotY="50%" />
<ProgressBar
  android:id="@+id/progressBar"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:indeterminateDrawable="@drawable/anim" />

部分情况下,在加载成功/ 失败之后会显示对应的静态图片,所以一开始想直接通过 setIndeterminateDrawable(Drawable d) 来加载静态图片,但是直接写是显示不出图片的,还要设置Drawable 的位置 d.setBounds(Rect bounds) ,即使这样加载出了静态图片,但是设置 R.drawable.anim 的转圈动画时 却没有了转圈的效果,好气哟 ~~

所以在自定义view 的布局里 成功/失败的状态单独用 imageView 显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="110dp"
  android:layout_height="110dp"
  android:background="@drawable/shape_dialog_bg"
  android:gravity="center"
  android:orientation="vertical">
  <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:indeterminateDrawable="@drawable/anim" />
  <ImageView
    android:id="@+id/iv"
    android:visibility="gone"
    android:layout_width="50dp"
    android:layout_height="50dp" />
  <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="正在加载"
    android:textColor="#fff" />
</LinearLayout>

自定义view,提供三种状态的方法。

public class LoadingView extends LinearLayout {
  ...构造函数...
  /**
   * loading
   */
  public void showLoading() {
    iv.setVisibility(GONE);
    progressBar.setVisibility(VISIBLE);
  }
  /**
   * 成功
   */
  public void showSuccess() {
    iv.setImageResource(R.mipmap.load_success);
    iv.setVisibility(View.VISIBLE);
    progressBar.setVisibility(GONE);
  }
  /**
   *失败
   */
  public void showFail() {
    iv.setImageResource(R.mipmap.load_fail);
    iv.setVisibility(View.VISIBLE);
    progressBar.setVisibility(GONE);
  }
  /**
   * 提示文字
   *
   * @param txt string
   */
  public void setText(String txt) {
    tv.setText(txt);
  }
  /**
   * 提示文字
   */
  public void setText(@StringRes int txtId) {
    tv.setText(txtId);
  }
}

效果图:

github地址: https://github.com/taixiang/loading

以上所述是小编给大家介绍的Android仿ios加载loading菊花图效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多