andrroid 动画 oncreate 和 onclick 启动方式


今天登陆界面弄了一个注册和登陆切换动画,发现 Android 平移动画 在

1. oncreate 方法中启动

2.onclick方法中启动


这两者有区别

其中第一种,只需要这样写:

main_info_container.setAnimation(anim1);

这样启动activity时,就能进行执行动画

具体如下:

@AfterViews
    void showAnimation() {

        int  width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        int  height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        main_info_container.measure(width,height);

        int height_ = main_info_container.getMeasuredHeight();
        height_ = TranslatingUtil.Dp2Px(this, height_);

        width_ = main_info_container.getMeasuredWidth();
        //width_ = TranslatingUtil.Dp2Px(this, width_ / 2.0f);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width_ = TranslatingUtil.Px2Dp(this,dm.widthPixels);

        Rotate3dAnimation anim1 = new Rotate3dAnimation(0,90,width_,0,0,false);
        anim1.setDuration(1000);
        anim1.setFillAfter(true);
        anim1.setInterpolator(new AccelerateInterpolator());
        // 设置监听
        //anim.setAnimationListener(new DisplayNextView());

        anim1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                /**注册显示**/
                main_info_container2.setVisibility(View.VISIBLE);
                main_info_container.setVisibility(View.GONE);
                Rotate3dAnimation anim2 = new Rotate3dAnimation(-90, 0, width_, 0, 0, false);
                anim2.setDuration(100);
                anim2.setFillAfter(true);
                anim2.setInterpolator(new AccelerateInterpolator());

                main_info_container2.setAnimation(anim2);
                //main_info_container2.startAnimation(anim2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        main_info_container.setAnimation(anim1);

    }


而第二种按钮点击效果的时候,需要启动才可以:

main_info_container.startAnimation(anim1);


具体如下:


 @Click
    void translate(){
        showAnimation();
    }
    @AfterViews
    void showAnimation() {

        int  width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        int  height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        main_info_container.measure(width,height);

        int height_ = main_info_container.getMeasuredHeight();
        height_ = TranslatingUtil.Dp2Px(this, height_);

        width_ = main_info_container.getMeasuredWidth();
        //width_ = TranslatingUtil.Dp2Px(this, width_ / 2.0f);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width_ = TranslatingUtil.Px2Dp(this,dm.widthPixels);

        Rotate3dAnimation anim1 = new Rotate3dAnimation(0,90,width_,0,0,false);
        anim1.setDuration(1000);
        anim1.setFillAfter(true);
        anim1.setInterpolator(new AccelerateInterpolator());
        // 设置监听
        //anim.setAnimationListener(new DisplayNextView());

        anim1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                /**注册显示**/
                main_info_container2.setVisibility(View.VISIBLE);
                main_info_container.setVisibility(View.GONE);
                Rotate3dAnimation anim2 = new Rotate3dAnimation(-90, 0, width_, 0, 0, false);
                anim2.setDuration(100);
                anim2.setFillAfter(true);
                anim2.setInterpolator(new AccelerateInterpolator());

                //main_info_container2.setAnimation(anim2);
                main_info_container2.startAnimation(anim2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        main_info_container.startAnimation(anim1);
        //main_info_container.setAnimation(anim1);

    }



你可能感兴趣的:(andrroid 动画 oncreate 和 onclick 启动方式)