在上一篇中我们使用到了位移动画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.  
  17. package android.view.animation;
  18.  
  19. import android.content.Context;
  20. import android.content.res.TypedArray;
  21. import android.util.AttributeSet;
  22.  
  23. /**
  24. * An animation that controls the position of an object. See the
  25. * {@link android.view.animation full package} description for details and
  26. * sample code.
  27. *
  28. */
  29. public class TranslateAnimation extends Animation {
  30. private int mFromXType = ABSOLUTE;
  31. private int mToXType = ABSOLUTE;
  32.  
  33. private int mFromYType = ABSOLUTE;
  34. private int mToYType = ABSOLUTE;
  35.  
  36. private float mFromXValue = 0.0f;
  37. private float mToXValue = 0.0f;
  38.  
  39. private float mFromYValue = 0.0f;
  40. private float mToYValue = 0.0f;
  41.  
  42. private float mFromXDelta;
  43. private float mToXDelta;
  44. private float mFromYDelta;
  45. private float mToYDelta;
  46.  
  47. /**
  48. * Constructor used when a TranslateAnimation is loaded from a resource.
  49. *
  50. * @param context Application context to use
  51. * @param attrs Attribute set from which to read values
  52. */
  53. public TranslateAnimation(Context context, AttributeSet attrs) {
  54. super(context, attrs);
  55.  
  56. TypedArray a = context.obtainStyledAttributes(attrs,
  57. com.android.internal.R.styleable.TranslateAnimation);
  58.  
  59. Description d = Description.parseValue(a.peekValue(
  60. com.android.internal.R.styleable.TranslateAnimation_fromXDelta));
  61. mFromXType = d.type;
  62. mFromXValue = d.value;
  63.  
  64. d = Description.parseValue(a.peekValue(
  65. com.android.internal.R.styleable.TranslateAnimation_toXDelta));
  66. mToXType = d.type;
  67. mToXValue = d.value;
  68.  
  69. d = Description.parseValue(a.peekValue(
  70. com.android.internal.R.styleable.TranslateAnimation_fromYDelta));
  71. mFromYType = d.type;
  72. mFromYValue = d.value;
  73.  
  74. d = Description.parseValue(a.peekValue(
  75. com.android.internal.R.styleable.TranslateAnimation_toYDelta));
  76. mToYType = d.type;
  77. mToYValue = d.value;
  78.  
  79. a.recycle();
  80. }
  81.  
  82. /**
  83. * Constructor to use when building a TranslateAnimation from code
  84. *
  85. * @param fromXDelta Change in X coordinate to apply at the start of the
  86. * animation
  87. * @param toXDelta Change in X coordinate to apply at the end of the
  88. * animation
  89. * @param fromYDelta Change in Y coordinate to apply at the start of the
  90. * animation
  91. * @param toYDelta Change in Y coordinate to apply at the end of the
  92. * animation
  93. */
  94. public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
  95. mFromXValue = fromXDelta;
  96. mToXValue = toXDelta;
  97. mFromYValue = fromYDelta;
  98. mToYValue = toYDelta;
  99.  
  100. mFromXType = ABSOLUTE;
  101. mToXType = ABSOLUTE;
  102. mFromYType = ABSOLUTE;
  103. mToYType = ABSOLUTE;
  104. }
  105.  
  106. /**
  107. * Constructor to use when building a TranslateAnimation from code
  108. *
  109. * @param fromXType Specifies how fromXValue should be interpreted. One of
  110. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  111. * Animation.RELATIVE_TO_PARENT.
  112. * @param fromXValue Change in X coordinate to apply at the start of the
  113. * animation. This value can either be an absolute number if fromXType
  114. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  115. * @param toXType Specifies how toXValue should be interpreted. One of
  116. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  117. * Animation.RELATIVE_TO_PARENT.
  118. * @param toXValue Change in X coordinate to apply at the end of the
  119. * animation. This value can either be an absolute number if toXType
  120. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  121. * @param fromYType Specifies how fromYValue should be interpreted. One of
  122. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  123. * Animation.RELATIVE_TO_PARENT.
  124. * @param fromYValue Change in Y coordinate to apply at the start of the
  125. * animation. This value can either be an absolute number if fromYType
  126. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  127. * @param toYType Specifies how toYValue should be interpreted. One of
  128. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  129. * Animation.RELATIVE_TO_PARENT.
  130. * @param toYValue Change in Y coordinate to apply at the end of the
  131. * animation. This value can either be an absolute number if toYType
  132. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  133. */
  134. public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
  135. int fromYType, float fromYValue, int toYType, float toYValue) {
  136.  
  137. mFromXValue = fromXValue;
  138. mToXValue = toXValue;
  139. mFromYValue = fromYValue;
  140. mToYValue = toYValue;
  141.  
  142. mFromXType = fromXType;
  143. mToXType = toXType;
  144. mFromYType = fromYType;
  145. mToYType = toYType;
  146. }
  147.  
  148. @Override
  149. protected void applyTransformation(float interpolatedTime, Transformation t) {
  150. float dx = mFromXDelta;
  151. float dy = mFromYDelta;
  152. if (mFromXDelta != mToXDelta) {
  153. dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
  154. }
  155. if (mFromYDelta != mToYDelta) {
  156. dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
  157. }
  158. t.getMatrix().setTranslate(dx, dy);
  159. }
  160.  
  161. @Override
  162. public void initialize(int width, int height, int parentWidth, int parentHeight) {
  163. super.initialize(width, height, parentWidth, parentHeight);
  164. mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth);
  165. mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth);
  166. mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight);
  167. mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight);
  168. }

能够看到实际上重写了两个方法: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. public void 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. protected void applyTransformation(float interpolatedTime, Transformation t) {
  13. }

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

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

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

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

代码解释:

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:运行矩阵指定的转换(后面两个方法怎么转换,有什么差别,这就是数学知识了。一两句也说不清)。

Android中的动画具体解释系列【3】——自己定义动画研究的更多相关文章

  1. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  2. Android中时间戳的详细解释

    Android中时间戳的详细解释: (1).定义: 时间戳就是根据当前系统时间生成的一组随机数字. (2).作用: 作为对数据唯一性的一种判断依据.避免了重复修改数据所带来的错误! (3).应用: ( ...

  3. Android中的动画具体解释系列【2】——飞舞的蝴蝶

    这一篇来使用逐帧动画和补间动画来实现一个小样例,首先我们来看看Android中的补间动画. Android中使用Animation代表抽象的动画类,该类包含以下几个子类: AlphaAnimation ...

  4. Android中AsyncTask使用具体解释

    在Android中我们能够通过Thread+Handler实现多线程通信.一种经典的使用场景是:在新线程中进行耗时操作.当任务完毕后通过Handler向主线程发送Message.这样主线程的Handl ...

  5. Android中的动画具体解释系列【1】——逐帧动画

    逐帧动画事实上非常easy,以下我们来看一个样例: <?xml version="1.0" encoding="utf-8"?> <anima ...

  6. Android中Activity切换时共享视图元素的切换动画(5.0以上)

    同一时候公布在我的博客 点此进入 背景 说来这个的背景很easy,常常在使用图片列表的时候就会想,假设"列表中的图片放大到整个屏幕"作为 Activity 的补间动画.就很完美了. ...

  7. android中OnItemClickListener的参数解释

    @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {} ...

  8. Android中SharedPreferences函数具体解释

    Android平台提供了一个SharedPreferences类,它是一个轻量级应用程序内部轻量级的存储方案,特别适合用于保存软件配置參数,比方boolean,int,float,long,Strin ...

  9. [转]android中drawable资源的解释及例子

    原文链接:         http://blog.csdn.net/wode_dream/article/details/38584693 文章中的内容参考Dev Guide中的Drawable R ...

随机推荐

  1. 项目列表dl、dt、dd使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. SQL数值转字符串保留指定小数位

    IF EXISTS ( SELECT * FROM sysobjects WHERE xtype = 'fn' AND name = 'fn_NumberFormat' ) BEGIN DROP FU ...

  3. 轻松学习Linux之用户账户管理及实例

    Linux用户管理基础 (下载清晰视频:http://down.51cto.com/data/158699) 轻松学习Linux之用户账户管理的实例-跨硬盘移动数据 (此处视频不清楚下按下面地址下载清 ...

  4. JS/CSS 在屏幕底部弹出消息

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  5. C#与C++ DLL的交互

    C#与C++交互,总体来说可以有两种方法: 1.利用C++/CLI作为代理中间层 2.利用PInvoke实现直接调用   第一种方法:实现起来比较简单直观,并且可以实现C#调用C++所写的类,但是问题 ...

  6. JavaScript学习总结(6)——js弹出框、对话框、提示框、弹窗总结

    一.JS的三种最常见的对话框 [javascript] view plaincopy //====================== JS最常用三种弹出对话框 =================== ...

  7. ASP.Net MVC Filter验证用户登录

    一.Filter是什么 ASP.NetMVC模式自带的过滤器Filter,是一种声明式编程方式,支持四种过滤器类型,各自是:Authorization(授权),Action(行为),Result(结果 ...

  8. GestureDetector-onfling不执行

    今天在做计算器的时候,遇到了一个问题,就是当我使用GestureDetector的时候,onFling方法不执行,而其他的可以执行.代码如下 @Override public boolean onDo ...

  9. hbs模板(zmaze ui用的)

    hbs模板(zmaze ui用的) 一.总结 1.模板引擎:就是来生成界面的啊,只不过实现了view和数据分离以及一些其它的功能(预加载等). 2.Handlebars :但他是一个单纯的模板引擎,在 ...

  10. Android 继承framelayout,实现ScrollView 和 HorizontalScrollView 的效果

    有些项目,需要让控件或者布局进行水平和垂直同时能拖拽,当然,ScrollView 和 HorizontalScrollView 的结合写法是一种写法.但是,这么写用户体验效果不佳,会有迟钝感,因此推荐 ...