透明度变化
1.普通动画
其中repeatCount为循环多少次,infinite为无限循环
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.1"
android:repeatCount="infinite"
android:duration="500"
android:toAlpha="1" />
</set>
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
animation.setDuration(1000);
image.startAnimation(animation);
2.属性动画
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image, "alpha", 0f, 1.0f,0f);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.setDuration(1000);
objectAnimator.start();
平移
1.普通动画
平移回来又回去,注意回去就已经变为负数,因为新的起点变为0
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:duration="1000"
android:toXDelta="500" />
<translate
android:fromXDelta="0"
android:duration="1000"
android:startOffset="1000"
android:toXDelta="-500"/>
</set>
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.translation);
image.startAnimation(animation1);
2.属性动画
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(image, "translationX", 0, 300.0f, 0f);
objectAnimator1.setRepeatCount(2);
objectAnimator1.setDuration(1000);
objectAnimator1.start();
旋转
普通动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
Animation animation3 = AnimationUtils.loadAnimation(this, R.anim.rotate);
animation3.setDuration(1000);
image.startAnimation(animation3);
属性动画
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(image, “rotation”, 0f, 360f);
objectAnimator3.setDuration(1000);
objectAnimator3.start();
大小
1.普通动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.scale);
animation2.setDuration(1000);
image.startAnimation(animation2);
2.属性动画
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(image, "scaleX", 0f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(image, "scaleY", 0f, 1.0f);
//组合动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(1000);
animatorSet.play(objectAnimator2).with(objectAnimator4);
animatorSet.start();
组合动画
实现一个效果,就是先透明度变化并旋转一圈,然后平移一段距离又回来
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:startOffset="1000"
android:toXDelta="500" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:startOffset="2000"
android:toXDelta="-500" />
</set>
Animation animation4 = AnimationUtils.loadAnimation(this, R.anim.combination);
image.startAnimation(animation4);
属性动画
ObjectAnimator anim_alpha = ObjectAnimator.ofFloat(image, "alpha", 0f, 1.0f);
ObjectAnimator anim_rotation = ObjectAnimator.ofFloat(image, "rotation", 0f, 360f);
ObjectAnimator anim_translation = ObjectAnimator.ofFloat(image, "translationX", 0f, 500f);
ObjectAnimator anim_translation1 = ObjectAnimator.ofFloat(image, "translationX", 500f, 0f);
//用了两个set
AnimatorSet animatorSet1 = new AnimatorSet();
animatorSet1.play(anim_alpha).with(anim_rotation);
animatorSet1.setDuration(1000);
animatorSet1.start();
AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.play(anim_translation).before(anim_translation1);
animatorSet2.setDuration(1000).setStartDelay(1000);
animatorSet2.start();