一.View 动画
它提供了AlphaAnimation , RotateAnimation , TranslateAnimation , ScaleAnimation 四种动画方式,并提供AnimationSet动画集合,可以混合使用多种动画。
View动画非常常用,但是有个非常大的缺点—— 不具有交互性 .当一个控件在视图上移动了,但是其响应位置还在原来的位置! 所以它只能做一些避免交互的动画效果。
下面来介绍一下简单的例子:
1.1 透明度动画
private void startAlpha() {
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
mImageView.startAnimation(alphaAnimation);
Log.d(Tag, "startAlpha");
}
1.2 旋转动画
private void startRotate() {
RotateAnimation rotateAnimation = new RotateAnimation(0, 300,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);
mImageView.startAnimation(rotateAnimation);
Log.d(Tag, "startRotate");
}
参数是控制以自身为旋转中心,也可以设置其他值
1.3 位移动画
private void startTranslate() {
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
translateAnimation.setDuration(2000);
// translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true); // 完成视图动画后,保持动画结束时位置
mImageView.startAnimation(translateAnimation);
Log.d(Tag, "startTranslate");
}
1.4 缩放动画
private void startScale() {
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
mImageView.startAnimation(scaleAnimation);
Log.d(Tag, "startScale");
}
参数是控制以自身中心为中心缩放视图
1.5 动画集合
通过AnimationSet,可以将动画以组合的形式展示出来。
AnimationSet animationSet = new AnimationSet(true);
animationSet.setDuration(2000);
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
将透明度动画和旋转动画放到一起展示。
1.6 实现动画的监听,可以根据动画的状态来执行相应的操作
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { // 开始动画执行
}
@Override
public void onAnimationEnd(Animation animation) { // 结束动画执行
}
@Override
public void onAnimationRepeat(Animation animation) { // 重复动画执行
}
});