项目实战:浅谈属性动画(2)-动画监听事件,消失的按钮,酷炫Path2.0

有了属性动画(1)的基础后,我们进阶玩法,为动画设置监听事件。
先看一下消失的Button,核心代码3行。
在布局文件中添加Button,为其指定响应方法clickListener:

    

在Activity中完成响应方法:

    public void dismiass(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",0f,1f);
        animator.setDuration(1000);
        animator.start();
    }

设置透明度,run一下,消失的按钮~Get。

进入正题,为其设置监听事件,强大的AnimatorListener,给人无限遐想:

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Toast.makeText(FirstClassActivity.this,"onAnimationEnd.",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

下面我们实现Path2.0酷炫的动画效果:一一初始化图片并为其设置监听事件:

    private int mResIdArray[] = {R.id.a_iv_path,R.id.b_iv_path,R.id.c_iv_path,R.id.d_iv_path,R.id.e_iv_path,
            R.id.f_iv_path,R.id.g_iv_path,R.id.h_iv_path};
    private List mImageList = new ArrayList();
    private ImageView mTempImage;
    private boolean mAnimFlag = true;

  
    @Override
    public void initView() {
        for (int i = 0;i < mResIdArray.length;i++){
            mTempImage = (ImageView) findViewById(mResIdArray[i]);
            mTempImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()){
                        case R.id.a_iv_path:
                            if (mAnimFlag){
                                startAnim();
                            }else {
                                closeAnim();
                            }
                            break;
                        default:
                            Toast.makeText(PathActivity.this,"Click-" + v.getId(),Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
            mImageList.add(mTempImage);
        }
    }

设置开始动画和结束动画,
开始动画如下,同时设置X轴和Y轴的平移动画,组合一下,特别值得一提的是设置差值器(重力加速度),效果十分酷炫最后记得设置标志位,实现开始和结束的切换:

private void startAnim() {
        for (int i = 1;i < mResIdArray.length;i++){
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageList.get(i),
                    "translationY",0f,10f + i*50f);
            ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageList.get(i),
                    "translationX",0f,400f - i*50f);
            AnimatorSet set = new AnimatorSet();
            set.playTogether(animator1,animator2);
            set.setDuration(700);
            set.setInterpolator(new BounceInterpolator());//设置差值器,重力加速度
            set.setStartDelay(i*300);
            set.start();
        }
        mAnimFlag = false;
    }

结束动画和开始动画类似,只不过正好将变化范围倒过来。

private void closeAnim() {
        for (int i = 1;i < mResIdArray.length;i++){
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageList.get(i),
                    "translationY",i*300f,0f);
            ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageList.get(i),
                    "translationX",i*300f,0f);
            AnimatorSet set = new AnimatorSet();
            set.playTogether(animator1,animator2);
            set.setDuration(700);
            set.setInterpolator(new BounceInterpolator());//设置差值器,重力加速度
            set.setStartDelay(i*300);
            set.start();
        }
        mAnimFlag = true;
    }

布局文件如下,一笔带过:




    
    

    

        

        

        

        

        

        

        

        

    

原创不易,转载请注明出处哈。
权兴权意
产品可以更优雅~
项目实战:浅谈属性动画(2)-动画监听事件,消失的按钮,酷炫Path2.0 - hxqneuq2012的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/hxqneuq2012/article/details/62222257
项目源代码,欢迎提建议(star)。
https://github.com/HXQWill/QuanStudy/blob/master/app/src/main/java/com/example/quan/quanstudy/objectAnimator/PathActivity.java

你可能感兴趣的:(项目实战:浅谈属性动画(2)-动画监听事件,消失的按钮,酷炫Path2.0)