Android动画浮动按钮

时间:2016-08-12 06:32:50

标签: android android-studio animation

所以看起来我在Android中移动浮动按钮时遇到了一些麻烦。下面我已经尝试了不同的补救措施之前和之后的预览,但按钮将被放错位置或不寒而栗。 (请忽略背景中不断变化的视图,浮动按钮接缝是唯一有问题的部分)

enter image description hereBefore

enter image description hereAfter

private Animator animateRevealColorFromCoordinates(boolean clip) {
    float finalRadius = (float) Math.hypot(Recontent.getWidth(), Recontent.getHeight());

    if(fabCenterX == 0){
        fabCenterX = ((int) (fab.getX() + (fab.getWidth() / 2)));
        fabCenterY = ((int) (fab.getY() - Recontent.getY() + (fab.getHeight() / 2)));
    }

    final int nik = (clip) ? 1 : 0; 
    //This is what I'm calling the clipping check, basically a isHidingView.

    final AnimationSet animSet = new AnimationSet(true);
    animSet.setInterpolator(new DecelerateInterpolator());
    animSet.setFillAfter(true); //Tried true and false
    animSet.setFillEnabled(true); //for both of these
    animSet.setDuration(250);

    final RotateAnimation animRotate = new RotateAnimation(-45.0f*nik, -45.0f*Math.abs(nik - 1),
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    animSet.addAnimation(animRotate);

    TranslateAnimation animation = new TranslateAnimation(0f, fab.getWidth()*((int) ((nik-.5)*2)), 0f, 0f);
    animSet.addAnimation(animation);

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

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            /*android.support.design.widget.CoordinatorLayout.LayoutParams lp = (android.support.design.widget.CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            lp.setMargins((int) toDP(16),(int) toDP(16),(int) (fab.getWidth()*Math.abs(nik-1)+toDP(16)),(int) toDP(16));
            fab.setRotation(-45*Math.abs(nik-1));
            fab.setLayoutParams(lp);*/
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    Animator anim = ViewAnimationUtils.createCircularReveal(Recontent, fabCenterX, fabCenterY, finalRadius*nik, finalRadius*Math.abs(nik - 1));
    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            super.onAnimationStart(animation);
            fab.setOnClickListener(new blankClickListener());
            if(nik == 0){Recontent.setVisibility(View.VISIBLE);}
            fab.startAnimation(animSet);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            if(nik == 1){Recontent.setVisibility(View.INVISIBLE);}
            //toDP converts the numeric value to its dp equivalent. 
            /*android.support.design.widget.CoordinatorLayout.LayoutParams lp = (android.support.design.widget.CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            lp.setMargins((int) toDP(16),(int) toDP(16),(int) (fab.getWidth()*Math.abs(nik-1)+toDP(16)),(int) toDP(16));
            TranslateAnimation ta = new TranslateAnimation(0,0,0,0);
            ta.setDuration(0);
            fab.startAnimation(ta);fab.setRotation(45*nik);fab.setLayoutParams(lp);*/
            fab.setOnClickListener(fabsNorm);
        }
    });
    anim.start();
    return anim;
}

1 个答案:

答案 0 :(得分:0)

尝试此链接

动画Android浮动操作按钮

https://www.sitepoint.com/animating-android-floating-action-button/

此外,您还可以在此示例中尝试此链接更多动画和更多性能

使用Android FAB(浮动操作按钮)动画

https://www.learn2crack.com/2015/10/android-floating-action-button-animations.html