项目地址:https://github.com/CuteWiseCode/MyLoadingView

先上效果图

实现思路: 代码实现主要结合自定义view 以及动画属性的方式,根据需求调整动画的展示方式、加速度等。

一、动画的布局文件

将白色背景图以及需要转动的图片资源引用到布局文件中。布局文件以FrameLayout 作为父view,默认展示背景图以及第一个转动的图片。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" android:layout_gravity="center"><ImageViewandroid:layout_gravity="center"android:layout_width="86.0dip"android:layout_height="86.0dip"android:background="@mipmap/bg_animation" /><ImageViewandroid:layout_gravity="center"android:id="@+id/loading_view_01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/loading_01" /><ImageViewandroid:layout_gravity="center"android:id="@+id/loading_view_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/loading_02"android:visibility="invisible" /><ImageViewandroid:layout_gravity="center"android:id="@+id/loading_view_03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/loading_03"android:visibility="invisible" /><ImageViewandroid:layout_gravity="center"android:id="@+id/loading_view_04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/loading_04"android:visibility="invisible" /><ImageViewandroid:layout_gravity="center"android:id="@+id/loading_view_05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/loading_05"android:visibility="invisible" /></FrameLayout>

二、创建一个 AnimationView 类

2.1、继承 FrameLayout ,重写两个构造函数   AnimationView(Context paramContext) 和 AnimationView(@NonNull Context context, @Nullable AttributeSet attrs) ,了解自定义View的童鞋 应该知道,第一个构造函数用于在代码中新建时调用,第二个构造函数用户在布局文件中定义时调用。

 //构造函数public AnimationView(Context paramContext){super(paramContext);inflate(getContext(), R.layout.loading_view, this);//layout_loading_viewinitializeView();}public AnimationView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);inflate(getContext(), R.layout.loading_view, this);//layout_loading_viewinitializeView();}

2.2、在构造函数中解析布局文件,并进行初始化

/*** 初始化控件*/private void initializeView(){this.viewf = ((ImageView)findViewById(R.id.loading_view_01));this.viewg = ((ImageView)findViewById(R.id.loading_view_02));this.viewh = ((ImageView)findViewById(R.id.loading_view_03));this.viewi = ((ImageView)findViewById(R.id.loading_view_04));this.viewj = ((ImageView)findViewById(R.id.loading_view_05));this.viewe = this.viewf;//测量控件的大小 UNSPECIFIED = 0 EXACTLY = 1 AT_MOST= 2int m = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);int n = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);this.viewf.measure(m, n);this.floata = (this.viewf.getMeasuredWidth() / 2);//获取宽的值,除2this.floatb = (this.viewf.getMeasuredHeight() / 2);//获取高的值除2initView();//开始动画}

2.3、设置并开始动画,注意这里使用到动画监听   animationListener

 public void initView(){tostartAnimation(this.viewe, 0.0F, 90.0F);}
/*** * @param paramView 具体某个view* @param paramFloat1 0.0f* @param paramFloat2 90.0f* * */private void tostartAnimation(View paramView, float paramFloat1, float paramFloat2){AnimationLoad animation = new AnimationLoad(paramFloat1, paramFloat2, this.floata, this.floatb, this.floatc, true);animation.setDuration(this.intk);animation.setFillAfter(true);//true if the animation should apply its transformation after it endsanimation.setInterpolator(new AccelerateInterpolator());//Sets the acceleration curve for this animation. Defaults to a linear interpolation.Parameters:i The interpolator which defines the acceleration curveanimation.setAnimationListener(new animationListener());//the animation listener to be notifiedparamView.startAnimation(animation);}

监听类:animationListener。在动画结束的时候,启动runnable,启动另一个动画

 private final class animationListenerimplements Animation.AnimationListener{private animationListener(){}public void onAnimationEnd(Animation paramAnimation){//The Runnable that will be executed.AnimationView.this.post(new runnable());}public void onAnimationRepeat(Animation paramAnimation){}public void onAnimationStart(Animation paramAnimation){}}

runnable 线程:

 private final class runnableimplements Runnable{private runnable(){}public void run(){viewf.setVisibility(View.GONE);//1viewg.setVisibility(View.GONE);//2viewh.setVisibility(View.GONE);//3viewi.setVisibility(View.GONE);//4viewj.setVisibility(View.GONE);//5viewe.setVisibility(View.GONE);intd++;if (intd % 5 == 0)//intdsetData(viewf);//第一张while (true){if (1 == intd % 5){setData( viewg);
//                  continue;}if (2 == intd % 5){setData( viewh);
//                  continue;}if (3 == intd % 5){setData( viewi);
//                  continue;}if (4 == intd % 5)setData( viewj);viewe.setVisibility(View.VISIBLE);viewe.requestFocus();AnimationLoad animation = new AnimationLoad(-90.0F, 0.0F, floata, floatb, floatc, false);animation.setDuration(intk);animation.setFillAfter(true);animation.setInterpolator(new DecelerateInterpolator());//减速animation.setAnimationListener(new Animation.AnimationListener(){public void onAnimationEnd(Animation paramAnimation){AnimationView.this.initView();}public void onAnimationRepeat(Animation paramAnimation){}public void onAnimationStart(Animation paramAnimation){}});viewe.startAnimation(animation);return;}}}

上述代码中 AnimationLoad 类如下:继承了Animation,使用Camera 以及Matrix  定义了旋转的方式。

  private final float floata;private final float floatb;private final float floatc;private final float floatd;private final float floate;private final boolean boolf;private Camera camerag;public AnimationLoad(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4, float paramFloat5, boolean paramBoolean){this.floata = paramFloat1;//0this.floatb = paramFloat2;//90.0fthis.floatc = paramFloat3;//172this.floatd = paramFloat4;//172this.floate = paramFloat5;//0this.boolf = paramBoolean;}protected void applyTransformation(float paramFloat, Transformation paramTransformation){float f1 = this.floata;//0.0ffloat f2 = f1 + paramFloat * (this.floatb - f1);//90float f3 = this.floatc;//172float f4 = this.floatd;//172Camera localCamera = this.camerag;Matrix localMatrix = paramTransformation.getMatrix();localCamera.save();//Saves the camera state. Each save should be balanced with a call to restore().//如果时加速if (this.boolf)localCamera.translate(0.0F, 0.0F, paramFloat * this.floate);//Applies a translation transform on all three axislocalCamera.translate(0.0F, 0.0F, this.floate * (1.0F - paramFloat));while (true){localCamera.rotateY(f2);//Applies a rotation transform around the Y axis.localCamera.getMatrix(localMatrix);//Computes the matrix corresponding to the current transformation and copies it to the supplied matrix object.localCamera.restore();//Restores the saved state, if any
//      localMatrix.postScale(0.5f, 0.5f);//使原来的图像缩放成原来的1/2localMatrix.preTranslate(-f3, -f4);localMatrix.postTranslate(f3, f4);return;}}/*** Initialize this animation with the dimensions of the object being animated */public void initialize(int paramInt1, int paramInt2, int paramInt3, int paramInt4){super.initialize(paramInt1, paramInt2, paramInt3, paramInt4);this.camerag = new Camera();}

分享到此结束,demo 请前往github,  记得给个star哦

Android 自定义动画 LoadingView相关推荐

  1. android 自定义loading,Android自定义动画-StarLoadingView

    今天来分享第二个自定义loading的动画,起了个名字叫 蹦跶的星星 ,还是老规矩先介绍,后上图. 实现效果在最后,GIF有点大,手机流量慎重. 介绍 首先声明做这个动画的初衷是为了学习和分享,所以从 ...

  2. Android自定义动画专题二

    android自定义动画专题二 在上篇文章中给大家介绍了android自定义动画的第一种表现形式:view的绘制:不过这只是一种单纯利用自定义控件绘制的方式去实现:这篇文章会给大家演示如何通过自定义控 ...

  3. Android自定义动画专题一

    Android自定义动画 在目前的移动端产品中,不管是app还是网页一个好看酷炫的页面总是会第一时间吸引人的眼球,那么对于android开发人员来说,要想实现一个好看的页面必然需要掌握自定义控件以及自 ...

  4. Android自定义动画三-SVG动画

    Android自定义动画三-SVG动画 本篇文章主要是对SVG的一个介绍和使用,以及Android中对SVG的一个支持,从而可以帮助我们在android下很轻松的通过SVG实现一些非常酷炫的动画效果. ...

  5. Android自定义动画学习,实现左右摇摆动画

    (转载)http://johnnyg.iteye.com/blog/2074464 我们都知道Android SDK给我们提供了4种常用的动画效果分别是: AlphaAnimation:透明度变化动画 ...

  6. android 属性翻牌动画,Android自定义动画--卡牌翻牌动画

    Android系统中自带了四种动画,但是都只是平面上的并不能实现我们很常见的翻牌动画,所以今天我们就要通过自定义动画来实现翻牌动画. 要实现翻牌动画,我们需要了解三个类,一个是matrix类,一个是c ...

  7. Android 自定义动画(实现类似分享动画)

    最近在开发app中,要实现点击进入分享动画页面,然后照着每个Item的功能,来实现各自的功能 效果图如下: 首选自定义动画Activity import android.animation.Anima ...

  8. android自定义动画插值器(Interpolator)

    前言 之前已经讲过动画相关的内容,没有接触过的读者可以看下笔者之前对android动画使用的整理. Android动画总结 (valueAnimator.objectAnimator.Animator ...

  9. Android 自定义动画view(小变大,旋转,色值)

    也不知道到看了多少的动画总结了,但是用到的时候太少,过段时间就会忘记了. 既然如此,我选择直接去动手学习,步步进阶. 效果: 上代码之前我们分析一下才会加深自己的印象: 需要画一个矩形 和 一个圆形 ...

最新文章

  1. MM的SQLDMO- -哈哈(数据备份与恢复篇)
  2. 如何高性能的给UIImageView加个圆角
  3. 计算机等级保护2.0标准,网络安全等级保护2.0标准情况-马力.pdf
  4. vs最好的版本_Win10 环境下,LightGBM GPU 版本的安装
  5. golang和php哪个性能更强,相同逻辑的php与golang代码效率对比,最好语言落谁家…...
  6. Magicodes.IE Excel合并行数据导入教程
  7. 如何从零开始搭建网站?
  8. idea中Tomcat启动乱码问题
  9. 如何解决使用webpack打包之后,font-awsome路径不对的问题,终极解决方法
  10. python——学习登录用户和密码的判断——1
  11. uniapp 拍照 或者 相册 识别身份证信息
  12. 客户数据中台(CDP): 当代数字化营销顶梁柱
  13. 模拟人生4极乐净土mod_如何在《模拟人生4》中下载Mod
  14. DEDE网站安全设置防挂马教程
  15. 自己做量化交易软件(10)通通量化AI框架的数据获取与格式
  16. 30个精美的简单网站
  17. Vector space
  18. Maya: Rendering with Arnold 5 Maya教程之Arnold5渲染 Lynda课程中文字幕
  19. css3摇骰子,css3实现掷骰子(无图)
  20. 【硬币计数】基于matlab形态学硬币计数【含Matlab源码 393期】

热门文章

  1. 为什么文件会自动恢复成旧文件? -- windows server 2003
  2. swoole UDP TCP客户端
  3. 为什么越来越多的企业使用互联网电话(VoIP)?—Vecloud微云
  4. exp中query的使用方法
  5. 大数据算法:排位问题(2)
  6. Windows环境安装运行:Angular.js
  7. Kosaraju算法、Tarjan算法分析及证明--强连通分量的线性算法
  8. csu1377Putter HOJ12816
  9. 使用 Mashups4JSF 生成和消费 Mashup Feed
  10. 国家卫健委发布第一版新冠疫苗接种技术指南