欢迎使用CSDN-markdown编辑器

来源:互联网 发布:u盘数据丢失 编辑:程序博客网 时间:2024/06/06 02:00

今天回顾一下Tween Animation中的alpha、scale、translate、rotate、set的用法
一、概述:
Android Animation分为四种:
alpha
渐变透明度动画
scale
渐变尺寸伸缩
translate
平移动画
rotate
旋转动画

二、实现方式:
动画使用方式有两种一种是通过xml方式,一种是在代码中实现
接下来就先说下XML的实现方式。
首先需要在res/anim创建一个xml文件,调用方式为R.anim.xxx
1.alpha 透明度属性

<alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="1.0"    android:toAlpha="0.1"    android:duration="3000"    android:fillBefore="true"></alpha>

android:fromAlpha : 动画开始时,透明度状态,从0.0表示全透明,1.0标识不透明
android:toAlpaha: 动画结束时,透明度状态。
2.scale 尺寸渐变动画

<scale xmlns:android="http://schemas.android.com/apk/res/android"android:fromXScale="0.5"android:toXScale="1.4"android:fromYScale="0.5"android:toYScale="1.4"android:pivotX="50%p"android:pivotY="50%p"android:duration="9000" />    

android:fromXScale : 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
android:toXScale: 结尾的X方向上相对自身的缩放比例
android:fromYScale:起始的Y方向上相对自身缩放比例
android:toYScale:结尾Y方向上相对自身缩放比例
android:pivotX :起始点X位置(可以有三种模式)
android:pivotY : 起始点Y位置

值得注意的就是pivotX和pivotY这两个属性代表了起始点,而表示方式有三种
第一种是android:pivotX:50 这种表示方式代表 坐标原点的基础上XY轴加上50px的距离为坐标原点,常用的也是这种
第二种是android:pivotX:50% 是在自己原点坐标的基础上加上自己宽的的50%
第三种是android:pivotX:50%p 这种表示在原点坐标上加上父空间的50%宽度

3.translate平移动画

<translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXDelta="0"    android:toXDelta="-80"    android:fromYDelta="0"    android:toYDelta="-80"    android:duration="2000"    android:fillBefore="true"></translate>  

android:fromXDelta :起点X轴坐标,同样有3种数值表达形式
android:toXDelta :终点X轴坐标
android:fromYDelta :起点Y轴坐标
android:toYDelta:终点Y轴坐标

4.rotate旋转动画

<Rotate xmlns:android="http://schemas.android.com/apk/res/android">    android:fromDegrees="0"    android:toDegrees="-650"    android:pivotX="50%"    android:pivotY="50%"    android:duration="3000"    android:fillAfter="true"></Rotate>

android:fromDegrees 起始旋转角度
android:toDegrees 结束旋转角度
android:pivotX 旋转中心X点位置
android:pivotY 旋转中心Y点位置

5.set 组合动画

<set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="3000"    android:fillAfter="true">    <alpha        android:fromAlpha="0.0"        android:toAlpha="1.0"        />    <scale        android:fromXScale="0.0"        android:fromYScale="0.0"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.4"        android:toYScale="1.4" />    <rotate        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="720" /></set>

set属性可以做动画组合操作

其中还有一些节点属性是从Animation中集成的:
android:duration 动画持续时间
android:fillAfter 如果设置为true,控件动画结束时,将保持动画最后时的状态
android:fileBefore 如果设置为true 控件结束时,还原到动画最初的状态
android:repeatCount 动画的重复次数
android:repeatMode 重复类型,有reverse和restart两种模式。revrse表示倒叙播放,restart从新播放一遍
android:interpolator 设定插补器,其实就是指定的动作效果。

调用方式:

Animation scaleAnimation =AnimationUtils.loadAnimation(this,R.anim.trananim);tv.startAnimation(scaleAnimation);

三、接下来是代码的实现方式
代码继承中同样有一些API是从Animation中集成过来了
setDuration(long) 动画持续时间,以毫秒为单位
setFillAfter(boolean) 如果设置为true,控件动画结束时,将保持动画最后时的状态
setFillBefore(boolean) 如果设置为true,控件动画结束时,还原到开始动画前的状态
setFillEnabled(boolean) 与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
setRepeatCount(int) 重复次数
setRepeatMode(int) 重复类型,有reverse和restart两个值,取值为RESTART或 REVERSE,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
setInterpolator(Interpolator) 设定插值器,其实就是指定的动作效果,比如弹跳效果等

1.alpha透明度动画
通过构造函数了解到,创建Alpha动画时,需要传递起始状态和终止状态值
AlphaAnimation(float fromAlpha,float toAlpha);

        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);        alphaAnimation.setDuration(100);        tv.startAnimation(alphaAnimation);

2.scale 缩放动画
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
ScaleAnimation(float fromX,float toX,float fromY,float toY,int pivaotXType,float pivotXValue,int pivotYType,float pivotValue);
在标签属性中pivoteX中有三种取值,数,百分数,百分数p;提现在构造函数中,就是就是一个构造函数的pivotxType,他的取值有三个,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT。

scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  scaleAnim.setDuration(700);  

3.translate平抑动画

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
构造方法中的的type参数同样有3种取值类型三种取值,数,百分数,百分数p。

translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,           Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);  translateAnim.setDuration(2000);

4.AnimationSet组合动画

alphaAnim = new AlphaAnimation(1.0f,0.1f);  scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  setAnim=new AnimationSet(true);  setAnim.addAnimation(alphaAnim);  setAnim.addAnimation(scaleAnim);  setAnim.addAnimation(rotateAnim); 
1 0
原创粉丝点击