Android 动画Animation

动画分为视图动画(view animation)和属性动画(property animation),视图动画又分为帧动画和补间动画android

视图动画控件(iv)点击事件(OnClickListener接口)触发位置在原位置ide

1.帧动画(Frame animation)布局

res/drawable/xxx.xml动画

<animation-list ... android:oneshot="true">    //    falsethis

    <item android:drawable="@drawable/..."spa

              android:duration="200"/>    //    显示时间.net

    ...    //    按前后顺序写rest

</animation-list>xml

 

iv.setBackgroundResource(R.drawable.xxx);接口

((AnimationDrawable)iv.getBackground()).start();    //    View类的start()

 

2.补间动画(Tween animation)

分为平移、缩放、透明、旋转和混合

res/anim/xxx.xml

a.平移(TranslateAnimation)

<translate ...

    android:fromXDelta="0"

    android:fromYDelta="0"    //图片起始位置坐标(00为左上角)

    android:toXDelta="500"

    android:toYDelta="500"

    android:duration="2000"    //    5个必要属性

    android:fillAfter="true"       //    保持动画最后那个状态

    android:repeatCount="1"   //    执行两次,"infinite":永久

    android:repeatMode="restart"/>    //    "reverse"    两种重复模式

 

pubic void translate(View view) {

    iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

a.2    代码构造平移动画

public void translate(View view){

    TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);    //    float

    //    fromXDelta = toXDelta = fromYDelta = 0;toYDelta = -500;    //    移动Y

    animation.setDuration(2000);

    animation.setFillAfter(true);

    animation.setRepeatCount(1);    //    Animation.INFINITE
    animation.setRepeatMode(Animation.RESTART);

    iv.startAnimation(animation);

 

    animation = new TranslateAnimation(

                        fromXType, fromXValue,    //    Animation.RELATIVE_TO_SELF, 0

                        toXType, toXValue,            //    Animation.RELATIVE_TO_PARENT, 0

                        fromYType, fromYValue,    //    Animation.RELATIVE_TO_SELF, 0

                        toYType, toYValue,            //    Animation.RELATIVE_TO_PARENT, 倍数

    );

    注意第四排参数,结束Y位置 = 原来动画Y + toYValue*布局的高度

    改第四排参数为://    Animation.RELATIVE_TO_SELF, 倍数    后,结束Y位置 = 原来动画Y + toYValue*控件的高度/布局的高度

}

 

b.缩放(ScalaAnimation)

<scale ...     //    属性都是相对于图片自己,1212是以图片左上角为原点的缩放倍数

    android:fromXScale="0"        //    "1"

    android:toXScale="1"            //    "2"

    android:fromYScale="0"        //    "1"

    android:toYScale="1"            //    "2"

    android:duration="2000"      //    5个必要属性

    android:povotX="50%"

    android:povotY="50%"         //缩放中心(默认为控件左上角),百分数:不能为小数,为控件宽/高的百分比

    android:fillAfter="true"       //    保持动画最后那个状态

    android:repeatCount="1"   //    执行两次,"infinite":永久

    android:repeatMode="restart"/>    //    "reverse"    两种重复模式

 

public void scale(View view) {

    iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

 

b.2 代码实现缩放动画

public void scale(View view) {

    每一个动画都有的属性:

    1.构造方法     ScalAnimation animation = new ScalAnimation(fromX, toX, fromY, toY);    //    1, 0, 1, 0

    2.时间:        animation.setDuration(2000);

    3.是否停留    //    345可省略

    4.重复次数

    5.重复模式

    6.让控件展现动画        img.setAnimation(animation);

 

    animation = new ScalAnimation(1, 0, 1, 0, pivotX, pivotY);    //    pivotX = iv.getWidth()/2;    pivotY = iv.getHeight()/2;    //    iv表示控件

    animation = new ScalAnimation(1, 0, 1, 0,

                                                      pivotXType, pivotX,            //    Animation.RELATIVE_TO_SELF, 0.5f

                                                      pivotYType, pivotY);           //    Animation.RELATIVE_TO_PARENT, 0.5f

}

 

c.透明(AlphaAnimation)

<alpha ...

    android:fromAlpha="1"    //    彻底不透明(原图),[0,1]的小数,不能是百分数

    android:toAlpha="0"        //    彻底透明(消失)

    android:duration="2000"/>

 

public void alpha(View view) {

    iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

 

c.2 代码实现透明动画

public void alpha(View view) {

    AlphaAnimation animation = new AlphaAnimation(1,0.2f);

    animation.setDuration(2000);

    animation.setFillAfter(true);

    iv.startAnimation(animation);

}

 

d.旋转(RotateAnimation)

<rotate ...

    fromDegress="0"

    toDegress="360"

    pivotX="50%"    //    100%

    pivotY="50%"    //    100%

    duration="2000"/>

 

public void rotate(View view) {

    iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

d.2 代码实现旋转动画

public void rotate(View view) {

    RotateAnimation animation = new RotateAnimation(0,360);

 

    animation = new RotateAnimation(0, -360, iv.getWidth()/2, iv.getHeight()/2);

    

    animation = new RotateAnimation(0, 720,

                                                          Animation.RELATIVE_TO_PARENT, 0.5f,

                                                          Animation.RELATIVE_TO_PARENT, 0.5f);    //    ps:    不是屏幕中心

    animation.setDuration(2000);

    iv.startAnimation(animation);

}

 

e.混合(AnimationSet) 

<set ... >        //    动画集合

    <translate />

    <rotate    />

    ...

</set>

 

public void mix(View view) {

    iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

 

e.2 代码实现动画集合

AnimationSet set = new AnimationSet(false);        //    为true时set可设置全部类型动画的属性

set.addAnimation(translateAnimation);

...

iv.startAnimation(set);

 

2.2    补间动画监听器

animation.setAnimationListener(new AnimationListener() {

    @Override

    public void onAnimationStart(Animation animation) {}

    @Override

    public void onAnimationRepeat(Animation animation) {}

    @Override

    public void onAnimationEnd(Animation animation) {}

})

3.属性动画

下一个动画类型的起始状态即为上一个动画类型结束的状态,推荐使用

res/animator/xxx.xml

有animator、objectAnimator、set三个节点

<objectAnimator

    android:propertyName="translationX"        //    xml方式只能写一个方向,透明无方向。translationX代码提示方法:iv.setTranslationX(translationX);

    android:valueFrom="0"

    android:valueTo="300"

    duration="2000"/>

 

ObjectAnimator animator = (ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.xxx);

animator.setTarget(iv);

animator.start();

3.2    代码实现属性动画

public void translate(View view) {

    ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", float ... values);        //    float ... values:0, 300

    animator.setDuration(2000);

    animator.start();

}

 

public void translate(View view) {

    ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(iv,

                                                PropertyValuesHolder.ofFloat("translationX", 0, 300),        //

                                                PropertyValuesHolder.ofFloat("translationY", 0, 300));       //    PropertyValueHolder ... values

    animator.setDuration(2000);

    animator.setReaptCount(1);                                    //    可选

    animator.setReaptMode(Animation.REVERSE)         //    可选

    animator.start();

    参考translation的取值,可替换为:

    scale;        "scaleX", 1, 2, 1

    alpha:        "alpha", 1, 0, 1

    rotate:        "rotationX", 0, 360    //    以X轴旋转360度    还可取值"rotation"

}

 

3.3    属性动画监听器

animator.addListener(new AnimatorListener() {

    

@Override

    public void onAnimationStart(Animator animator) {}

    @Override

    public void onAnimationRepeat(Animator animator) {}

    @Override

    public void onAnimationEnd(Animator animator) {}

    @Override

    public void onAnimationCancel(Animator animator) {}

});

 

3.4 属性动画集:

public void together(View view) {

    AnimatorSet set = new AnimatorSet();

    ...        //    设置时间等

    set.playTogether(Animator ... items);            //    同时执行

    

    set.playSequentially(Animator ... items);        //    前后调用

    set.start();

}