前言

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直播软件搭建之实现背景颜色滑动渐变效果的详细代码
# 的是
# 如果你
# 好了
# 放在
# 也会
# 就像
# 很难
# 是指
# 要在
# 这篇文章
# 更容易
# 指的是
# 主要用于
# 如在
# 太不
# 就可以
# 邮件列表
# 系统自带
# 新邮件
# 有疑问
相关文章:
如何通过VPS建站实现广告与增值服务盈利?
建站之星如何防范黑客攻击与数据泄露?
如何通过建站之星自助学习解决操作问题?
已有域名和空间如何搭建网站?
如何通过.red域名打造高辨识度品牌网站?
中山网站制作网页,中山新生登记系统登记流程?
青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?
制作国外网站的软件,国外有哪些比较优质的网站推荐?
建站OpenVZ教程与优化策略:配置指南与性能提升
如何在腾讯云免费申请建站?
如何通过服务器快速搭建网站?完整步骤解析
已有域名建站全流程解析:网站搭建步骤与建站工具选择
GML (Geography Markup Language)是什么,它如何用XML来表示地理空间信息?
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
北京网站制作网页,网站升级改版需要多久?
建站之星手机一键生成:多端自适应+小程序开发快速建站指南
如何在阿里云高效完成企业建站全流程?
PHP 500报错的快速解决方法
网站制作的方法有哪些,如何将自己制作的网站发布到网上?
如何选择美橙互联多站合一建站方案?
如何通过wdcp面板快速创建网站?
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
宝塔新建站点为何无法访问?如何排查?
常州自助建站:操作简便模板丰富,企业个人快速搭建网站
ui设计制作网站有哪些,手机UI设计网址吗?
建站之星在线客服如何快速接入解答?
高端网站建设与定制开发一站式解决方案 中企动力
开心动漫网站制作软件下载,十分开心动画为何停播?
如何挑选最适合建站的高性能VPS主机?
如何快速搭建虚拟主机网站?新手必看指南
表情包在线制作网站免费,表情包怎么弄?
如何自定义建站之星模板颜色并下载新样式?
如何通过宝塔面板实现本地网站访问?
平台云上自主建站:模板化设计与智能工具打造高效网站
一键网站制作软件,义乌购一件代发流程?
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?
儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?
外贸公司网站制作哪家好,maersk船公司官网?
Python文件管理规范_工程实践说明【指导】
广东企业建站网站优化与SEO营销核心策略指南
建站之星2.7模板快速切换与批量管理功能操作指南
建站三合一如何选?哪家性价比更高?
三星网站视频制作教程下载,三星w23网页如何全屏?
兔展官网 在线制作,怎样制作微信请帖?
如何彻底卸载建站之星软件?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
javascript中对象的定义、使用以及对象和原型链操作小结
*请认真填写需求信息,我们会在24小时内与您取得联系。