[关闭]
@coder-pig 2015-11-16T03:22:40.000000Z 字数 9851 阅读 1395

Android基础入门教程——8.4.2 Android动画合集之补间动画

Android基础入门教程


本节引言:

本节带来的是Android三种动画中的第二种——补间动画(Tween),和前面学的帧动画不同,帧动画
是通过连续播放图片来模拟动画效果,而补间动画开发者只需指定动画开始,以及动画结束“关键帧”,
而动画变化的“中间帧”则由系统计算并补齐!好了,开始本节学习~


1.补间动画的分类和Interpolator

Andoird所支持的补间动画效果有如下这五种,或者说四种吧,第五种是前面几种的组合而已~

  • AlphaAnimation:透明度渐变效果,创建时许指定开始以及结束透明度,还有动画的持续
    时间,透明度的变化范围(0,1),0是完全透明,1是完全不透明;对应<alpha/>标签!
  • ScaleAnimation:缩放渐变效果,创建时需指定开始以及结束的缩放比,以及缩放参考点,
    还有动画的持续时间;对应<scale/>标签!
  • TranslateAnimation:位移渐变效果,创建时指定起始以及结束位置,并指定动画的持续
    时间即可;对应<translate/>标签!
  • RotateAnimation:旋转渐变效果,创建时指定动画起始以及结束的旋转角度,以及动画
    持续时间和旋转的轴心;对应<rotate/>标签
  • AnimationSet:组合渐变,就是前面多种渐变的组合,对应<set/>标签

在开始讲解各种动画的用法之前,我们先要来讲解一个东西:Interpolator

用来控制动画的变化速度,可以理解成动画渲染器,当然我们也可以自己实现Interpolator
接口,自行来控制动画的变化速度,而Android中已经为我们提供了五个可供选择的实现类:

  • LinearInterpolator:动画以均匀的速度改变
  • AccelerateInterpolator:在动画开始的地方改变速度较慢,然后开始加速
  • AccelerateDecelerateInterpolator:在动画开始、结束的地方改变速度较慢,中间时加速
  • CycleInterpolator:动画循环播放特定次数,变化速度按正弦曲线改变:
    Math.sin(2 * mCycles * Math.PI * input)
  • DecelerateInterpolator:在动画开始的地方改变速度较快,然后开始减速
  • AnticipateInterpolator:反向,先向相反方向改变一段再加速播放
  • AnticipateOvershootInterpolator:开始的时候向后然后向前甩一定值后返回最后的值
  • BounceInterpolator: 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
  • OvershottInterpolator:回弹,最后超出目的值然后缓慢改变到目的值

而这个东东,我们一般是在写动画xml文件时会用到,属性是:android:interpolator
而上面对应的值是:@android:anim/linear_interpolator,其实就是驼峰命名法变下划线而已
AccelerateDecelerateInterpolator对应:@android:anim/accelerate_decelerate_interpolator!


2.各种动画的详细讲解

这里的android:duration都是动画的持续时间,单位是毫秒~


1)AlphaAnimation(透明度渐变)

anim_alpha.xml

  1. <alpha xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  3. android:fromAlpha="1.0"
  4. android:toAlpha="0.1"
  5. android:duration="2000"/>

属性解释:

fromAlpha :起始透明度
toAlpha:结束透明度
透明度的范围为:0-1,完全透明-完全不透明


2)ScaleAnimation(缩放渐变)

anim_scale.xml

  1. <scale xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_interpolator"
  3. android:fromXScale="0.2"
  4. android:toXScale="1.5"
  5. android:fromYScale="0.2"
  6. android:toYScale="1.5"
  7. android:pivotX="50%"
  8. android:pivotY="50%"
  9. android:duration="2000"/>

属性解释:

fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例
toXScale/toYScale:沿着X轴/Y轴缩放的结束比例
pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的
中心为中轴点


3)TranslateAnimation(位移渐变)

anim_translate.xml

  1. <translate xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  3. android:fromXDelta="0"
  4. android:toXDelta="320"
  5. android:fromYDelta="0"
  6. android:toYDelta="0"
  7. android:duration="2000"/>

属性解释:

fromXDelta/fromYDelta:动画起始位置的X/Y坐标
toXDelta/toYDelta:动画结束位置的X/Y坐标


4)RotateAnimation(旋转渐变)

anim_rotate.xml

  1. <rotate xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  3. android:fromDegrees="0"
  4. android:toDegrees="360"
  5. android:duration="1000"
  6. android:repeatCount="1"
  7. android:repeatMode="reverse"/>

属性解释:

fromDegrees/toDegrees:旋转的起始/结束角度
repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次
另外,值为-1或者infinite时,表示动画永不停止
repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时
才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!


5)AnimationSet(组合渐变)

非常简单,就是前面几个动画组合到一起而已~

anim_set.xml

  1. <set xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/decelerate_interpolator"
  3. android:shareInterpolator="true" >
  4. <scale
  5. android:duration="2000"
  6. android:fromXScale="0.2"
  7. android:fromYScale="0.2"
  8. android:pivotX="50%"
  9. android:pivotY="50%"
  10. android:toXScale="1.5"
  11. android:toYScale="1.5" />
  12. <rotate
  13. android:duration="1000"
  14. android:fromDegrees="0"
  15. android:repeatCount="1"
  16. android:repeatMode="reverse"
  17. android:toDegrees="360" />
  18. <translate
  19. android:duration="2000"
  20. android:fromXDelta="0"
  21. android:fromYDelta="0"
  22. android:toXDelta="320"
  23. android:toYDelta="0" />
  24. <alpha
  25. android:duration="2000"
  26. android:fromAlpha="1.0"
  27. android:toAlpha="0.1" />
  28. </set>

3.写个例子来体验下

好的,下面我们就用上面写的动画来写一个例子,让我们体会体会何为补间动画:
首先来个简单的布局:activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <Button
  6. android:id="@+id/btn_alpha"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="透明度渐变" />
  10. <Button
  11. android:id="@+id/btn_scale"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="缩放渐变" />
  15. <Button
  16. android:id="@+id/btn_tran"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="位移渐变" />
  20. <Button
  21. android:id="@+id/btn_rotate"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="旋转渐变" />
  25. <Button
  26. android:id="@+id/btn_set"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="组合渐变" />
  30. <ImageView
  31. android:id="@+id/img_show"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_gravity="center"
  35. android:layout_marginTop="48dp"
  36. android:src="@mipmap/img_face" />
  37. </LinearLayout>

好哒,接着到我们的MainActivity.java,同样非常简单,只需调用AnimationUtils.loadAnimation()
加载动画,然后我们的View控件调用startAnimation开启动画即可~

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  2. private Button btn_alpha;
  3. private Button btn_scale;
  4. private Button btn_tran;
  5. private Button btn_rotate;
  6. private Button btn_set;
  7. private ImageView img_show;
  8. private Animation animation = null;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. bindViews();
  14. }
  15. private void bindViews() {
  16. btn_alpha = (Button) findViewById(R.id.btn_alpha);
  17. btn_scale = (Button) findViewById(R.id.btn_scale);
  18. btn_tran = (Button) findViewById(R.id.btn_tran);
  19. btn_rotate = (Button) findViewById(R.id.btn_rotate);
  20. btn_set = (Button) findViewById(R.id.btn_set);
  21. img_show = (ImageView) findViewById(R.id.img_show);
  22. btn_alpha.setOnClickListener(this);
  23. btn_scale.setOnClickListener(this);
  24. btn_tran.setOnClickListener(this);
  25. btn_rotate.setOnClickListener(this);
  26. btn_set.setOnClickListener(this);
  27. }
  28. @Override
  29. public void onClick(View v) {
  30. switch (v.getId()){
  31. case R.id.btn_alpha:
  32. animation = AnimationUtils.loadAnimation(this,
  33. R.anim.anim_alpha);
  34. img_show.startAnimation(animation);
  35. break;
  36. case R.id.btn_scale:
  37. animation = AnimationUtils.loadAnimation(this,
  38. R.anim.anim_scale);
  39. img_show.startAnimation(animation);
  40. break;
  41. case R.id.btn_tran:
  42. animation = AnimationUtils.loadAnimation(this,
  43. R.anim.anim_translate);
  44. img_show.startAnimation(animation);
  45. break;
  46. case R.id.btn_rotate:
  47. animation = AnimationUtils.loadAnimation(this,
  48. R.anim.anim_rotate);
  49. img_show.startAnimation(animation);
  50. break;
  51. case R.id.btn_set:
  52. animation = AnimationUtils.loadAnimation(this,
  53. R.anim.anim_set);
  54. img_show.startAnimation(animation);
  55. break;
  56. }
  57. }
  58. }

运行效果图

嘿嘿,有点意思是吧,还不动手试试,改点东西,或者自由组合动画,做出酷炫的效果吧~


4.动画状态的监听

我们可以对动画的执行状态进行监听,调用动画对象的:
setAnimationListener(new AnimationListener())方法,重写下面的三个方法:
onAnimationStart():动画开始
onAnimtaionRepeat():动画重复
onAnimationEnd():动画结束
即可完成动画执行状态的监听~


5.为View动态设置动画效果

先调用AnimationUtils.loadAnimation(动画xml文件),然后View控件调用startAnimation(anim)
开始动画~这是静态加载的方式,当然你也可以直接创建一个动画对象,用Java代码完成设置,再调用
startAnimation开启动画~


6.为Fragment设置过渡动画

这里要注意一点,就是Fragment是使用的v4包还是app包下的Fragment!
我们可以调用FragmentTransaction对象的setTransition(int transit)
为Fragment指定标准的过场动画,transit的可选值如下:
TRANSIT_NONE:无动画
TRANSIT_FRAGMENT_OPEN:打开形式的动画
TRANSIT_FRAGMENT_CLOSE:关闭形式的动画
上面的标准过程动画是两个都可以调用的,而不同的地方则在于自定义转场动画
setCustomAnimations()方法!

  • app包下的Fragment
    setCustomAnimations(int enter, int exit, int popEnter, int popExit)
    分别是添加,移除,入栈,以及出栈时的动画!
    另外要注意一点的是,对应的动画类型是:属性动画(Property),就是动画文件
    的根标签要是:<objectAnimator>,<valueAnimator>或者是前面两者放到一个<set>里;

  • v4包下的Fragment
    v4包下的则支持两种setCustomAnimations()

另外要注意一点的是,对应的动画类型是:补间动画(Tween),和上面的View一样~

可能你会有疑惑,你怎么知道对应的动画类型,其实只要你到Fragment源码那里找下:
onCreateAnimation()方法的一个返回值就知道了:
v4包

app包


7.为Activity设置过场动画

Activty设置过场动画非常简单,调用的方法是:overridePendingTransition(int enterAnim, int exitAnim)
用法很简单:在startActivity(intent)或者finish()后添加
参数依次是:新Activity进场时的动画,以及旧Activity退场时的动画
下面提供几种比较简单而且常用的过场动画供大家使用~

下载传送门:Activity常用过渡动画.zip


8.写个进入APP后登陆注册按钮从底部弹出动画效果的例子:

运行效果图

代码实现

首先是我们的布局文件: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. android:background="#DDE2E3"
  6. tools:context=".MainActivity">
  7. <LinearLayout
  8. android:id="@+id/start_ctrl"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentBottom="true"
  12. android:orientation="vertical"
  13. android:visibility="gone">
  14. <Button
  15. android:id="@+id/start_login"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:background="#F26968"
  19. android:gravity="center"
  20. android:paddingBottom="15dp"
  21. android:paddingTop="15dp"
  22. android:text="登陆"
  23. android:textColor="#FFFFFF"
  24. android:textSize="18sp" />
  25. <Button
  26. android:id="@+id/start_register"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:background="#323339"
  30. android:gravity="center"
  31. android:paddingBottom="15dp"
  32. android:paddingTop="15dp"
  33. android:text="注册"
  34. android:textColor="#FFFFFF"
  35. android:textSize="18sp" />
  36. </LinearLayout>
  37. </RelativeLayout>

接着是MainActivity.java

  1. public class MainActivity extends AppCompatActivity {
  2. private LinearLayout start_ctrl;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. start_ctrl = (LinearLayout) findViewById(R.id.start_ctrl);
  8. //设置动画,从自身位置的最下端向上滑动了自身的高度,持续时间为500ms
  9. final TranslateAnimation ctrlAnimation = new TranslateAnimation(
  10. TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
  11. TranslateAnimation.RELATIVE_TO_SELF, 1, TranslateAnimation.RELATIVE_TO_SELF, 0);
  12. ctrlAnimation.setDuration(500l); //设置动画的过渡时间
  13. start_ctrl.postDelayed(new Runnable() {
  14. @Override
  15. public void run() {
  16. start_ctrl.setVisibility(View.VISIBLE);
  17. start_ctrl.startAnimation(ctrlAnimation);
  18. }
  19. }, 2000);
  20. }
  21. }

注释写得很清楚了,这里就不BB解释了,如果你对TranslateAnimation.RELATIVE_TO_SELF这个有疑惑,
请自己谷歌或者百度,限于篇幅(我懒),这里就不写了,蛮简单的~


9.本节代码示例下载

AnimationDemo3.zip
AnimationDemo4.zip


本节小结:

本节给大家细细地讲解了下Android中的第二种动画(渐变动画),四种动画的详解,以及
设置动画监听器,还有如何为View,Fragment和Activity设置动画,最后还写了一个进入后
从APP底部弹出登陆按钮和注册按钮的例子,篇幅可能有点长,不过都非常容易理解,相信
大家看完都能够收获满满~!好的,本节就到这里,谢谢~

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注