AnimatorSet的使用

这里写图片描述

这个场景其实就是一个用户的头像选择,最中间的为用户默认的头像,点击四周头像可以与中间的头像进行交换。交换的过程就是用到了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());
        }
    }