1 安卓属性动画Animator
1)区别:传统动画用于显示效果,属性动画用于界面交互
ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F).setDuration(2000).start(); //imageView,实现位移动画
ObjectAnimator.ofFloat(imageView, "X", 0F, 200F).setDuration(2000).start(); //imageView,实现位移动画,先向左移动到X坐标为0F,然后再向右移动到X坐标为200F。
ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(2000).start(); //imageView,实现旋转动画
ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F) .setDuration(2000).start();
ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F).setDuration(2000).start();
ObjectAnimator.ofFloat(imageView, "translationY", 0F, 200F).setDuration(2000).start(); //效果动画同时执行
PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation", 0F,360F);
PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX", 0F, 200F);
PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY", 0F, 200F);
ObjectAnimator.ofPropertyValuesHolder(imageView, p1,p2,p3).setDuration(2000).start();//效果动画同时执行
AnimatorSet set = new AnimatorSet();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView,"translationY", 0F, 200F);
set.playTogether(animator1,animator2,animator3);
set.playSequentially(animator1,animator2,animator3); //顺序播放动画
set.play(animator2).with(animator3);
set.play(animator1).after(animator2); // 先animator2和animator3一起播放,然后animator1播放。
set.setDuration(2000);
set.start(); //效果动画同时执行
2) 安卓卫星菜单实例:
private int[] res = { R.id.imageView7, R.id.imageView6, R.id.imageView5,
R.id.imageView4, R.id.imageView3, R.id.imageView2, R.id.imageView1
};
private List<ImageView> imageViewList = new ArrayList<ImageView>();
private boolean flag = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < res.length; i++) {
ImageView imageView = (ImageView) findViewById(res[i]);
imageView.setOnClickListener(this);
imageViewList.add(imageView);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageView7:
if (flag) {
startAnimation();
} else {
closeAnimation();
}
break;
default:
break;
}
}
private void closeAnimation() { //点击按钮进行菜单回收
// TODO Auto-generated method stub
for (int i = 1; i < res.length; i++) {
ObjectAnimator animator = ObjectAnimator.ofFloat(
imageViewList.get(i), "translationY", i * 130F, 0F);
animator.setDuration(500);
animator.setInterpolator(new BounceInterpolator());
animator.setStartDelay(i * 300);
animator.start();
flag = true;
}
}
private void startAnimation() {
// TODO Auto-generated method stub
for (int i = 1; i < res.length; i++) {
ObjectAnimator animator = ObjectAnimator.ofFloat(
imageViewList.get(i), "translationY", 0F, i * 130F); //设置每个Y偏移量i * 130F
animator.setInterpolator(new BounceInterpolator()); //设置回弹动画
animator.setDuration(500);
animator.setStartDelay(i * 300); //每个按钮的开启动画延迟i * 300
animator.start();
flag = false;// 标志是否处于回收状态
}
}
3) ValueAnimation实例:
ValueAnimator animator = ValueAnimator.ofInt(0, 100);
animator.setDuration(5000);
animator.start();
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
Integer value = (Integer) animation.getAnimatedValue();
button.setText("" + value);
}
});// 实行按钮在5秒内从0-100变化
ValueAnimator animator = ValueAnimator
.ofObject(new TypeEvaluator<PointF>() { //其中PointF为浮点数的坐标泛型
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
// TODO Auto-generated method stub
return null;
}
});
//其中还有许多不知道有待学习。