Android动画基本使用

来源:互联网 发布:lee levis 知乎 编辑:程序博客网 时间:2024/05/16 07:54
一、Frame Animation 帧动画

1、在res/drawable/目录下新建xx.xml文件,root element为<animation-list>,引入需要播放的帧图片:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="a1.jpg" android:duration="200"/>
    <item android:drawable="a2.jpg" android:duration="200"/>
</animation-list>

2、在code中为View设置背景view.setBackgroundResource(R.drawable.xx);
3、获取AnimationDrawable对象anim:view.getBackground();
4、播放动画:anim.start();
     GameView gameView = (GameView) findViewById(R.id.game);
        gameView.setBackgroundResource(R.drawable.weather_background);
        AnimationDrawable animationDrawable = (AnimationDrawable) gameView.getBackground();
        animationDrawable.start();
        
        
二、Tween Animation 补间动画

1、在res/anim/目录下新建xx.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator=["true" | "false"] >
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float" />
    <scale
        android:fromXScale="float"
        android:toXScale="float"
        android:fromYScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float" />
        </set>
     
   2、在code中获取Animation对象;
    Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
   
   3、为object设置动画;
    ImageView image = (ImageView) findViewById(R.id.image);
image.startAnimation(hyperspaceJump);


三、属性动画


   1、在res/animator/目录下新建xx.xml文件:
<set android:ordering=["together" | "sequentially"]>
        <objectAnimator
android:propertyName="string"
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>


     <animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
</set>
   2、在code中获取AnimatorSet:
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);

   3、为object设置动画;
set.setTarget(myObject);
set.start();
0 0