xml或者代码实现Animation

前端之家收集整理的这篇文章主要介绍了xml或者代码实现Animation前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@ 1.Tweened Animation 渐变动画
@H_502_0@ 该类提供了 旋转,移动,淡入淡出,缩放
@H_502_0@
@H_502_0@ 2.Frame-by-Frame Animation
@H_502_0@ 和放电影一样 通过一系列的图片 按照我们制定的动作 显示出来
@H_502_0@
@H_502_0@ Tweened Animation:
@H_502_0@ 1.Alpha:淡入淡出效果 @H_502_0@ 2.Scale:缩放效果 @H_502_0@ 3.Rotate:旋转效果 @H_502_0@ 4.Translate:移动效果 @H_502_0@
@H_502_0@
@H_502_0@ Tweened Animation 渐变动画 @H_502_0@
@H_502_0@
@H_502_0@ AlphaAmination:淡入淡出 @H_502_0@
@H_502_0@ Public Constructors: @H_502_0@
AlphaAnimation( Contextcontext,AttributeSetattrs)
Constructor used when an AlphaAnimation is loaded from a resource.
@H_502_0@
@H_502_0@
AlphaAnimation(float fromAlpha,float toAlpha)
Constructor to use when building an AlphaAnimation from code
@H_502_0@
@H_502_0@ Public Methods: @H_502_0@
willChangeBounds() :返回值boolean

Indicates whether or not this animation will affect the bounds of the animated view.


@H_502_0@
@H_502_0@
willChangeTransformationMatrix() :返回值 boolean

Indicates whether or not this animation will affect the transformation matrix.


@H_502_0@
@H_502_0@ Protected Methods @H_502_0@
@H_502_0@
applyTransformation(float interpolatedTime,Transformationt) 无返回值
Changes the alpha property of the supplied Transformation
使用
@H_502_0@
@H_502_0@ 代码方式使用该效果: @H_502_0@ // AlphaAnimation alphaAnimation = new AlphaAnimation(0,1); @H_502_0@ // alphaAnimation.setDuration(1000); @H_502_0@ // arg0.startAnimation(alphaAnimation); @H_502_0@
@H_502_0@ 布局形式使用该效果: @H_502_0@ arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha)); @H_502_0@ 布局文件: @H_502_0@ <?xml version="1.0" encoding="utf-8"?> @H_502_0@ <alpha xmlns:android="http://schemas.android.com/apk/res/android" @H_502_0@ android:fromAlpha="0" @H_502_0@ android:toAlpha="1" @H_502_0@ android:duration="1000"> @H_502_0@ </alpha> @H_502_0@
@H_502_0@ RotateAnimation:旋转动画效果 @H_502_0@
@H_502_0@ 实现控件的旋转效果; @H_502_0@
@H_502_0@ Public Constructors: @H_502_0@
@H_502_0@
RotateAnimation( AttributeSetattrs) :使用资源文件来构造RotateAnimation对象
Constructor used when a RotateAnimation is loaded from a resource.
@H_502_0@
@H_502_0@
RotateAnimation(float fromDegrees,float toDegrees) 直接构造RotateAnimation对象 中心点为默认的控件de(fromDegrees,toDegrees) 做旋转
Constructor to use when building a RotateAnimation from code.
@H_502_0@
@H_502_0@
函数 以自定义的(pivotX,pivotY)做旋转
Constructor to use when building a RotateAnimation from code
@H_502_0@
@H_502_0@
RotateAnimation(float fromDegrees,int pivotXType,float pivotXValue,int pivotYType,float pivotYValue) 直接构造 RotateAnimation 以某一个事物为参考 做旋转
Constructor to use when building a RotateAnimation from code
@H_502_0@
@H_502_0@ Public Methods @H_502_0@
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.
将初始化动画组件及其父容器的宽高;通常亦可进行另外的初始化工作
@H_502_0@
@H_502_0@ Ptotected Methods @H_502_0@
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,Animation.RELATIVE_TO_SELF,0.5f,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
TranslateAnimation( AttributeSetattrs) 使用资源文件
Constructor used when a TranslateAnimation is loaded from a resource.
@H_502_0@
@H_502_0@
TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float toYDelta) 从给定的数值
Constructor to use when building a TranslateAnimation from code
@H_502_0@
@H_502_0@
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
@H_502_0@
@H_502_0@ Public Methods: @H_502_0@
Initialize this animation with the dimensions of the object being animated as well as the objects parents.

Protected Methods
Transformationt)
Helper for getTransformation.
@H_502_0@
@H_502_0@ 代码实现: @H_502_0@ TranslateAnimation translateAnimation = new TranslateAnimation(0,200,400);//相对于自身的 @H_502_0@ translateAnimation.setDuration(3000); @H_502_0@ arg0.startAnimation(translateAnimation); @H_502_0@ @H_502_0@ 布局文件实现: @H_502_0@ arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.tranlate)); @H_502_0@ 布局文件: @H_502_0@ <?xml version="1.0" encoding="utf-8"?> @H_502_0@ <translate @H_502_0@ android:fromXDelta="0" @H_502_0@ android:toXDelta="200" @H_502_0@ android:fromYDelta="0" @H_502_0@ android:toYDelta="200" @H_502_0@ android:duration="3000" xmlns:android="http://schemas.android.com/apk/res/android"> @H_502_0@ </translate> @H_502_0@
@H_502_0@
@H_502_0@ ScaleAnimation 缩放效果 @H_502_0@ 缩放动画效果: @H_502_0@ Public Constructions: @H_502_0@
ScaleAnimation( AttributeSetattrs) :从资源中创建对象
Constructor used when a ScaleAnimation is loaded from a resource.
@H_502_0@
@H_502_0@
ScaleAnimation(float fromX,float toX,float fromY,float toY) 各个参数都是相对于控件的倍数 默认从控件的左上角(0,0)开始缩放 前两个参数为X轴方向 后两个参数为Y轴方向
Constructor to use when building a ScaleAnimation from code
@H_502_0@
@H_502_0@
ScaleAnimation(float fromX,float toY,float pivotY) 各个参数都是相对于控件的倍数 从(pivotX,pivotY)开始缩放
Constructor to use when building a ScaleAnimation from code
@H_502_0@
@H_502_0@
@H_502_0@ Public Methods; @H_502_0@
initialize(int width,247)"> Initialize this animation with the dimensions of the object being animated as well as the objects parents.
@H_502_0@
@H_502_0@ Protected Methods: @H_502_0@
Helper for getTransformation.

代码实现:
ScaleAnimation scaleAnimation = new ScaleAnimation(0,(float)1.5,(float)1.5);////默认从左上角缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(0,50,50);
ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,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;
TranslateAnimation(float fromXDelta,float toYDelta) 在X Y轴相对于控件的当前位置进行偏移
Helper for getTransformation.
@H_502_0@
@H_502_0@ 代码实现: @H_502_0@ TranslateAnimation translateAnimation = new TranslateAnimation(0,0); @H_502_0@ TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,50f,0f); @H_502_0@ translateAnimation.setDuration(3000); @H_502_0@ arg0.startAnimation(translateAnimation); @H_502_0@
@H_502_0@
@H_502_0@ 综上总结:这四个效果在实现上是一样的。他们都有一些共同的属性:如Duration 持续时间 interpolator(:动画渲染器 如动画加速器,动画减速器,动画加速减速器) 。。。 @H_502_0@ 如果要是动画可以循环执行那可以设置 setRepeatCount(Animation.INFINITE) @H_502_0@ 他们也有事件的监听 需要实现AnimationListener接口 @H_502_0@ onAnimationStart onAnimationEnd onAnimationRepeat @H_502_0@
@H_502_0@ 动画的混合效果 @H_502_0@
@H_502_0@ 就是使用一个容器将 这些效果装载起来 然后一起给控件使用 @H_502_0@
@H_502_0@ 用AnimationSet创建对象animationSet 使用animationSet.addAnimation()往容器中添加效果 然后将这个容器给控件使用 @H_502_0@
@H_502_0@
@H_502_0@ 自定义动画效果 @H_502_0@ 由上面四个效果我们知道,那四种类都是继承于Animation.由此,我们可以设想,如果自定义一个类去继承Animation,那么就可以实现自己想要的动画效果了! @H_502_0@ 创建一个类继承Animation 然后去重写initialize,applyTransformation两个方法 @H_502_0@ 其中initialize:初始化 @H_502_0@ applyTransformation:实现效果 @H_502_0@
@H_502_0@ 在绘制动画的过程中会反复的调用applyTransformation函数,每次调用参数interpolatedTime值都会变化,该参数从0渐变为1,当该参数为1时表明动画结束。通过参数Transformation来获取变换的矩阵(matrix),通过改变矩阵就可以实现各种复杂的效

猜你在找的XML相关文章