本文由PurpleSword(jzj1993)原创,转载请注明
原文网址 http://blog.csdn.net/jzj1993
常见动画有几种
控件View的动画(如Dialog窗口中的圆形进度条)
空间Window的动画(如DialogWindow,PopupWindow的动画,Activity切换时整个Window页面的动画)
ViewGroup的LayoutAnimation动画,每个子控件按照设定的顺序、延迟播放动画
动画常用anim/*.xml定义
xml中定义动画,可直接使用<translate><scale><alpha><rotate>标签直接定义,也可以放在<set>标签中,里面定义的动画将同时开始播放。
两者都可使用AnimationUtils.loadAnimation方法加载。如果是set标签定义,加载时返回的是AnimationSet实例(AnimationSet继承自Animation)。
在set标签中设置的一些属性,会直接覆盖它里面定义动画的对应属性,而 AnimationSet的另外一些从Animation继承的属性则无效,下面是AnimationSet类的官方说明。
Represents a group of Animations that should be played together. The transformation of each individual
animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet
affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:
- duration, repeatMode, fillBefore, fillAfter: These properties, when set on an AnimationSet object, will be pushed down to all child animations.
- repeatCount, fillEnabled: These properties are ignored for AnimationSet.
- startOffset, shareInterpolator: These properties apply to the AnimationSet itself.
Starting with
android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH
,
the behavior of these properties is the same in XML resources and at runtime (prior to that release, the values set in XML were ignored for AnimationSet). That is, callingsetDuration(500)
on
an AnimationSet has the same effect as declaringandroid:duration="500"
in an
XML resource for an AnimationSet object.
常规补间动画:弹跳(移动)
Layout/activity_welcome_anim.xml
(0%p表示占父组件尺寸的百分比)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="0%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="0"
android:toYDelta="42%p" />
<translate
android:duration="350"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:startOffset="500"
android:toXDelta="0"
android:toYDelta="-21%p" />
<translate
android:duration="350"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="850"
android:toXDelta="0"
android:toYDelta="21%p" />
<translate
android:duration="250"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:startOffset="1200"
android:toXDelta="0"
android:toYDelta="-10%p" />
<translate
android:duration="250"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="1450"
android:toXDelta="0"
android:toYDelta="10%p" />
<translate
android:duration="150"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:startOffset="1700"
android:toXDelta="0"
android:toYDelta="-5%p" />
<translate
android:duration="150"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="1850"
android:toXDelta="0"
android:toYDelta="5%p" />
</set>
再例如常规补间动画:缩放、透明度
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="800"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="800"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
再如上浮效果(移动、透明度)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="5%"
android:toXDelta="0"
android:toYDelta="0%" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<alpha
android:duration="100"
android:fromAlpha="1.0"
android:startOffset="1400"
android:toAlpha="1.0" />
</set>
可使用Java程序加载
this.setContentView(R.layout.activity_welcome);
anim = AnimationUtils.loadAnimation(this,
R.anim.welcome_anim);
// 动画效果执行完毕后,View对象保留在终止的位置
anim.setFillEnabled(true);
anim.setFillAfter(true);
this.findViewById(R.id.point).setAnimation(anim);
还可设置动画监听器
anim.setAnimationListener(listener);
/**
* 动画监听器
*/
private AnimationListener listener = new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
// startMain();
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};
在Dialog中动画的加载
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.dialog_voice, null);
ImageView img = (ImageView) v.findViewById(R.id.dialog_img);
Animation anim = AnimationUtils.loadAnimation(context,
R.anim.center_rotate_repeat);
img.startAnimation(anim);
new AlertDialog.Builder(context).setView(v);
给整个Dialog设置动画
dialog.getWindow().setWindowAnimations(R.style.quick_scale_anim);
给PopupWindow设置动画
pop.setAnimationStyle(R.style.quick_scale_anim);
Activity的切换动画
代码实现Activity切换动画
startActivity(new Intent(this, ListAlarm.class));
overridePendingTransition(R.anim.anim_activity_in,
R.anim.anim_activity_unchange);
或者
ActivityGuide.this.finish();
overridePendingTransition(R.anim.alpha_stay, R.anim.alpha_exit);
在XML中定义Activity切换动画
Activity切换动画:不能设置SingleInstance属性,会导致动画失效
<style name="activityAnimation" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@null</item>
<item name="android:windowExitAnimation">@null</item>
<!-- 新的Activity启动时Enter动画 -->
<item name="android:activityOpenEnterAnimation">@anim/slide_left_in</item>
<!-- 新的Activity启动时原有Activity的Exit动画 -->
<item name="android:activityOpenExitAnimation">@anim/keep</item>
<!-- 新的Activity退出时原有ActivityEnter动画 -->
<item name="android:activityCloseEnterAnimation">@anim/keep</item>
<!-- 新的Activity退出时Exit动画 -->
<item name="android:activityCloseExitAnimation">@anim/slide_right_out</item>
</style>
Manifest.xml
<application
android:theme="@style/app_theme" >
<activity
android:name=".ui.ActivityWelcome"
android:theme="@style/app_theme_fullscreen" >
</activity>
</application>
style.xml
<style name="app_theme" parent="@android:style/Theme.Light.NoTitleBar">
<item name="android:windowAnimationStyle">@style/activity_anim</item>
</style>
<style name="activity_anim">
<item name="android:windowEnterAnimation">@anim/quick_alpha_enter</item>
<item name="android:windowExitAnimation">@anim/quick_alpha_stay</item>
</style>
anim/quick_alpha_enter.xml
anim/quick_alpha_stay.xml
LayoutAnimation
1、在 layout_anim_item.xml 中定义子View动画
2、在 layout_anim.xml 中定义Layout动画,并引用子View动画
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/layout_anim_item"
android:animationOrder="normal"
android:delay="0.25" />
3、在ViewGroup中引用自定义Layout动画
<ViewGroup
android:layoutAnimation="@anim/layout_anim" >