Android中Fab(FloatingActionButton)实现上下滑动的渐变效果
发布时间 - 2026-01-10 22:58:40 点击率:次前言

Promoted Actions是指一种操作按钮,它不是放在actionbar中,而是直接在可见的UI布局中(当然这里的UI指的是setContentView所管辖的范围)。因此它更容易在代码中被获取到(试想如果你要在actionbar中获取一个菜单按钮是不是很难?),Promoted Actions往往主要用于一个界面的主要操作,比如在email的邮件列表界面,promoted action可以用于接受一个新邮件。promoted action在外观上其实就是一个悬浮按钮,更常见的是漂浮在界面上的圆形按钮,一般我直接将promoted action称作悬浮按钮,英文名称Float Action Button 简称(FAB,不是FBI哈)。
系统自带的 Fab 也会随着页面上下滚动,但是淡出或者进入的效果太不自然。
这里记录一个小知识点,Fab 随着页面的 RecyclerView 上下滚动而渐变的动画效果。
包含 Fab 控件的布局如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".view.activity.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" /> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" app:tabIndicatorColor="#FFFFFF" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" app:layout_behavior="com.wu.allen.zhuanlan.util.ScrollAwareFABBehavior"/> </android.support.design.widget.CoordinatorLayout>
实现的 Java 代码如下:
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private boolean mIsAnimatingOut = false;
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
}
@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);
}
@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);
}
}
// 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_in.xml 文件如下(fab_out.xml 同理),当然要改变淡出或者进入的样式,一般修改这里的 XML 文件就可以了 :
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0"/> <scale android:fromXScale="0.0" android:fromYScale="0.0" android:toXScale="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%"/> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0"/> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="0.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%"/> </set>
大致效果就像上面。
总结
好了,以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。
# android
# 上下滑动渐变
# fab
# fab动态效果
# Android实现状态栏和虚拟按键背景颜色的变化实例代码详解
# 修改Android FloatingActionButton的title的文字颜色及背景颜色实例详解
# Android实现沉浸式通知栏通知栏背景颜色跟随app导航栏背景颜色而改变
# Android设置PreferenceCategory背景颜色的方法
# Android之scrollview滑动使标题栏渐变背景色的实例代码
# Android 顶部标题栏随滑动时的渐变隐藏和渐变显示效果
# Android中Toolbar随着ScrollView滑动透明度渐变效果实现
# Android直播软件搭建之实现背景颜色滑动渐变效果的详细代码
# 的是
# 如果你
# 好了
# 放在
# 也会
# 就像
# 很难
# 是指
# 要在
# 这篇文章
# 更容易
# 指的是
# 主要用于
# 如在
# 太不
# 就可以
# 邮件列表
# 系统自带
# 新邮件
# 有疑问
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】
PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑
php8.4header发送头信息失败怎么办_php8.4header函数问题解决【解答】
百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧
Bootstrap整体框架之CSS12栅格系统
活动邀请函制作网站有哪些,活动邀请函文案?
Laravel如何记录自定义日志?(Log频道配置)
Laravel Asset编译怎么配置_Laravel Vite前端构建工具使用
Python面向对象测试方法_mock解析【教程】
javascript中对象的定义、使用以及对象和原型链操作小结
简历没回改:利用AI润色让你的文字更专业
Laravel的HTTP客户端怎么用_Laravel HTTP Client发起API请求教程
如何为不同团队 ID 动态生成多个“认领值班”按钮
详解vue.js组件化开发实践
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口
微信小程序制作网站有哪些,微信小程序需要做网站吗?
安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出
Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能
html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】
七夕网站制作视频,七夕大促活动怎么报名?
公司网站制作需要多少钱,找人做公司网站需要多少钱?
Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能
Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】
如何在腾讯云免费申请建站?
DeepSeek是免费使用的吗 DeepSeek收费模式与Pro版本功能详解
在线制作视频网站免费,都有哪些好的动漫网站?
java中使用zxing批量生成二维码立牌
高性能网站服务器配置指南:安全稳定与高效建站核心方案
齐河建站公司:营销型网站建设与SEO优化双核驱动策略
西安专业网站制作公司有哪些,陕西省建行官方网站?
Laravel storage目录权限问题_Laravel文件写入权限设置
Laravel怎么配置自定义表前缀_Laravel数据库迁移与Eloquent表名映射【步骤】
微信公众帐号开发教程之图文消息全攻略
Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制
香港服务器租用费用高吗?如何避免常见误区?
高防服务器租用首荐平台,企业级优惠套餐快速部署
php 三元运算符实例详细介绍
Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验
Laravel API路由如何设计_Laravel构建RESTful API的路由最佳实践
大连网站制作公司哪家好一点,大连买房网站哪个好?
做企业网站制作流程,企业网站制作基本流程有哪些?
如何在云主机快速搭建网站站点?
Python高阶函数应用_函数作为参数说明【指导】
如何在云指建站中生成FTP站点?
合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?
Laravel如何创建和注册中间件_Laravel中间件编写与应用流程
Google浏览器为什么这么卡 Google浏览器提速优化设置步骤【方法】
Laravel如何升级到最新版本?(升级指南和步骤)

