简介:
怎样使用 property animations,特别是ObjectAnimator,给出了多种多样的动画。比起ViewAnimations ,ObjectAnimator表现了更多易使用的方法。
ps: property animations只是支持在android 3.0之上。
https://www.youtube.com/watch?v=3UbJhmkeSig
public class PropertyAnimations extends Activity {
CheckBox mCheckBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_property_animations);
mCheckBox = (CheckBox) findViewById(R.id.checkbox);
final Button alphaButton = (Button) findViewById(R.id.alphaButton);
final Button translateButton = (Button) findViewById(R.id.translateButton);
final Button rotateButton = (Button) findViewById(R.id.rotateButton);
final Button scaleButton = (Button) findViewById(R.id.scaleButton);
final Button setButton = (Button) findViewById(R.id.setButton);
// Fade the button out and back in
ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(alphaButton,
View.ALPHA, 0);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
// Move the button over to the right and then back
ObjectAnimator translateAnimation =
ObjectAnimator.ofFloat(translateButton, View.TRANSLATION_X, 800);
translateAnimation.setRepeatCount(1);
translateAnimation.setRepeatMode(ValueAnimator.REVERSE);
// Spin the button around in a full circle
ObjectAnimator rotateAnimation =
ObjectAnimator.ofFloat(rotateButton, View.ROTATION, 360);
rotateAnimation.setRepeatCount(1);
rotateAnimation.setRepeatMode(ValueAnimator.REVERSE);
// Scale the button in X and Y. Note the use of PropertyValuesHolder to animate
// multiple properties on the same object in parallel.
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(View.SCALE_X, 2);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 2);
ObjectAnimator scaleAnimation =
ObjectAnimator.ofPropertyValuesHolder(scaleButton, pvhX, pvhY);
scaleAnimation.setRepeatCount(1);
scaleAnimation.setRepeatMode(ValueAnimator.REVERSE);
// Run the animations above in sequence
AnimatorSet setAnimation = new AnimatorSet();
setAnimation.play(translateAnimation).after(alphaAnimation).before(rotateAnimation);
setAnimation.play(rotateAnimation).before(scaleAnimation);
setupAnimation(alphaButton, alphaAnimation, R.animator.fade);
setupAnimation(translateButton, translateAnimation, R.animator.move);
setupAnimation(rotateButton, rotateAnimation, R.animator.spin);
setupAnimation(scaleButton, scaleAnimation, R.animator.scale);
setupAnimation(setButton, setAnimation, R.animator.combo);
}
private void setupAnimation(View view, final Animator animation, final int animationID) {
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// If the button is checked, load the animation from the given resource
// id instead of using the passed-in animation parameter. See the xml files
// for the details on those animations.
if (mCheckBox.isChecked()) {
Animator anim = AnimatorInflater.loadAnimator(PropertyAnimations.this, animationID);
anim.setTarget(v);
anim.start();
return;
}
animation.start();
}
});
}
}
ps:上面的动画的实现有java Code ,当点击CheckBox就切换到了xml的实现的方式,xml实现的方式,请到官网上下载源码。
参考: