您的位置:首页 > 移动开发 > Android开发

TweenAnimation 补间动画

2016-01-21 17:03 531 查看


这里有几篇比较好的相关文章:

Animation 动画详解(一)——alpha、scale、translate、rotate、set的xml属性及用法 


Animation动画详解(二)——Interpolator插值器 


Animation动画详解(三)—— 代码生成alpha、scale、translate、rotate、set及插值器动画



有4种动画       TranslateAnimation 、 ScaleAnimation、AlphaAnimation、RotateAnimation

一个动画集合 AnimationSet

多种Interpolator  插补器

一个工具类AnimationUtils   可以将xml定义的动画加载到java代码中

两种方式定义补间动画:

1、java代码中 创建动画对象  并设置参数

2、在res/anim/中定义  xml的动画

java代码中创建动画:

1)平移动画

TranslateAnimation ta = new TranslateAnimation(0, 200, 50, 50);
//有两种构造方法:
// 1、  TranslateAnimation ta = new TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float toYDelta);
//delta    希腊字母, 其大写为Δ,小写为δ。 在数学或者物理中大写的Δ用来表示增量符号。在原来的基础上的偏移
//例如:该view 的原始位置是X= 100 ; Y= 100;  那上面的值 相对于屏幕的值就是  100+0;100+200;100 +50;100+50;

/*
TranslateAnimation ta = new TranslateAnimation(int fromXType ,float fromXValue,int toXType ,float toXValue,
int fromYType ,float fromYValue, int toYType ,float toYValue);
fromXType 的值有 Animation.RELATIVE_TO_PARENT   和 Animation.RELATIVE_TO_SELF
以自己  的原始  坐标点  加上 多少倍的   自己或者父控件 宽或者 高

例如: view 的 原始坐标为  x =100  y=100   高=200  宽 = 300    父控件的  宽是  600     高  是800
如果fromXType 为 Animation.RELATIVE_TO_SELF
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF ,0.3f ,Animation.RELATIVE_TO_SELF,1f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,2f);
动画起始位置x = 100 + 0.3() * 300(宽)      y = 100+ 0.5*200(高);
动画的终止位置        x =100 +1 * 300     y = 100 + 2 *200

如果fromXType 为 Animation.RELATIVE_TO_PARENT    那就这里的宽高都换成 父控件的值
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT ,0.3f ,Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,2f);
动画起始位置x = 100 + 0.3 * 600(宽)      y = 100+ 0.5*800(高);
动画的终止位置        x =100 +1 * 600     y = 100 + 2 *800
*/
// 总结:上面两种方法都是 相对应与其实位置的坐标  加上偏移值  获得新的坐标值,
// 前一个是移动具体的像素  后一个是移动多少倍个自己尺寸或者父控件的尺寸
ta.setDuration(2000);  //1次动画持续的时间
ta.setRepeatCount(1);  //动画播放的次数
ta.setRepeatMode(Animation.REVERSE);  //重复的动画是和原先的动画反着的
ta.setFillAfter(true);//就是view最最终显示在最后结束动画的地方
// ta.setFillBefore(true);//就是view最最终显示在开始动画的地方
ta.setStartOffset(2000); //开始后 延时几毫秒  开始动画
ta.setInterpolator(new BounceInterpolator()); //添加一个 插补器
//插补器  就是以时间为单位   控制动画的位置     以便动画不是匀速的移动
ivJack.startAnimation(ta);  //开始动画


2)缩放动画

/*
以view的中心点 为中心进行缩放  宽度 10 变成50   高10 变成 50
public ScaleAnimation(float fromX, float toX, float fromY, float toY);
public ScaleAnimation(10, 50, 10, 50);
//下面的方法  就是设置缩放的的中点   和translateAnimation差不多
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
float pivotXValue, float pivotYValue);
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);
*/

3)旋转动画

/*
有三个构造方法:
以该view的中心为 原点进行旋转    从多少度旋转到多少度
public RotateAnimation(float fromDegrees, float toDegrees);

以X + pivotX  ; Y + pivotY为旋转的圆心
public RotateAnimation(float fromDegrees, float toDegrees,int pivotX,int pivotY);

以 X + 多少倍的自身或者父控件的宽 ; Y+ 多少倍的自身或者父控件的高  为旋转的圆心
public RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);
*/
RotateAnimation ra = new RotateAnimation(0f,180f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(2000);
ra.setFillAfter(true);
ivJack.startAnimation(ra);

4)透明度动画Alpha

5) 动画集合

AnimationSet  set  = new AnimationSet(false);//几个动画不共享同一个插补器
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);
ivJack.startAnimation(set);

该动画集合有一个特点: 集合中的动画是一起播放的 ,如果想要 有序的播放就要 给每个动画设置 延时播放就是setStartOffset()  就可以了。



第二种方式  在XML文件中创建动画

1)创建 xml动画文件





\res\anim\scale_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="2"
android:fromYScale="0.5"
android:toYScale="2"
android:duration="4000"
android:interpolator="@android:interpolator/bounce"
android:fillAfter="true"
android:repeatCount="1"
android:repeatMode="reverse"
android:pivotX="30%p"
android:pivotY="30%p"
android:startOffset="1000"
>
</scale>


2)java代码中加载

ScaleAnimation sa = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale_animation);
ivJack.startAnimation(sa);





当然通过XML  也可以创建其他动画 :
       TranslateAnimation 、 ScaleAnimation、AlphaAnimation、RotateAnimation  、[b]AnimationSet、LayoutAnimation(布局动画),[/b]Interpolator



AnimationUtils.loadAnimation(context,resID); 将xml文件 加载为动画
AnimationUtils.loadInterpolator(context, resID); 将xml文件 加载为插补器
AnimationUtils.loadLayoutAnimation(context,resID); 将xml文件 加载为布局动画


两种方式效果是一样的,个人认为XML 文件的形式跟方便!

 学习补间动画 关键是搞懂 那些参数如何设置       使用很简单。

[b]补间动画是不断通过onDraw()方法来绘制 view  使我们看到有动画的效果  , 事件上他一直在“原地没有动过”,设置点击事件就可以观察过结果了。[/b]

实际开发中 还是建议多用属性动画   他是真实的改变了view的位置 ,改变了view的相关属性------所以叫属性动画。

只要View提供了set  和get方法 的属性,  都可以通过属性动画来改变

性能上 属性动画由于补间动画       样式上也比补间动画多      就是使用不是很方法便

所以 简单的动画   不需要触摸的动画用   补间动画  方便  复杂的动画  使用  属性动画!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 动画