Android Design Support Library( 五 )


ViewCompat.setTranslationY(mView, mView.getHeight());ViewCompat.animate(mView).translationY(0f).setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR).setDuration(ANIMATION_DURATION)
也就是说一开始就向下移动了mView.()的长度 , 当出现的时候只是向着它原来的位置移动 , 本质上还是相当于从它原来的位置移动了一段距离 , 只是这个距离随着向上浮动的越来越多而变得越来越小 , 直至回到原来的位置 , 这么说应该可以理解了 , 接下来我们在XML文件中加入一个作为蒙版:

因为相当于帧布局是一层一层叠加的所以这个蒙版放在和FAB中间 , 整个布局代码:

看看效果:

Android Design Support Library

文章插图
效果
是不是有一个很奇怪的地方 , 知乎的FAB并没有弹出啊 , 那就说明一开始的思路错了 , 但是一个FAB只能设置一个app:  , 如果我们把这个用作FAB的旋转效果那么FAB的滚动移出视图的效果就没了 , 所以换一种思路 , 用动画来做FAB的旋转效果:
//开始旋转public void turnLeft(View v) {ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v, "rotation", 0, -155, -135);objectAnimator.setDuration(300);objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());objectAnimator.start();hide.setVisibility(View.VISIBLE);AlphaAnimation alphaAnimation = new AlphaAnimation(0, 0.75f);alphaAnimation.setDuration(300);alphaAnimation.setFillAfter(true);hide.startAnimation(alphaAnimation);hide.setClickable(true);isOpen = true;}//回到起始位置public void turnRight(View v) {ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v, "rotation", -135, 20, 0);objectAnimator.setDuration(300);objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());objectAnimator.start();hide.setVisibility(View.GONE);AlphaAnimation alphaAnimation = new AlphaAnimation(0.75f, 0);alphaAnimation.setDuration(300);alphaAnimation.setFillAfter(true);hide.startAnimation(alphaAnimation);hide.setClickable(false);isOpen = false;}//注:hide就是TextView控件(蒙版)
然后实现FAB的滚动移出视图效果的:
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {//先慢后快再慢private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();private boolean mIsAnimatingOut = false;public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {super();}//初始条件@Overridepublic boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,final View directTargetChild, final View target, final int nestedScrollAxes) {//垂直滚动return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);}@Overridepublic void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,final View target, final int dxConsumed, final int dyConsumed,final int dxUnconsumed, final int dyUnconsumed) {super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {// User scrolled down and the FAB is currently visible -> hide the FABanimateOut(child);} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {// User scrolled up and the FAB is currently not visible -> show the FABanimateIn(child);}}// Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exitsprivate void animateOut(final FloatingActionButton button) {if (Build.VERSION.SDK_INT >= 14) {//withLayer()使动画中的某些操作变得更顺畅,加速渲染,API 14以后ViewCompat.animate(button).translationY(button.getHeight() + getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {public void onAnimationStart(View view) {ScrollAwareFABBehavior.this.mIsAnimatingOut = true;}public void onAnimationCancel(View view) {ScrollAwareFABBehavior.this.mIsAnimatingOut = false;}public void onAnimationEnd(View view) {ScrollAwareFABBehavior.this.mIsAnimatingOut = false;view.setVisibility(View.GONE);}}).start();} else {}}// Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout entersprivate void animateIn(FloatingActionButton button) {button.setVisibility(View.VISIBLE);if (Build.VERSION.SDK_INT >= 14) {ViewCompat.animate(button).translationY(0).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();} else {}}private int getMarginBottom(View v) {int marginBottom = 0;final ViewGroup.LayoutParams layoutParams = v.getLayoutParams();if (layoutParams instanceof ViewGroup.MarginLayoutParams) {marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;}return marginBottom;}