在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress

我写写使用步骤

自定义view(CircleProgress )的代码

  1. package com.hysmarthotel.view;
  2.  
  3. import com.hysmarthotel.roomcontrol.R;
  4. import com.hysmarthotel.util.EaseInOutCubicInterpolator;
  5.  
  6. import android.animation.TimeInterpolator;
  7. import android.content.Context;
  8. import android.content.res.TypedArray;
  9. import android.graphics.Canvas;
  10. import android.graphics.Paint;
  11. import android.graphics.Point;
  12. import android.util.AttributeSet;
  13. import android.view.View;
  14. import android.view.animation.AnimationUtils;
  15.  
  16. public class CircleProgress extends View {
  17.  
  18. private static final int RED = 0xFFE5282C;
  19. private static final int YELLOW = 0xFF1F909A;
  20. private static final int BLUE = 0xFFFC9E12;
  21. private static final int COLOR_NUM = 3;
  22. private int[] COLORS;
  23. private TimeInterpolator mInterpolator = new EaseInOutCubicInterpolator();
  24.  
  25. private final double DEGREE = Math.PI / 180;
  26. private Paint mPaint;
  27. private int mViewSize;
  28. private int mPointRadius;
  29. private long mStartTime;
  30. private long mPlayTime;
  31. private boolean mStartAnim = false;
  32. private Point mCenter = new Point();
  33.  
  34. private ArcPoint[] mArcPoint;
  35. private static final int POINT_NUM = 15;
  36. private static final int DELTA_ANGLE = 360 / POINT_NUM;
  37. private long mDuration = 3600;
  38.  
  39. public CircleProgress(Context context) {
  40. super(context);
  41. init(null, 0);
  42. }
  43.  
  44. public CircleProgress(Context context, AttributeSet attrs) {
  45. super(context, attrs);
  46. init(attrs, 0);
  47. }
  48.  
  49. public CircleProgress(Context context, AttributeSet attrs, int defStyle) {
  50. super(context, attrs, defStyle);
  51. init(attrs, defStyle);
  52. }
  53.  
  54. private void init(AttributeSet attrs, int defStyle) {
  55. mArcPoint = new ArcPoint[POINT_NUM];
  56.  
  57. mPaint = new Paint();
  58. mPaint.setAntiAlias(true);
  59. mPaint.setStyle(Paint.Style.FILL);
  60.  
  61. TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleProgress, defStyle, 0);
  62. int color1 = a.getColor(R.styleable.CircleProgress_color1, RED);
  63. int color2 = a.getColor(R.styleable.CircleProgress_color2, YELLOW);
  64. int color3 = a.getColor(R.styleable.CircleProgress_color3, BLUE);
  65. a.recycle();
  66.  
  67. COLORS = new int[]{color1, color2, color3};
  68. }
  69.  
  70. @Override
  71. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  72. int defaultSize = getResources().getDimensionPixelSize(R.dimen.default_circle_view_size);
  73. int width = getDefaultSize(defaultSize, widthMeasureSpec);
  74. int height = getDefaultSize(defaultSize, heightMeasureSpec);
  75. mViewSize = Math.min(width, height);
  76. setMeasuredDimension(mViewSize, mViewSize);
  77. mCenter.set(mViewSize / 2, mViewSize / 2);
  78.  
  79. calPoints(1.0f);
  80. }
  81.  
  82. @Override
  83. protected void onDraw(Canvas canvas) {
  84. canvas.save();
  85. canvas.translate(mCenter.x, mCenter.y);
  86.  
  87. float factor = getFactor();
  88. canvas.rotate(36 * factor);
  89. float x, y;
  90. for (int i = 0; i < POINT_NUM; ++i) {
  91. mPaint.setColor(mArcPoint[i].color);
  92. float itemFactor = getItemFactor(i, factor);
  93. x = mArcPoint[i].x - 2 * mArcPoint[i].x * itemFactor;
  94. y = mArcPoint[i].y - 2 * mArcPoint[i].y * itemFactor;
  95. canvas.drawCircle(x, y, mPointRadius, mPaint);
  96. }
  97.  
  98. canvas.restore();
  99.  
  100. if (mStartAnim) {
  101. postInvalidate();
  102. }
  103. }
  104.  
  105. private void calPoints(float factor) {
  106. int radius = (int) (mViewSize / 3 * factor);
  107. mPointRadius = radius / 12;
  108.  
  109. for (int i = 0; i < POINT_NUM; ++i) {
  110. float x = radius * -(float) Math.sin(DEGREE * DELTA_ANGLE * i);
  111. float y = radius * -(float) Math.cos(DEGREE * DELTA_ANGLE * i);
  112.  
  113. ArcPoint point = new ArcPoint(x, y, COLORS[i % COLOR_NUM]);
  114. mArcPoint[i] = point;
  115. }
  116. }
  117.  
  118. private float getFactor() {
  119. if (mStartAnim) {
  120. mPlayTime = AnimationUtils.currentAnimationTimeMillis() - mStartTime;
  121. }
  122. float factor = mPlayTime / (float) mDuration;
  123. return factor % 1f;
  124. }
  125.  
  126. private float getItemFactor(int index, float factor) {
  127. float itemFactor = (factor - 0.66f / POINT_NUM * index) * 3;
  128. if (itemFactor < 0f) {
  129. itemFactor = 0f;
  130. } else if (itemFactor > 1f) {
  131. itemFactor = 1f;
  132. }
  133. return mInterpolator.getInterpolation(itemFactor);
  134. }
  135.  
  136. public void startAnim() {
  137. mPlayTime = mPlayTime % mDuration;
  138. mStartTime = AnimationUtils.currentAnimationTimeMillis() - mPlayTime;
  139. mStartAnim = true;
  140. postInvalidate();
  141. }
  142.  
  143. public void reset() {
  144. stopAnim();
  145. mPlayTime = 0;
  146. postInvalidate();
  147.  
  148. }
  149.  
  150. public void stopAnim() {
  151. mStartAnim = false;
  152. }
  153.  
  154. public void setInterpolator(TimeInterpolator interpolator) {
  155. mInterpolator = interpolator;
  156. }
  157.  
  158. public void setDuration(long duration) {
  159. mDuration = duration;
  160. }
  161.  
  162. public void setRadius(float factor) {
  163. stopAnim();
  164. calPoints(factor);
  165. startAnim();
  166. }
  167.  
  168. static class ArcPoint {
  169. float x;
  170. float y;
  171. int color;
  172.  
  173. ArcPoint(float x, float y, int color) {
  174. this.x = x;
  175. this.y = y;
  176. this.color = color;
  177. }
  178. }
  179.  
  180. }

EaseInOutCubicInterpolator是自定义view(CircleProgress )中要是用的一个工具

  1. package com.hysmarthotel.util;
  2.  
  3. import android.animation.TimeInterpolator;
  4.  
  5. public class EaseInOutCubicInterpolator implements TimeInterpolator {
  6.  
  7. @Override
  8. public float getInterpolation(float input) {
  9. if ((input *= 2) < 1.0f) {
  10. return 0.5f * input * input * input;
  11. }
  12. input -= 2;
  13. return 0.5f * input * input * input + 1;
  14. }
  15.  
  16. }

在activity中的调用(还有一些其他用法可以自己看看github上的源代码)

  1. mProgressView = (CircleProgress)findViewById(R.id.progress_vie);
  2. mProgressView.startAnim(); //开始
  3.  
  4. mProgressView.stopAnim(); //结束
  5.  
  6. mProgressView.setRadius(factor); //半径
  7.  
  8. mProgressView.reset(); //复原

在xml文件中的布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:circleprogress="http://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol" //这个地方记得要加 //包名
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/bg1" >
  8.  
  9. <com.hysmarthotel.view.CircleProgress //类名
  10. android:id="@+id/progress_vie"
  11. android:layout_x="350.5px"
  12. android:layout_y="150.0px"
  13. android:layout_width="1140.0px"
  14. android:layout_height="700.0px"
  15. circleprogress:color1="@android:color/holo_red_light" //这些参数就是通过xmlns:circleprogress,和attrs文件相关联的
  1.     circleprogress:color2="@android:color/holo_green_light"
  2.  
  3.     circleprogress:color3="@android:color/holo_blue_light" />

自己在values目录中新建的attrs文件,这是与自定义view中自定义参数相关的

  1. <declare-styleable name="CircleProgress">
  2. <attr name="color1" format="reference|color"/>
  3. <attr name="color2" format="reference|color"/>
  4. <attr name="color3" format="reference|color"/>
  5. </declare-styleable>

自己在values目录中新建的dimens文件,这个只是几个颜色参数

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <dimen name="activity_horizontal_margin">16dp</dimen>
  4. <dimen name="activity_vertical_margin">16dp</dimen>
  5. <dimen name="default_circle_view_size">200dp</dimen>
  6. </resources>

自定义加载loading view动画组件的使用。的更多相关文章

  1. 页面预加载loading动画,再载入内容

    默认情况下如果网站请求速度慢,所以会有一段时间的空白页面等等,用户体验效果不好,见到很多的页面都有预加载的效果,加载之前先加载一个动画,后台进程继续加载页面内容,当页面内容加载完之后再退出动画显示内容 ...

  2. Android 自定义View修炼-自定义加载进度动画XCLoadingImageView

    一.概述 本自定义View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进度颜色. ...

  3. [Swift通天遁地]五、高级扩展-(11)图像加载Loading动画效果的自定义和缓存

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. React Native封装Toast与加载Loading组件

    React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...

  5. html自定义加载动画

    整体代码 HTML 实现自定义加载动画,话不多说如下代码所示: <!DOCTYPE html> <html lang="en"> <head> ...

  6. 纯css3 加载loading动画特效

    最近项目中要实现当页面还没有加载完给用户提示正在加载的loading,本来是想做个图片提示的,但是图片如果放大电脑的分辨率就会感觉到很虚,体验效果很不好.于是就采用css3+js实现这个loading ...

  7. 简易仿ios菊花加载loading图

    原文链接:https://mp.weixin.qq.com/s/wBbQgOfr59wntNK9ZJ5iRw 项目中经常会用到加载数据的loading显示图,除了设计根据app自身设计的动画loadi ...

  8. 一个很酷的加载loading效果--IT蓝豹

    一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...

  9. Android:webView加载h5网页视频,播放不了,以及横屏全屏的问题和实现自定义加载进度条的效果

    1.webView加载h5网页视频,播放不了,android3.0之后要在menifest添加硬件加速的属性 android:hardwareAccelerated="true". ...

随机推荐

  1. Eclipse迁移到Android studio步骤如下:

    一.从Eclipse中导出:1.将你的ADT插件版本升级到22.0以上.2.在Eclipse中,选择File-->Export.3.在弹出的导出窗口中,打开Android的文件夹,选择“Gene ...

  2. Java中的网络编程

    ​ Java中的网路编程主要是Java的Socket编程,属于JavaEE中的高级的部分,以下内容是对java网路编程的一个小结,代码都是经过编译调试的 C/S程序应用:客户/服务器模式,如QQ客户端 ...

  3. mac+php+xdebug+phpstorm在苹果下配置xdebug一波三折

    1.下载xdebug文件 http://xdebug.org/wizard.php 将phpinfo()的源代码复制到文本框中,xdebug会提示如何配置和下载哪个版本的xdebug. 全部下载地址: ...

  4. php上传功能集后缀名判断和随机命名

    form.php <html> <head> <meta http-equiv="content-type" content="text/h ...

  5. 【类库】私房干货.Net数据层方法的封装

    [类库]私房干货.Net数据层方法的封装 作者:白宁超 时间:2016年3月5日22:51:47 摘要:继上篇<Oracle手边常用70则脚本知识汇总>文章的发表,引起很多朋友关注.便促使 ...

  6. 【Java】 环境变量如何配置?

    Java知识简介与环境变量配置问题 一.在学习一门语言中,不仅需要掌握其语法结构,开发平台以及环境也是很重要的.在开始Java学习之前首先对其进行压缩包的下载安装,以及开发平台环境下载安装.基于此下面 ...

  7. 1Z0-053 争议题目解析304

    1Z0-053 争议题目解析304 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 304.What privileges must be granted to allow an a ...

  8. 韩顺平_linux_随堂笔记

    这还是自己3年前(2011年)整理的笔记,记得当时那会儿自己对Linux还特别的憧憬,也很喜欢韩老师的讲课风格,边看边做笔记乐此不彼,现在开通了技术博客,所以把当年的笔记也放上来和大家分享.同时推荐没 ...

  9. 前端开发编辑器(notepad++、sublime text)

    1.Notepad++ 正则替换: 如<td>第三节</td> 替换成<td><input type="text" value=" ...

  10. 如何让你的网站支持https

    如何让你的网站支持https 当今世界的主流网站基本都是使用https对外界提供服务,甚至有某些公司建议完全使用https, 那么https是什么呢?请参考如下的图解,https是在我们通常说的tcp ...