android滑动布局渐变,Android中Fab(FloatingActionButton)实现上下滑动的渐变效果

本文主要是介绍android滑动布局渐变,Android中Fab(FloatingActionButton)实现上下滑动的渐变效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

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 控件的布局如下:

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:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

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:id="@+id/tab_layout"

app:tabIndicatorColor="#FFFFFF"

android:layout_width="match_parent"

android:layout_height="wrap_content">

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"/>

实现的 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 文件就可以了 :

android:toAlpha="1.0"/>

android:fromYScale="0.0"

android:toXScale="1.0"

android:toYScale="1.0"

android:pivotX="50%"

android:pivotY="50%"/>

android:toAlpha="0.0"/>

android:fromYScale="1.0"

android:toXScale="0.0"

android:toYScale="0.0"

android:pivotX="50%"

android:pivotY="50%"/>

29da25b818ba7b373141d7db9bb962ee.gif

大致效果就像上面。

总结

好了,以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。

这篇关于android滑动布局渐变,Android中Fab(FloatingActionButton)实现上下滑动的渐变效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/728982

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景