1.Tweened Animation 渐变动画
该类提供了 旋转,移动,淡入淡出,缩放
2.Frame-by-Frame Animation
和放电影一样 通过一系列的图片 按照我们制定的动作 显示出来
Tweened Animation:
1.Alpha:淡入淡出效果
2.Scale:缩放效果
3.Rotate:旋转效果
4.Translate:移动效果
Tweened Animation 渐变动画
AlphaAmination:淡入淡出
Public Constructors:
Constructor used when an AlphaAnimation is loaded from a resource.
AlphaAnimation(float fromAlpha, float toAlpha)
Constructor to use when building an AlphaAnimation from code
Public Methods:
willChangeBounds() :返回值boolean
Indicates whether or not this animation will affect the bounds of the animated view.
willChangeTransformationMatrix() :返回值 boolean
Indicates whether or not this animation will affect the transformation matrix.
Protected Methods
applyTransformation(float interpolatedTime,
Transformation t) 无返回值
Changes the alpha property of the supplied
Transformation
使用
代码方式使用该效果:
// AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
// alphaAnimation.setDuration(1000);
// arg0.startAnimation(alphaAnimation);
布局形式使用该效果:
arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha));
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
</alpha>
RotateAnimation:旋转动画效果
实现控件的旋转效果;
Public Constructors:
Constructor used when a RotateAnimation is loaded from a resource.
RotateAnimation(float fromDegrees, float toDegrees) 直接构造RotateAnimation对象 中心点为默认的控件de( fromDegrees, toDegrees) 做旋转
Constructor to use when building a RotateAnimation from code.
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) 直接构造函数 以自定义的( pivotX, pivotY)做旋转
Constructor to use when building a RotateAnimation from code
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 直接构造 RotateAnimation 以某一个事物为参考 做旋转
Constructor to use when building a RotateAnimation from code
Public Methods
initialize(int width, int height, int parentWidth, int parentHeight) 无返回值
Initialize this animation with the dimensions of the object being animated as well as the objects parents.
将初始化动画组件及其父容器的宽高;通常亦可进行另外的初始化工作
Ptotected Methods
applyTransformation(float interpolatedTime,
Transformation t) 无返回值
Helper for getTransformation.
第一个参数为动画的进度时间值,取值范围为[0.0f,1.0f],第二个参数Transformation记录着动画某一帧中变形的原始数据。该方法在动画的每一帧显示过程中都会被调用。
代码实现效果:
RotateAnimation rotateAnimation = new RotateAnimation(0, 360); 以默认值为控件左上角为中心点 做旋转
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 100, 200);//以我们自定义的一个点 作为中心点
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(3000);
arg0.startAnimation(rotateAnimation);
布局文件实现效果:
arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate));
布局文件: 注意属性: android:pivotX android:pivotY 如果去百分比则以自身作为参考点 以数值则不是
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="3000"
android:pivotX="50%"
android:pivotY="50%">
</rotate
TranslateAnimation:移动动画效果
实现控件的移动效果;
Public Constructors
Constructor used when a TranslateAnimation is loaded from a resource.
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 从给定的数值
Constructor to use when building a TranslateAnimation from code
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 相对于某一个控件 把他当做参考系
Constructor to use when building a TranslateAnimation from code
Public Methods:
initialize(int width, int height, int parentWidth, int parentHeight)
Initialize this animation with the dimensions of the object being animated as well as the objects parents.
Protected Methods
applyTransformation(float interpolatedTime,
Transformation t)
Helper for getTransformation.
代码实现:
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 400);//相对于自身的
translateAnimation.setDuration(3000);
arg0.startAnimation(translateAnimation);
布局文件实现:
arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.tranlate));
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="3000" xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
ScaleAnimation 缩放效果
缩放动画效果:
Public Constructions:
Constructor used when a ScaleAnimation is loaded from a resource.
ScaleAnimation(float fromX, float toX, float fromY, float toY) 各个参数都是相对于控件的倍数 默认从控件的左上角(0,0)开始缩放 前两个参数为X轴方向 后两个参数为Y轴方向
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) 各个参数都是相对于控件的倍数 从( pivotX, pivotY)开始缩放
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 各个参数都是相对于控件的倍数 需要选择参考系
Constructor to use when building a ScaleAnimation from code
Public Methods;
initialize(int width, int height, int parentWidth, int parentHeight)
Initialize this animation with the dimensions of the object being animated as well as the objects parents.
Protected Methods:
applyTransformation(float interpolatedTime,
Transformation t)
Helper for getTransformation.
代码实现:
ScaleAnimation scaleAnimation = new ScaleAnimation(0, (float)1.5, 0, (float)1.5);默认从左上角缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(0, (float)1.5, 0, (float)1.5, 50,50);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
arg0.startAnimation(scaleAnimation);
布局实现:
arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale));
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:duration="3000" >
</scale>
TranslateAnimation 移动效果
Public Constructions;
Constructor used when a TranslateAnimation is loaded from a resource.
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 在X Y轴相对于控件的当前位置进行偏移
Constructor to use when building a TranslateAnimation from code
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 有指定参考系
Constructor to use when building a TranslateAnimation from code
Public Methods:
initialize(int width, int height, int parentWidth, int parentHeight)
Initialize this animation with the dimensions of the object being animated as well as the objects parents.
Protected Methods:
applyTransformation(float interpolatedTime,
Transformation t)
Helper for getTransformation.
代码实现:
TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 0);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 50f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
translateAnimation.setDuration(3000);
arg0.startAnimation(translateAnimation);
综上总结:这四个效果在实现上是一样的。他们都有一些共同的属性:如Duration 持续时间 interpolator(:动画渲染器 如动画加速器,动画减速器,动画加速减速器) 。。。
如果要是动画可以循环执行那可以设置 setRepeatCount(Animation.INFINITE)
他们也有事件的监听 需要实现AnimationListener接口
onAnimationStart onAnimationEnd onAnimationRepeat
动画的混合效果:
就是使用一个容器将 这些效果装载起来 然后一起给控件使用
用AnimationSet创建对象animationSet 使用animationSet.addAnimation()往容器中添加效果 然后将这个容器给控件使用
自定义动画效果:
由上面四个效果我们知道,那四种类都是继承于Animation.由此,我们可以设想,如果自定义一个类去继承Animation,那么就可以实现自己想要的动画效果了!
创建一个类继承Animation 然后去重写initialize,applyTransformation两个方法
其中initialize:初始化
applyTransformation:实现效果
在绘制动画的过程中会反复的调用applyTransformation函数,每次调用参数interpolatedTime值都会变化,该参数从0渐 变为1,当该参数为1时表明动画结束。通过参数Transformation 来获取变换的矩阵(matrix),通过改变矩阵就可以实现各种复杂的效