微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml或者代码实现Animation

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,Transformationt) 无返回值
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:

RotateAnimation( AttributeSetattrs) :使用资源文件来构造RotateAnimation对象
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.

degrees,float pivotX,float pivotY) 直接构造函数自定义的(pivotX,pivotY)做旋转
Constructor to use when building a RotateAnimation from code

RotateAnimation(float fromdegrees,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
第一个参数为动画的进度时间值,取值范围为[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
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:

Protected Methods
Helper for getTransformation.

代码实现:
TranslateAnimation translateAnimation = new TranslateAnimation(0,200,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:
ScaleAnimation( AttributeSetattrs) :从资源中创建对象
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 toY,float pivotY) 各个参数都是相对于控件的倍数 从(pivotX,pivotY)开始缩放
Constructor to use when building a ScaleAnimation from code


Public Methods;
initialize(int width,247)"> Initialize this animation with the dimensions of the object being animated as well as the objects parents.

Protected Methods:

代码实现:
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.

代码实现:
TranslateAnimation translateAnimation = new TranslateAnimation(0,0);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,50f,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),通过改变矩阵就可以实现各种复杂的效

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类