无论是补间动画还是属性动画都有俩种实现方法:
- 用代码定义
- 使用xml方式定义
补间动画
setDuration:设置动画时长
setRepeatCount:设置重复的次数
setRepeatMode:设置动画执行的模式
startAnimation:执行动画
1.实现透明效果[AlphaAnimation]
AlphaAnimation a1 = new AlphaAnimation(1.0f, 0.0f);
a1.setDuration(2000);
a1.setRepeatCount(1);
a1.setRepeatMode(Animation.REVERSE);
iv.startAnimation(a1);
2.实现旋转效果[RotateAnimation]
fromDegrees:开始的角度
toDegrees:结束的角度
Animation.RELATIVE_TO_SELF:以自身为中心旋转
RotateAnimation a2 = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
a2.setDuration(2000);
a2.setRepeatCount(1);
a2.setRepeatMode(Animation.REVERSE);
iv.startAnimation(a2);
3.实现缩放效果[ScaleAnimation]
Animation.RELATIVE_TO_SELF:以自身为中心缩放
ScaleAnimation a3 = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
a3.setDuration(2000);
a3.setRepeatCount(1);
a3.setRepeatMode(Animation.REVERSE);
iv.startAnimation(a3);
2.实现位移效果[
Translate
Animation]
setFillAfter:设置此属性为true意为动画停留在结束位置
Animation.RELATIVE_TO_PARENT:相对于父控件动
TranslateAnimation a4 = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0.1f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
a4.setDuration(2000);
// a4.setRepeatCount(1);
a4.setRepeatMode(Animation.REVERSE);
a4.setFillAfter(true);// 当前动画结束后,动画停留在结束位置
iv.startAnimation(a4);
使用xml方式定义补间动画
(补间动画)
-
android——res——建一个anim目录——new一个xml
-
xml中设置的属性与在java中写的类似
-
在Activity中加载声明动画(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.alpha));
(属性动画)
android——res——animator目录
属性动画【ObjectAnimation.ofFloat】
translationX:位移
scaleY:缩放
alpha:透明
rotationX:旋转
把动画集合实现
补间动画是(AnimationSet set=new AnimationSet)
属性动画是(AnimatorSet set=new AnimatorSet)