前两天接到任务做一个UI,有用到动画,于是抽空看了下Android动画相关知识。

Android Animation共有四大类型,分别是

Alpha      透明度动画

Scale      大小伸缩动画

Translate 位移动画

Rotate     旋转动画

这四类动画按模式又可分为:

tweened animation(渐变动画) —— alpha  与   scale

frame by frame(画面转换动画) ——  translate  与 rotate

讲一下我所了解到的rotate动画的各个属性:

在XML中:

 

官方给予的Rotate属性如上所示。

android:drawable    需要进行旋转动画的图片

android:fromDegrees  旋转的起始点(旋转开始的角度)

android:toDegrees     旋转的结束点(旋转最终角度)

andoird:pivotX       旋转点的X值(距离左侧的偏移量)

android:pivotY旋转点的Y值(距离顶部的偏移量)

android: visible这个好理解,就是图片初始的显示状态

我对这个的理解是:

rotate动画是以设置的旋转点(pivotX,pivotY)为坐标原点,顺时针方向从旋转起始角度(fromDegrees)到旋转最终角度(toDegrees)的动画,

其中旋转点默认为图片左上角是(0,0)。

现在写一个rotate动画的XML文件:rotate_anim.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <rotate
  4. android:fromDegrees="0"
  5. android:interpolator="@android:anim/linear_interpolator"//设置转动速率,这里设置的是匀速转动
  6. android:pivotX="50%"
  7. android:pivotY="50%"
  8. android:toDegrees="359"
  9. android:duration = "1500"
  10. android:repeatCount = "-1"
  11. android:drawable = "@drawable/ic_launcher"
  12. android:visible = "true">
  13. </rotate>
  14. </rotate>

这里需要注意下:

android:pivotX 与 android:pivotY 这两个属性值为float,可以给具体数也可以给百分比。比如你知道图片大小是100,你可以给(50,50)表示旋转中心点距图片左边50,右边50

如果不知道图片的准确大小,可以跟代码所示,给百分比。

上面的代码中还有一些在官方API中并未提及的属性,但是这些属性依旧可以使用到。


android:interpolator:这个属性是用来设置转动速率的。

LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果,

android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止

android:duration属性表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。

android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行

android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。

在android:repeatCount大于0或为infinite时生效

android:detachWallpaper 表示是否在壁纸上运行

android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。normal保持内容当前的z轴顺序top运行时在最顶层显示bottom运行时在最底层显示


以上属性中,博主亲测,均可以正常使用。

布局文件 activity_main.xml,没什么特别的:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:id="@+id/img"
  7. android:layout_centerInParent="true"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"/>
  10. </RelativeLayout>

MainActivity.java

  1. package com.example.rotateanimation;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.animation.Animation;
  5. import android.view.animation.AnimationUtils;
  6. import android.view.animation.LinearInterpolator;
  7. import android.widget.ImageView;
  8. public class MainActivity extends Activity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. Animation rotate = AnimationUtils.loadAnimation(this, R.drawable.rotate_anim);
  14. ((ImageView)findViewById(R.id.img)).setAnimation(rotate);
  15. ((ImageView)findViewById(R.id.img)).startAnimation(rotate);
  16. }
  17. }

这样,运行工程,一个旋转的Android小绿人就出现了。PS:不知道怎么制作动态图,效果展示不了。

在编写过程中有两个地方需要大家注意:

1、在rotate_anim.xml文件中,最外层的item名字要为rotate,而不是set

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <rotate
  4. android:fromDegrees="0"
  5. android:interpolator="@android:anim/linear_interpolator"//设置转动速率,这里设置的是匀速转动
  6. android:pivotX="50%"
  7. android:pivotY="50%"
  8. android:toDegrees="359"
  9. android:duration = "1500"
  10. android:repeatCount = "-1"
  11. android:drawable = "@drawable/ic_launcher"
  12. android:visible = "true">
  13. </rotate>
  14. </set>

如果代码如上所示,是set的话,属性android:interpolator就会失效。具体原因我没有找到,有知道的朋友希望能在博文下方解惑,多谢。

这种情况下 需要在代码中设置动画的interpolator:

  1. Animation rotate = AnimationUtils.loadAnimation(this, R.drawable.rotate_anim);
  2. LinearInterpolator lin = new LinearInterpolator();
  3. rotate.setInterpolator(lin);
  4. ((ImageView)findViewById(R.id.img)).setAnimation(rotate);
  5. ((ImageView)findViewById(R.id.img)).startAnimation(rotate);

2.我试过在布局文件中直接设置ImageView的src:android:src="@drawable/rotate_anim" 结果是图片会出现,但是不会旋转,所以不要这样做。

3. 如果ImageView本身就带图片了,那么rotate里面设置的drawable属性是无效的。会优先使用ImageView自身的图片

4.设置android:drawable属性的时候,不要忘了设置android:visible = "true",因为它默认是false(不可见)的。

在Java代码中:

RotateAnimation共有三个构造方法,这里详细讲述第三个,也就是参数最多的那个。
下面是Google官网中对于RotateAnimation的相关参数介绍:
 

这里面大部分参数已经在上面介绍过了,重点说下pivotXType与pivotYType

int pivotXType,  动画在X轴相对于物件位置类型,与下面的pivotXValue结合,确定X轴上旋转中心。

可能值为:Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT

如果pivotXType=Animation.ABSOLUTE,则此参数为旋转中心在屏幕上X轴的值;

如果pivotXType=Animation.RELATIVE_TO_PARENT,则参数pivotXValue为旋转中心在父控件水平位置百分比,如0.5表示在父控件水平方向中间位置;
如果pivotXType=Animation.RELATIVE_TO_SELF,则参数pivotXValue为旋转中心在控件自身水平位置百分比,如果X和Y的Value都设置为0.5,则该控件以自身中心旋转。

好了,贴代码: activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:id="@+id/img"
  7. android:layout_centerInParent="true"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:src="@drawable/ic_launcher"/>
  11. </RelativeLayout>

MainActivity.java

  1. package com.example.rotateanimation;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.animation.Animation;
  5. import android.view.animation.AnimationUtils;
  6. import android.view.animation.LinearInterpolator;
  7. import android.view.animation.RotateAnimation;
  8. import android.widget.ImageView;
  9. public class MainActivity extends Activity {
  10. private ImageView img;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. img = (ImageView) findViewById(R.id.img);
  16. //用xml实现
  17. /*    Animation rotate = AnimationUtils.loadAnimation(this, R.drawable.rotate_anim);
  18. //        LinearInterpolator lin = new LinearInterpolator();
  19. //        rotate.setInterpolator(lin);
  20. img.setAnimation(rotate);
  21. img.startAnimation(rotate);*/
  22. //用Java code实现
  23. RotateAnimation rotate  = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  24. LinearInterpolator lin = new LinearInterpolator();
  25. rotate.setInterpolator(lin);
  26. rotate.setDuration(1500);//设置动画持续时间
  27. rotate.setRepeatCount(-1);//设置重复次数
  28. rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
  29. rotate.setStartOffset(10);//执行前的等待时间
  30. img.setAnimation(rotate);
  31. }
  32. }

rotate里还有很多set属性的方法,跟XML中的属性一一对应,有兴趣的可以自己去尝试一下。

原创博文,转载请注明出处

以上就是RotateAnimation的内容,有不对的地方欢迎指正~

点击下载源码

参考文章:

1.http://my.oschina.net/ryanisme/blog/109674

2.http://www.android100.org/html/201304/24/2282.html

3.http://blog.csdn.net/jason0539/article/details/16370405

Android 常用动画之RotateAnimation的更多相关文章

  1. Android 常用动画

    一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha :渐变透明度动画效果 scale :渐变尺寸伸缩 ...

  2. Android常用动画Frame-By-Frame Animations的使用

    在Android的动画中有一种叫做Frame by Frame 的动画效果,就是跟Flash播放一样,是一帧一帧地显示,如果动画是连续并且有规律的话,就跟播放视频一样. 首先在drawable目录下添 ...

  3. Android常用动画alpha和rotate同时使用

    Android的动画可以是一种动画,也可以多种动画作用于一张图片上,如RotaeAnimation和AlphaAnimation同时放到一个配置文件中 alpha1.xml <?xml vers ...

  4. Android常用动画Animation的使用

    Andriod中有几种常用的Animation AlphaAnimation  淡入淡出效果 RotateAnimation 旋转效果 ScaleAnimation 缩放动画 TranslaAnima ...

  5. Android 常用动画小结

    1. 渐入动画 // Request the next activity transition (here starting a new one). startActivity(new Intent( ...

  6. Android 动画之RotateAnimation应用详解

    android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 TranslateAnimation 位移动画效果 RotateAnimat ...

  7. android 属性动画

    一直再追郭霖的博客和imooc上的一些新的视频,最近有讲到属性动画. 以下内容为博客学习以及imooc上视频资料的学习笔记: 在3.0之前比较常见的动画为tween动画和frame动画: tween动 ...

  8. Android之动画

    Android的动画可以分为三种,View动画.帧动画.属性动画.View动画通过对场景里的对象不断做图像变化(平移.缩放.旋转.透明度)从而产生动画效果,它是一种渐进式动画,而且View动画支持自定 ...

  9. 【Android开发日记】之基础篇(二)——Android的动画效果

          什么是动画,动画的本质是通过连续不断地显示若干图像来产生“动”起来的效果.比如说一个移动的动画,就是在一定的时间段内,以恰当的速率(起码要12帧/秒以上,才会让人产生动起来的错觉)每隔若干 ...

随机推荐

  1. Windows 2019 下安装Oracle18c

    1. 跟之前版本不一样 与linux 的版本一样 18c的DB 端的安装有区别. 首先需要 创建一个oracle的目录. 这里最简单的方法是 参照12c的目录来 创建 比如我创建的 然后将 db_ho ...

  2. [转帖] 外部访问k8s 里面pod的方式方法

    https://jimmysong.io/posts/accessing-kubernetes-pods-from-outside-of-the-cluster/ 从外部访问Kubernetes中的P ...

  3. 【题解】 P1879 玉米田Corn Fields (动态规划,状态压缩)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  4. 【BZOJ3232】圈地游戏(分数规划,网络流)

    [BZOJ3232]圈地游戏(分数规划,网络流) 题面 BZOJ 题解 很神仙的一道题. 首先看到最大化的比值很容易想到分数规划.现在考虑分数规划之后怎么计算贡献. 首先每条边的贡献就变成了\(mid ...

  5. CentOS服务器配置SSH免密码登录

    由于工作需要,经常要登录到多台服务器远程操作,每次都是ssh user@host:port 再输入密码,时间长了,难免觉得乏味-- 故而从度娘那里扒来了一些让SSH免密码登录的办法,其实这也是使用Gi ...

  6. 【转】Altium Designer 3D封装下载及导入教程

    首先 先晒几个图:是不是很逼真啊.. ---------------------------------------教程---------------------------------------- ...

  7. 洛谷 P1073 最优贸易 解题报告

    P1073 最优贸易 题目描述 \(C\)国有\(n\)个大城市和\(m\)条道路,每条道路连接这\(n\)个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这\(m\)条道路中有一部分 ...

  8. 前端学习 -- Css -- 选择器的优先级

    当使用不同的选择器,选中同一个元素时并且设置相同的样式时,这时样式之间产生了冲突,最终到底采用哪个选择器定义的样式,由选择器的优先级(权重)决定优先级高的优先显示. 优先级的规则 内联样式 , 优先级 ...

  9. 洛谷P3721 单旋

    什么毒瘤...... 题意:模拟一棵单旋splay,求每次插入,splay最值,删除最值的操作次数. 解:乍一看感觉很神,又因为是LCT题单上的,然后就折磨了我好久,最后跑去看题解... 居然是手玩找 ...

  10. 《剑指offer》— JavaScript(34)第一个只出现一次的字符

    第一个只出现一次的字符 题目描述 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置. *** 思路 新建一个对象,其中key用 ...