AnimatorSet:这个类为一组特定的动画指定次序。
总要办法如下:
play:该办法创建一个机关器对象用于创建束缚。
playTogether:设置同时运行一组动画
pase:暂停一个正在运行的动画
resume:从头运行暂停后的动画
isRunning:断定动画是否正在运行
isStarted:断定动画是否已经运行了
话不多说,直接上图:
这个场景其实就是一个用户的头像选择,最中间的为用户默认的头像,点击四周头像可以与中间的头像进行交换。交换的过程就是用到了Android动画中AnimationSet这个类。
onClick事件具体执行内容:
private void switchImage(ImageView image){
if(!animSet.isStarted() && image.getId()!=img_center.getId()){ //判断动画是否在运行,并且点击图片若在中间则不交换
ObjectAnimator anim1, anim2, anim3, anim4,
anim5, anim6, anim7, anim8, anim21, anim61;
animSet = new AnimatorSet();
anim1 = ObjectAnimator.ofFloat(image, "alpha",
0.2f, 1f);
anim2 = ObjectAnimator.ofFloat(image,
"y", image.getY() , img_center.getY());
anim21 = ObjectAnimator.ofFloat(image,
"x", image.getX() , img_center.getX());
anim3 = ObjectAnimator.ofFloat(image, "scaleX",
1.0f, 1.2f);
anim4 = ObjectAnimator.ofFloat(image, "scaleY",
1.0f, 1.2f);
anim5 = ObjectAnimator.ofFloat(img_center, "alpha",
1f, 0.2f);
anim6 = ObjectAnimator.ofFloat(img_center,
"y", img_center.getY() , image.getY());
anim61 = ObjectAnimator.ofFloat(img_center,
"x", img_center.getX() , image.getX());
anim7 = ObjectAnimator.ofFloat(img_center, "scaleX",
1f, 0.8f);
anim8 = ObjectAnimator.ofFloat(img_center, "scaleY",
1f, 0.8f);
animSet.play(anim1).with(anim2).with(anim3).with(anim4)
.with(anim5).with(anim6).with(anim7).with(anim8).with(anim21).with(anim61);
animSet.setDuration(1000);
animSet.start();
imgs.put(img_center, imgs.get(image));
imgs.put(image, 22);
image.setPadding(15,15,15,15);
img_center.setPadding(0,0,0,0);
img_center = image;
System.out.println("center_id:"+img_center.getId());
}
}