iis服务器助手广告
返回顶部
首页 > 资讯 > 移动开发 >Android中Fab(FloatingActionButton)实现上下滑动的渐变效果
  • 301
分享到

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

渐变效果fabAndroid 2022-06-06 04:06:30 301人浏览 安东尼
摘要

前言 Promoted Actions是指一种操作按钮,它不是放在actionbar中,而是直接在可见的UI布局中(当然这里的UI指的是setContentView所管辖的范围

前言

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仿知乎悬浮功能按钮FloatingActionButton效果Android自定义可拖拽的悬浮按钮DragFloatingActionButton修改Android FloatingActionButton的title的文字颜色及背景颜色实例详解Android中FloatingActionButton实现悬浮按钮实例Android悬浮按钮点击返回顶部FloatingActionButtonAndroid自定义FloatingActionButton滑动行为只隐藏不出现的问题小结Android 中FloatingActionButton(悬浮按钮)实例详解Android中FloatingActionButton的显示与隐藏示例Android自定义APP全局悬浮按钮Android开发悬浮按钮 Floating ActionButton的实现方法Android开发之FloatingActionButton悬浮按钮基本使用、字体、颜色用法示例


--结束END--

本文标题: Android中Fab(FloatingActionButton)实现上下滑动的渐变效果

本文链接: https://www.lsjlt.com/news/22385.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何在Android中使用Fab(FloatingActionButton)方法实现一个滑动渐变效果
    本篇文章为大家展示了如何在Android中使用Fab(FloatingActionButton)方法实现一个滑动渐变效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Promoted Actions...
    99+
    2023-05-31
    android fab(floatingactionbutton) roi
  • Android实现背景颜色滑动渐变效果的全过程
    目录前言一、介绍一下GradientDrawable二、实现三、源码:总结前言 今天和朋友聊到这个功能,刚开始的想法是自定义view,如何进行滑动监听,经过一列操作完成效果后,发现...
    99+
    2024-04-02
  • Android实现颜色渐变动画效果
    目录前言一、Android中插值器TypeEvaluator二、案例效果实现1.利用Android自带的颜色插值器ArgbEvaluator2.看看Android自带颜色插值器Arg...
    99+
    2024-04-02
  • Android实现流动的渐变色边框效果
    目录前言实现思路总结前言 记得在介绍 motion_toast 一篇的时候,开篇有一张动图,边框是渐变色而且感觉是流动的。这个动效挺有趣的,当时也有人问怎么实现,经过上一篇《让你的聊...
    99+
    2024-04-02
  • Android Textview实现颜色渐变滚动效果
    本文实例为大家分享了Android颜色渐变滚动展示的具体代码,供大家参考,具体内容如下public class FlashTextView extends android.support.v7.widget.AppCompatTextVie...
    99+
    2023-05-30
    android textview 渐变
  • android小程序上滑下滑效果怎么实现
    今天小编给大家分享一下android小程序上滑下滑效果怎么实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。首先看gif图:...
    99+
    2023-06-29
  • Android怎么实现颜色渐变动画效果
    本篇内容主要讲解“Android怎么实现颜色渐变动画效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android怎么实现颜色渐变动画效果”吧!效果图:一、Android中插值器TypeEva...
    99+
    2023-06-30
  • android颜色渐变动画效果怎么实现
    在Android中,你可以使用ValueAnimator和ArgbEvaluator类来实现颜色渐变动画效果。下面是一个简单的示例代...
    99+
    2023-08-18
    android
  • Android实现京东上滑效果
    本文实例为大家分享了Android实现京东上滑效果的具体代码,供大家参考,具体内容如下 前言: 现在很多app首页的结构都有头部广告,上滑固定toolbar及侧滑广告位等展示,典型的...
    99+
    2024-04-02
  • Android实现平滑翻动效果
    本文实例为大家分享了Android实现平滑翻动效果的具体代码,供大家参考,具体内容如下 效果 1.activity加implements implements GestureD...
    99+
    2024-04-02
  • Android实现三段式滑动效果
    目录高德的效果:高德的效果: 实现的效果: 我们实现的效果和高德差距不是很大,也很顺滑。具体实现其实就是集成CoordinatorLayout.Behavior publi...
    99+
    2024-04-02
  • Android如何实现View滑动效果
    这篇文章给大家分享的是有关Android如何实现View滑动效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、View的滑动简介View的滑动是Android实现自定义控件的基础,同时在开发中我们也难免会遇到...
    99+
    2023-06-14
  • Android Scroller实现弹性滑动效果
    本文实例为大家分享了Android Scroller实现弹性滑动的具体代码,供大家参考,具体内容如下 首先看下实现效果,可以看到当我们手指松开时图片会逐渐滑动到初始位置,而不是直接跳...
    99+
    2024-04-02
  • Android直播软件搭建之实现背景颜色滑动渐变效果的详细代码
    Android直播软件搭建实现背景颜色滑动渐变效果的相关代码 一、介绍一下GradientDrawable GradientDrawable 支持渐变色的Drawable,与sha...
    99+
    2024-04-02
  • Android实现状态栏(statusbar)渐变效果的示例
    前言qq最近更新搞了渐变式状态栏.然后...新需求就是要加这个.唉先来张效果图:常见的方式:设置Theme,状态栏透明. <item name="android:windowTranslucentStatus">true<...
    99+
    2023-05-30
    android 状态栏 渐变
  • Android自定义Span实现文字渐变效果
    目录前言ForegroundColorSpan解析文本颜色动画渐变样式实现小结前言 Android提供一些Span设置文本样式外,开发者若希望实现一些新特性也是能自定义开发实现的。只...
    99+
    2024-04-02
  • 如何通过纯CSS实现网页的平滑滚动背景渐变效果
    如何通过纯CSS实现网页的平滑滚动背景渐变效果一、引言在网页设计中,背景渐变效果可以为网站增加美感和动态感。而平滑滚动背景渐变则可以使网页更加吸引人,给用户带来舒适的浏览体验。本文将介绍如何通过纯CSS实现网页的平滑滚动背景渐变效果,并提供...
    99+
    2023-10-25
    CSS 平滑滚动 背景渐变
  • Android实现背景图滑动变大松开回弹效果
    本文实例为大家分享了Android实现背景图滑动变大松开回弹的具体代码,供大家参考,具体内容如下 原图 放大后 1、自定义view继承ScrollView实现效果 public ...
    99+
    2024-04-02
  • Android如何实现渐变色水波纹效果
    这篇文章主要介绍了Android如何实现渐变色水波纹效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。项目中使用到的效果,效果图如下:代码实现:public cla...
    99+
    2023-06-21
  • Android实现探探图片滑动效果
    之前一段时间,在朋友的推荐下,玩了探探这一款软件,初玩的时候,就发现,这款软件与一般的社交软件如陌陌之类的大相径庭,让我耳目一新,特别是探探里关于图片滑动操作让人觉得非常新鲜。所以在下通过网上之前的前辈的经历加上自己的理解,也来涉涉水。下面...
    99+
    2023-05-31
    android 图片滑动 roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作