动画
Android动画共分为四类
- Alpha 淡入淡出
- Scale 缩放
- Rotate 旋转
- Translate 平移
Alpha
//参数范围 0~1,0:完全透明, 1:完全不透明
//参数1: fromAlpha开始的透明度
//参数2: toAlpha结束的透明度
AlphaAnimation aa = new AlphaAnimation(1, 0);
aa.setDuration(300);//动画执行时间
View.startAnimation(aa);//启动动画,把View替换成要添加动画的控件
Scale
/**
* Constructor to use when building a ScaleAnimation from code
*
* @param fromX Horizontal scaling factor to apply at the start of the
* animation
* @param toX Horizontal scaling factor to apply at the end of the animation
* @param fromY Vertical scaling factor to apply at the start of the
* animation
* @param toY Vertical scaling factor to apply at the end of the animation
* @param pivotXType Specifies how pivotXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object
* is being scaled, specified as an absolute number where 0 is the
* left edge. (This point remains fixed while the object changes
* size.) This value can either be an absolute number if pivotXType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object
* is being scaled, specified as an absolute number where 0 is the
* top edge. (This point remains fixed while the object changes
* size.) This value can either be an absolute number if pivotYType
* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
ScaleAnimation sa = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(3000);
View.startAnimation(sa);
Rotate
/**
* Constructor to use when building a RotateAnimation from code
*
* @param fromDegrees Rotation offset to apply at the start of the
* animation.
*
* @param toDegrees Rotation offset to apply at the end of the animation.
*
* @param pivotXType Specifies how pivotXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object
* is being rotated, specified as an absolute number where 0 is the
* left edge. This value can either be an absolute number if
* pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%)
* otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object
* is being rotated, specified as an absolute number where 0 is the
* top edge. This value can either be an absolute number if
* pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%)
* otherwise.
*/
RotateAnimation ra = new RotateAnimation(0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
ra.setDuration(3000);
View.startAnimation(ra);
Translate
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);
ta.setDuration(3000);
View.startAnimation(ta);
Animation其他常用的方法
setRepeatCount(Animation.INFINITE);//设置动画的重复次数,INFINITE为无限次
setFillAfter(boolean fillAfter);//true:动画结束后,控件停留在执行后的状态
setFillBefore(boolean fillBefore);//true:动画结束后,控件停留在动画开始的状态
setInterpolator(new Interpolator);//动画的运动速度
组合动画
AnimationSet set = new Animation(true);
set.addAnimation(aa);
set.addAnimation(ra);
set.addAnimation(sa);
set.addAnimation(ta);
set.setDuration(3000);
set.setStartOffset(100);
View.startAnimation(set);
XML动画
xml文件存放在:res/anim下
XML组合动画 animation.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="2000"
/>
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
<rotate
android:fromDegrees="0"
android:toDegrees="400"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
<translate
android:fromXDelta="50%"
android:toXDelta="100%"
android:fromYDelta="50%"
android:toYDelta="100%"
android:duration="3000"
/>
<!-- android:pivotX="50"使用绝对坐标 android:pivotX="50%"相对自己 android:pivotX="50%p"相对父控件 -->
</set>
</span>
XML动画用法
代码获取动画
Animation a=AnimationUtils.loadAnimation(this, R.anim.animation);
View.startAnimation(a);
XML配置动画
<!-- 配置window展现、退出动画 -->
<style name="ActionSheetAnimation" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/push_up_in</item>
<item name="android:windowExitAnimation">@anim/push_up_out</item>
</style>
<!-- 设置动画 -->
<style name="Theme.DialogActivity" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@color/black</item>
<!-- 设置底层可见 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 设置跳转效果 -->
<item name="android:windowAnimationStyle">@style/ActionSheetAnimation</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
<!-- 在Manifest中给增加动画的Activity配置Theme -->
android:theme="@style/Theme.DialogActivity"
帧动画
<!-- anim.xml -->
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/a1" android:duration="500"/>
<item android:drawable="@drawable/a2" android:duration="500"/>
<item android:drawable="@drawable/a3" android:duration="500"/>
<item android:drawable="@drawable/a4" android:duration="500"/>
</animation-list>
ImageView.setBackgroundResource(R.drawable.anim);
AnimationDrawable ad = (AnimationDrawable)ImageView.getBackground();
ad.start();
LayoutAnimationController动画
XML 实现
<!-- anim.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="3000"
/>
</set>
<!-- layout.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/anim"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layout"/>
代码实现
AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(3000);
LayoutAnimationController lac=new LayoutAnimationController(alpha);
//LayoutAnimationController.ORDER_NORMAL;//顺序显示
//LayoutAnimationController.ORDER_REVERSE;//反显示
//LayoutAnimationController.ORDER_RANDOM;//随机显示
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
lv.setLayoutAnimation(lac);