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

android动画之补间动画

2016-05-31 15:54 459 查看
补间动画(移动补间动画,缩放补间动画,旋转补间动画,透明补间动画):

1.移动补间动画(TranslateAnimation):

指定移动的绝对位置(以自己的左上角为参照点):

TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 500);

translateAnimation.setDuration(5000);

imageview.startAnimation(translateAnimation);

2.指定移动的相对位置(相对父控件):

TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.5f);

translateAnimation.setFillAfter(true);//停留在动画最后一帧,不指定的话,动画播放完毕会回到初始状态

translateAnimation.setDuration(5000);

imageview.startAnimation(translateAnimation);

3.指定移动的相对位置(相对自己)

      

TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
4.0f);

translateAnimation.setFillAfter(true);

translateAnimation.setDuration(5000);

imageview.startAnimation(translateAnimation);

         

4.在xml中定义动画:

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"

    android:duration="5000"

    android:fromXDelta="0" 

    android:toXDelta="0"

    android:fromYDelta="0"

    android:toYDelta="200"

    android:fillAfter="true">

</translate>

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"

    android:duration="5000"

    android:fromXDelta="0%" 

    android:toXDelta="0%"

    android:fromYDelta="0%"

    android:toYDelta="200%"

    android:fillAfter="true">

</translate>

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"

    android:duration="5000"

    android:fromXDelta=“0%p” 

    android:toXDelta=“0%p”

    android:fromYDelta=“0%p”

    android:toYDelta="200%p”

    android:fillAfter="true">

</translate>

TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.anim_translate);

imageview.startAnimation(translateAnimation);

2.缩放补间动画:

以自己的左上角为圆心缩放:

ScaleAnimation scaleAnimation = new ScaleAnimation(

1.0f,

1.5f, 

1.0f,

1.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);

xml:

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXScale="1.0"

    android:toXScale="1.5"

    android:fromYScale="1.0"

    android:toYScale="1.5"

 >

</scale>

以自己的中心点为圆心进行缩放:

ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);

xml:

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXScale="1.0"

    android:toXScale="1.5"

    android:fromYScale="1.0"

    android:toYScale="1.5"

    android:pivotX="50%"

    android:pivotY="50%" >

</scale>

以父控件为参照:

ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_PARENT,
0.5f,
Animation.RELATIVE_TO_PARENT,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);

xml:

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXScale="1.0"

    android:toXScale="1.5"

    android:fromYScale="1.0"

    android:toYScale="1.5"

    android:pivotX="50%p"

    android:pivotY="50%p" >

</scale>

如果imageview在父控件中间,上面两种设置的效果并不一样,后面这个中心点并不在父控件中心,它是以imageview的左上角为参照,偏移父控件宽高的一半,这时候,动画的中心点并不在父控件的中心

3.旋转补间动画

以左上角为原点旋转:

RotateAnimation rotateAnimation = new RotateAnimation(
0,
180);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);

以自己的中心为原点旋转:

RotateAnimation rotateAnimation = 
new RotateAnimation(
0,
180,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);

4.透明补间动画:

Animation animation = new AlphaAnimation(1f,0.1f);
animation.setFillAfter(true);
animation.setDuration(5000);
imageview.startAnimation(animation);

5.多个动画同时进行:

AnimationSet set = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0.1f);
alphaAnimation.setDuration(5000);
set.addAnimation(alphaAnimation);

TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,300);
translateAnimation.setDuration(5000);
set.addAnimation(translateAnimation);

set.setFillAfter(true);

imageview.startAnimation(set);

xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:fromXScale="1.0"

        android:fromYScale="1.0"

        android:pivotX="50%"

        android:pivotY="50%"

        android:toXScale="1.5"

        android:toYScale="1.5"

        android:duration="5000" >

    </scale>

    <translate

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:duration="5000"

        android:fromXDelta="100"

        android:fromYDelta="0"

        android:toXDelta="100"

        android:toYDelta="300" >

    </translate>

</set>

AnimationSet animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);

animation.setFillAfter(true);

imageview.startAnimation(animation);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: