import android.content.Context;
import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator; /**
* Created by Bruce Too
* On 6/2/15.
* At 09:14
* 只需要在 layout 中
* app:layout_behavior="com.support.android.designlibdemo.ScrollAwareFABBehavior" 使用就OK
*/ public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private boolean mIsAnimatingOut = false; public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
} /**
* 滚动开始的监听
* @param coordinatorLayout
* @param child 设置了Behavior得子view
* @param directTargetChild
* @param target 需要滚动的子view
* @param nestedScrollAxes 滚动轴方向 SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL
* @return
*/
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
// Ensure we react to vertical scrolling
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
} /**
* 滚动时的监听
* 每一个设置了Behavior 并且是 CoordinatorLayout的直接子view都会调用此方法
* 比如 FloatActionButton,AppBarLayout
* @param coordinatorLayout the CoordinatorLayout parent of the view this Behavior is associated with
* @param child the child view of the CoordinatorLayout this Behavior is associated with
* @param target the descendant view of the CoordinatorLayout performing the nested scroll
* @param dxConsumed horizontal pixels consumed by the target's own scrolling operation
* @param dyConsumed vertical pixels consumed by the target's own scrolling operation
* @param dxUnconsumed horizontal pixels not consumed by the target's own scrolling operation, but requested by the user
*/
@Override
public 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 FAB
animateOut(child);
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// User scrolled up and the FAB is currently not visible -> show the FAB
animateIn(child);
} /**
* 如果>=V22.2.1'
* 可以使用系统预设的动画
*/ // if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
// // User scrolled down and the FAB is currently visible -> hide the FAB
// child.hide();
// } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// // User scrolled up and the FAB is currently not visible -> show the FAB
// child.show();
// } } // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
private void animateOut(final FloatingActionButton button) {
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).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 {
Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_out);
anim.setInterpolator(INTERPOLATOR);
anim.setDuration(200L);
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
ScrollAwareFABBehavior.this.mIsAnimatingOut = true;
} public void onAnimationEnd(Animation animation) {
ScrollAwareFABBehavior.this.mIsAnimatingOut = false;
button.setVisibility(View.GONE);
} @Override
public void onAnimationRepeat(final Animation animation) {
}
});
button.startAnimation(anim);
}
} // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
private void animateIn(FloatingActionButton button) {
button.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
.setInterpolator(INTERPOLATOR).withLayer().setListener(null)
.start();
} else {
Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_in);
anim.setDuration(200L);
anim.setInterpolator(INTERPOLATOR);
button.startAnimation(anim);
}
}
}

FAB使用在CoordinatorLayout的隐藏动画的更多相关文章

  1. ActionBar compat 如何禁用ActionBar的显示/隐藏动画

    ActionBar compat 如何关闭ActionBar的显示隐藏动画 @Override public boolean onCreateOptionsMenu(Menu menu) { //消除 ...

  2. android view控件的显示和隐藏动画效果

    // 显示动画 mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO ...

  3. AngularJS中实现显示或隐藏动画效果的3种方式

    本篇体验在AngularJS中实现在"显示/隐藏"这2种状态切换间添加动画效果. 通过CSS方式实现显示/隐藏动画效果 思路: →npm install angular-anima ...

  4. jQuery动画之显示隐藏动画

    1. 显示动画 以下面一个代码示例: <!doctype html> <html lang="en"> <head> <meta char ...

  5. 教你三种jQuery框架实现元素显示及隐藏动画方式

    摘要:在jQuery框架中对元素对象进行显示和隐藏有三种方式,分别是"默认方式显示和隐藏"."滑动方式显示和隐藏"."淡入淡出显示和隐藏". ...

  6. 使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态。点击列表的项,切换二级列表的显示或隐藏状态

    查看本章节 查看作业目录 需求说明: 使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态.点击列表的项,切 ...

  7. 自定义CoordinatorLayout Behavior 隐藏Footer View

    在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...

  8. jqury+animation+setTimeOut实现渐变显示与隐藏动画

    初始效果 实现效果 1,编写HTMl结构代码 <div class="box"> <i class="icon"></i> ...

  9. android布局中显示隐藏动画

    android 在布局中提供属性,能简单的加入动画效果,例如以下: <LinearLayout ... animateLayoutChanges="true" ... /&g ...

随机推荐

  1. Migrate a Domain-based Namespace to Windows Server 2008 Mode

    TechNet Library Scripting with Windows PowerShell Windows and Windows Server Automation with Windows ...

  2. mof格式的文件怎么打开?用什么工具?

    托管对象格式 (MOF) 文件是创建和注册提供程序.事件类别和事件的简便方法.在 MOF 文件中创建类实例和类定义后,可以对该文件进行编译.有关更多信息,请参见编译托管对象格式 (MOF) 文件.编译 ...

  3. bash shell命令与监测的那点事(三)

    bash shell命令与监测的那点事之df与du 前两篇介绍了bash shell的进程监控指令,但是有时候你需要知道在某个设备上还有多少磁盘空间.首先介绍df命令: df命令 df命令就是用来轻松 ...

  4. Python面向对象之私有方法(4)

    类里面有很多成员修饰符,用来修饰各种属性 (1)私有属性,只有内部的方法可以访问 class Foo: xo = 'xo'#表明是公共的,内部外部都可以访问 __ox = '私有属性'#私有属性,只有 ...

  5. nmon系统监控

    nmon系统监控篇 本文目录 1 nmon的安装 2 nmon控制台指令 3 输出监控文件 一 下载nmon 本次使用的是nmon_linux_14i.tar.gz 二 放入linux中后解压 gzi ...

  6. Centos定时自动执行脚本

    检查本机crond的基本情况 1.crond的运行状况 2.crond是否开机自启动 如何将脚本添加进自动运行的时间内 直接编辑 vim /etc/crontab ,默认的文件形式如后面的图:   我 ...

  7. 一些filter

    Vue.filter('money', (value, symbol = '', currency = '¥', decimals = 0) => { const digitsRE = /(\d ...

  8. windows部分快捷键及terminal命令

    切换任务: win+tab terminal命令 pwd:打印当前工作目录 hostname:获取我的计算机的网络名称 mkdir:创建目录 cd:更改目录 ls/ dir -R:列出目录下的文件 r ...

  9. Python实现knn

    #coding:utf-8 import numpy as np import operator import os def classify0(inX, dataSet, labels, k): d ...

  10. iOS中block 静态全局局部变量block变量,以及对象,详解!

    //最近总是犯迷糊,关于block对外部变量的引用,今天有时间就写了一下,加深自己的理解,巩固基础知识 1 #import <Foundation/Foundation.h> ; int ...