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

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的