动画

来源:互联网 发布:网络语乌干达什么意思 编辑:程序博客网 时间:2024/04/27 04:42
帧动画
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/girl_1" android:duration="200" />
    <item android:drawable="@drawable/girl_2" android:duration="200" />
    <item android:drawable="@drawable/girl_3" android:duration="200" />
    <item android:drawable="@drawable/girl_4" android:duration="200" />
    <item android:drawable="@drawable/girl_5" android:duration="200" />
    <item android:drawable="@drawable/girl_6" android:duration="200" />
    <item android:drawable="@drawable/girl_7" android:duration="500" />
    <item android:drawable="@drawable/girl_8" android:duration="200" />
    <item android:drawable="@drawable/girl_9" android:duration="200" />
    <item android:drawable="@drawable/girl_10" android:duration="200" />
    <item android:drawable="@drawable/girl_11" android:duration="200" />
</animation-list>
ImageView image=(ImageView) findViewById(R.id.imageView1);
AnimationDrawable anim=(AnimationDrawable) image.getBackground();
anim.start();
补间动画
//旋转动画
RotateAnimation ra=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);
ra.setRepeatCount(1);
ra.setRepeatMode(Animation.REVERSE);
image.startAnimation(ra);
//缩放动画
/**
 * 6:缩放时从哪个点开始缩放
 */
ScaleAnimation sa=new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(2000);
sa.setRepeatCount(1);
sa.setRepeatMode(Animation.REVERSE);
image.startAnimation(sa);
//位移动画
TranslateAnimation ta=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f);
ta.setDuration(2000);
ta.setRepeatCount(1);
ta.setRepeatMode(Animation.REVERSE);
image.startAnimation(ta);
//透明度变化
AlphaAnimation aa=new AlphaAnimation(0f, 1f);
aa.setDuration(2000);
aa.setRepeatCount(1);
aa.setRepeatMode(Animation.REVERSE);
image.startAnimation(aa);
//组合动画
AnimationSet set=new AnimationSet(true);
RotateAnimation ra=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);
ra.setRepeatCount(1);
ra.setRepeatMode(Animation.REVERSE);
TranslateAnimation ta=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f);
ta.setDuration(2000);
ta.setRepeatCount(1);
ta.setRepeatMode(Animation.REVERSE);
set.addAnimation(ra);
set.addAnimation(ta);
image.startAnimation(set);
以上三代码的方式  还有布局文件的方式
anim文件夹里面创建
//透明度变化
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.1"
    android:fillAfter="true"
    ><!-- 就停在最后一帧上面 -->
</alpha>

 android:fillAfter="true"<!-- 就停在最后一帧上面 -->
调用xml动画
Animation ta=AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
image.startAnimation(ta);

//旋转动画
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="180"
    android:fillAfter="true"
    android:duration="200"
    >
<!-- 开始角度,结束角度 -->
</rotate>
//缩放动画
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.2"
    android:toXScale="0.2"
    android:fromYScale="0.2"
    android:toYScale="0.2"
    android:duration="2000"
    >
</scale>
0 0