android 动画 -- tween动画

先看一下android动画框架:
这里写图片描述


tween动画详解:


tween动画可以实现让某个控件展现出旋转,渐变,移动,缩放的一种转换过程,当然,这些单独的动画也可以组合一起来使用,可以打造一些适用的效果。

主要类

  Animation
  AlphaAnimation--渐变透明度
  RotateAnimation --旋转
ScaleAnimation --缩放
TranslateAnimation   --移动
AnimationSet--动画集(组合动画)

实现方法

知道了这些类,那么怎么使用呢?要实现效果有两种方法


  1. 第一种是在创建xml文件,然后在代码中应用到控件上

首先创建xml文件,比如:
<?xml version="1.0" encoding="utf-8"?>
<rotate
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="100%"
android:pivotY="100%"/>

这是一个旋转的xml指定,其中duration表示时间,fromDegress属性表示开始角度,单位:度,todegrees表示结束的角度,单位:度,pivotX表示旋转中心的x坐标,pivotY表示旋转中心的Y坐标,这两个属性有3种表示方式,数字方式表示相对于自身左边缘的像素值,num%表示代表相对于自身左边缘或顶边缘的百分比,num%p表示相对于父容器的左边缘或顶边缘的百分比。


.再来看渐变动画
<alpha>
<android:fromAlpha="float"
android:toAlpha="float"/>

fromAlpha就是开始的alpha值,范围在0.0到1.0之间,分别代表透明和完全不透明,toAlpha技术结束的alpha值,范围也一样


缩放动画
<scale
android:fromXScale="float"
android:fromYScale="float"
android:pivotX="float"
android:pivotY="float"
android:toXScale="float"
android:toXScale="float"/>

fromXScale—开始的x方向相对自身的缩放比例,1.0表示没变化,0.5表示开始 时缩小一倍。
fromYScale—开始的x方向相对自身的缩放比例,1.0表示没变化,0.5表示开始 时缩小一倍。
toXScale—结尾的x方向上相对自身的缩放比例。
toXScale—结尾的x方向上相对自身的缩放比例。
pivotX—缩放的中心点x。
pivotY—缩放的中心点y


位移动画
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100"/>

这4个属性一看就能明白,同样也是前面3种表示方式哦。


另外如果想组合使用,这就要使用动画容器来管理了,比如:
<set
<alpha
...
/>
<scale
...
/>
....
</set>

还有一个插值资源器android:interpolator
系统自带的有:
AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator 动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator 以常量速率改变
OvershootInterpolator 向前甩一定值后再回到原来位置
那么在set里面使用插值器如果属性shareInterPolator为ture表示共享,不设置默认为true,设置为flase相当于不起作用


**xml设置好以后我们就需要在代码中把这个动画应用到控件上了,相应代码:
Animation animation=AnimationUtils.loadAnimation(this,R.anim.ds);
img=(ImageView)findViewById(R.id.imageView);
img.setAnimation(animation);

这样就完成了。


2.在代码中创建动画


首先创建相应的类:

  • AlphaAnimation 类有两个构造方法
    AlphaAnimation(Context context, AttributeSet attrs)—这个可以导入资源文件,也就是之前说的xml。
    AlphaAnimation(float fromAlpha, float toAlpha)—很明显,就是直接设定透明效果。

  • ScaleAnimation类
    ScaleAnimation(Context context, AttributeSet attrs)

    ScaleAnimation(float fromX, float toX, float fromY, float toY)—mPivotXType和mPivotYType为ABSOLUTE,mPivotX和mPivotY为0

    ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)—mPivotXType和mPivotYType为ABSOLUTE

    ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)—pivotXValue缩放中心点的X坐标类型,取值范围为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。pivotYValue缩放中心点的Y坐标类型。

    RELATIVE_TO_PARENT 相对于父控件

    RELATIVE_TO_SELF 相对于自己

    RELATIVE_TO_ABSOLUTE 绝对坐标


  • TranslateAnimation类

    TranslateAnimation(Context context, AttributeSet attrs)

    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)


  • RotateAnimation类(构造方法里面的参数规则跟上面一样)

    RotateAnimation(Context context, AttributeSet attrs)

    RotateAnimation(float fromDegrees, float toDegrees)

    RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

    RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

tween动画(续)

猜你喜欢

转载自blog.csdn.net/snailpeople/article/details/77429687