你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中的动画详解系列

自定义动画研究

[日期:2014-08-03] 来源:Linux社区  作者:dawanganban [字体:大 中 小]

在上一篇中我们使用到了位移动画TranslateAnimation,下面我们先来看看TranslateAnimation是如何实现Animation中的抽象方法的:

  1. /*
  2. * Copyright (C) 2006 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.view.animation;
  17. import android.content.Context;
  18. import android.content.res.TypedArray;
  19. import android.util.AttributeSet;
  20. /**
  21. * An animation that controls the position of an object. See the
  22. * {@link android.view.animation full package} description for details and
  23. * sample code.
  24. *
  25. */
  26. publicclass TranslateAnimation extends Animation {
  27. privateint mFromXType = ABSOLUTE;
  28. privateint mToXType = ABSOLUTE;
  29. privateint mFromYType = ABSOLUTE;
  30. privateint mToYType = ABSOLUTE;
  31. privatefloat mFromXValue = 0.0f;
  32. privatefloat mToXValue = 0.0f;
  33. privatefloat mFromYValue = 0.0f;
  34. privatefloat mToYValue = 0.0f;
  35. privatefloat mFromXDelta;
  36. privatefloat mToXDelta;
  37. privatefloat mFromYDelta;
  38. privatefloat mToYDelta;
  39. /**
  40. * Constructor used when a TranslateAnimation is loaded from a resource.
  41. *
  42. * @param context Application context to use
  43. * @param attrs Attribute set from which to read values
  44. */
  45. public TranslateAnimation(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. TypedArray a = context.obtainStyledAttributes(attrs,
  48. com.android.internal.R.styleable.TranslateAnimation);
  49. Description d = Description.parseValue(a.peekValue(
  50. com.android.internal.R.styleable.TranslateAnimation_fromXDelta));
  51. mFromXType = d.type;
  52. mFromXValue = d.value;
  53. d = Description.parseValue(a.peekValue(
  54. com.android.internal.R.styleable.TranslateAnimation_toXDelta));
  55. mToXType = d.type;
  56. mToXValue = d.value;
  57. d = Description.parseValue(a.peekValue(
  58. com.android.internal.R.styleable.TranslateAnimation_fromYDelta));
  59. mFromYType = d.type;
  60. mFromYValue = d.value;
  61. d = Description.parseValue(a.peekValue(
  62. com.android.internal.R.styleable.TranslateAnimation_toYDelta));
  63. mToYType = d.type;
  64. mToYValue = d.value;
  65. a.recycle();
  66. }
  67. /**
  68. * Constructor to use when building a TranslateAnimation from code
  69. *
  70. * @param fromXDelta Change in X coordinate to apply at the start of the
  71. * animation
  72. * @param toXDelta Change in X coordinate to apply at the end of the
  73. * animation
  74. * @param fromYDelta Change in Y coordinate to apply at the start of the
  75. * animation
  76. * @param toYDelta Change in Y coordinate to apply at the end of the
  77. * animation
  78. */
  79. public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
  80. mFromXValue = fromXDelta;
  81. mToXValue = toXDelta;
  82. mFromYValue = fromYDelta;
  83. mToYValue = toYDelta;
  84. mFromXType = ABSOLUTE;
  85. mToXType = ABSOLUTE;
  86. mFromYType = ABSOLUTE;
  87. mToYType = ABSOLUTE;
  88. }
  89. /**
  90. * Constructor to use when building a TranslateAnimation from code
  91. *
  92. * @param fromXType Specifies how fromXValue should be interpreted. One of
  93. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  94. * Animation.RELATIVE_TO_PARENT.
  95. * @param fromXValue Change in X coordinate to apply at the start of the
  96. * animation. This value can either be an absolute number if fromXType
  97. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  98. * @param toXType Specifies how toXValue should be interpreted. One of
  99. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  100. * Animation.RELATIVE_TO_PARENT.
  101. * @param toXValue Change in X coordinate to apply at the end of the
  102. * animation. This value can either be an absolute number if toXType
  103. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  104. * @param fromYType Specifies how fromYValue should be interpreted. One of
  105. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  106. * Animation.RELATIVE_TO_PARENT.
  107. * @param fromYValue Change in Y coordinate to apply at the start of the
  108. * animation. This value can either be an absolute number if fromYType
  109. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  110. * @param toYType Specifies how toYValue should be interpreted. One of
  111. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  112. * Animation.RELATIVE_TO_PARENT.
  113. * @param toYValue Change in Y coordinate to apply at the end of the
  114. * animation. This value can either be an absolute number if toYType
  115. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  116. */
  117. public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
  118. int fromYType, float fromYValue, int toYType, float toYValue) {
  119. mFromXValue = fromXValue;
  120. mToXValue = toXValue;
  121. mFromYValue = fromYValue;
  122. mToYValue = toYValue;
  123. mFromXType = fromXType;
  124. mToXType = toXType;
  125. mFromYType = fromYType;
  126. mToYType = toYType;
  127. }
  128. @Override
  129. protectedvoid applyTransformation(float interpolatedTime, Transformation t) {
  130. float dx = mFromXDelta;
  131. float dy = mFromYDelta;
  132. if (mFromXDelta != mToXDelta) {
  133. dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
  134. }
  135. if (mFromYDelta != mToYDelta) {
  136. dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
  137. }
  138. t.getMatrix().setTranslate(dx, dy);
  139. }
  140. @Override
  141. publicvoid initialize(int width, int height, int parentWidth, int parentHeight) {
  142. super.initialize(width, height, parentWidth, parentHeight);
  143. mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth);
  144. mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth);
  145. mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight);
  146. mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight);
  147. }

可以看到实际上重写了两个方法:initialize和applyTransformation

  1. /**
  2. * Initialize this animation with the dimensions of the object being
  3. * animated as well as the objects parents. (This is to support animation
  4. * sizes being specifed relative to these dimensions.)
  5. *
  6. * <p>Objects that interpret Animations should call this method when
  7. * the sizes of the object being animated and its parent are known, and
  8. * before calling {@link #getTransformation}.
  9. *
  10. *
  11. * @param width Width of the object being animated
  12. * @param height Height of the object being animated
  13. * @param parentWidth Width of the animated object's parent
  14. * @param parentHeight Height of the animated object's parent
  15. */
  16. publicvoid initialize(int width, int height, int parentWidth, int parentHeight) {
  17. reset();
  18. mInitialized = true;
  19. }
 
  1. /**
  2. * Helper for getTransformation. Subclasses should implement this to apply
  3. * their transforms given an interpolation value. Implementations of this
  4. * method should always replace the specified Transformation or document
  5. * they are doing otherwise.
  6. *
  7. * @param interpolatedTime The value of the normalized time (0.0 to 1.0)
  8. * after it has been run through the interpolation function.
  9. * @param t The Transofrmation object to fill in with the current
  10. * transforms.
  11. */
  12. protectedvoid applyTransformation(float interpolatedTime, Transformation t) {
  13. }

从initialize方法的注释上看,这个方法的主要作用是��初始化对象的尺寸以及父容器尺寸(为了确定和父容器的相对位置)。

 

从applyTransformation方法的注释上看,这个方法应该被实现,这个方法给出了一个interpolation值,并帮助获得Transformation对象,应该自己设置transformation对象来实现自己的动画效果。

好吧,基于以上研究,我们试着来重写一个Animation

  1. package com.example.testanimation;
  2. import android.graphics.Camera;
  3. import android.graphics.Matrix;
  4. import android.view.animation.Animation;
  5. import android.view.animation.LinearInterpolator;
  6. import android.view.animation.Transformation;
  7. /**
  8. * 自定义动画
  9. * @author 阳光小强
  10. *
  11. */
  12. publicclass MyAnimation extends Animation{
  13. privatefloat centerX;
  14. privatefloat centerY;
  15. privateint duration;
  16. private Camera camera = new Camera();
  17. public MyAnimation(float x, float y, int duration){
  18. this.centerX = x;
  19. this.centerY = y;
  20. this.duration = duration;
  21. }
  22. @Override
  23. publicvoid initialize(int width, int height, int parentWidth,
  24. int parentHeight) {
  25. super.initialize(width, height, parentWidth, parentHeight);
  26. setDuration(duration);
  27. setFillAfter(true);
  28. setInterpolator(new LinearInterpolator());
  29. }
  30. @Override
  31. protectedvoid applyTransformation(float interpolatedTime, Transformation t) {
  32. camera.save();
  33. camera.translate(100f - 100f * interpolatedTime, 150f * interpolatedTime -150, 80f - 80f * interpolatedTime);
  34. camera.rotateY(360 * interpolatedTime);
  35. camera.rotateX(360 * interpolatedTime);
  36. Matrix matrix = t.getMatrix();
  37. camera.getMatrix(matrix);
  38. matrix.preTranslate(-centerX, -centerY);
  39. matrix.postTranslate(centerX, centerY);
  40. camera.restore();
  41. }
  42. }

代码解释:

1、setDuration(duration):设置动画时间

2、setFillAfter(true):设置动画结束后保持,如果设为false则动画结束后回到原来状态。

3、setInterpolator(new LinearInterpolator()):Interpolater实际上是控制补间动画的插入帧的频率的,所以就会有加速、减速、匀速动画。

4、Camera:一个空间变换工具,类似于Matrix,提供了各种变换方法,如上面的translate和rotateY等。

5、Matrix:一个三维的矩阵变换函数。

6、camera.getMatrix(matrix):计算当前的矩阵变换,并将其复制到矩阵matrix中。

7、matrix.preTranslate :执行矩阵指定的转换。

8、matrix.postTranslate:执行矩阵指定的转换(后面两个方法怎么转换,有什么区别,这就是数学知识了,一两句也说不清)。

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-08/105023p4.htm

linux
【内容导航】
第1页:逐帧动画 第2页:飞舞的蝴蝶
第3页:自定义动画研究 第4页:Activity之间切换动画
相关资讯       Android动画详解 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款