android悬浮按钮(Floating action button)的两种实现方法
原文:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1028/1857.html
最近android中有很多新的设计规范被引入,最流行的莫过于被称作Promoted Actions的设计了,Promoted Actions是指一种操作按钮,它不是放在actionbar中,而是直接在可见的UI布局中(当然这里的UI指的是setContentView所管辖的范围)。因此它更容易在代码中被获取到(试想如果你要在actionbar中获取一个菜单按钮是不是很难?),Promoted Actions往往主要用于一个界面的主要操作,比如在email的邮件列表界面,promoted action可以用于接受一个新邮件。promoted action在外观上其实就是一个悬浮按钮,更常见的是漂浮在界面上的圆形按钮,一般我直接将promoted action称作悬浮按钮,英文名称Float Action Button 简称(FAB,不是FBI哈)。
float action button是android l中的产物,但是我们也可以在更早的版本中实现。假设我这里有一个列表界面,我想使用floataction button代表添加新元素的功能,界面如下:

要实现float action button可以有多种方法,一种只适合android L,另外一种适合任意版本。
用ImageButton实现
这种方式其实是在ImageButton的属性中使用了android L才有的一些特性:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<ImageButtonandroid:layout_width="56dp"android:layout_height="56dp"android:src="@drawable/plus"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginRight="16dp"android:layout_marginBottom="16dp"android:tint="@android:color/white"android:id="@+id/fab"android:elevation="1dp"android:background="@drawable/ripple"android:stateListAnimator="@anim/fab_anim"/> |
仔细一点,你会发现我们将这个ImageButton放到了布局的右下角,为了实现float action button应该具备的效果,需要考虑以下几个方面:
·Background
·Shadow
·Animation
背景上我们使用ripple drawable来增强吸引力。注意上面的xml代码中我们将background设置成了@drawable/ripple ,ripple drawable的定义如下:
|
1
2
3
4
5
6
7
|
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight"> <item> <shape android:shape="oval"> <solid android:color="?android:colorAccent" /> </shape> </item></ripple> |
既然是悬浮按钮,那就需要强调维度上面的感觉,当按钮被按下的时候,按钮的阴影需要扩大,并且这个过程是渐变的,我们使用属性动画去改变translatioz。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<item android:state_enabled="true" android:state_pressed="true"> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="translationZ" android:valueFrom="@dimen/start_z" android:valueTo="@dimen/end_z" android:valueType="floatType" /> </item> <item> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="translationZ" android:valueFrom="@dimen/end_z" android:valueTo="@dimen/start_z" android:valueType="floatType" /> </item></selector> |
使用自定义控件的方式实现悬浮按钮
这种方式不依赖于android L,而是码代码。
首先定义一个这样的类:
|
1
2
3
|
public class CustomFAB extends ImageButton {...} |
然后是读取一些自定义的属性(假设你了解styleable的用法)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private void init(AttributeSet attrSet) { Resources.Theme theme = ctx.getTheme(); TypedArray arr = theme.obtainStyledAttributes(attrSet, R.styleable.FAB, 0, 0); try { setBgColor(arr.getColor(R.styleable.FAB_bg_color, Color.BLUE)); setBgColorPressed(arr.getColor(R.styleable.FAB_bg_color_pressed, Color.GRAY)); StateListDrawable sld = new StateListDrawable(); sld.addState(new int[] {android.R.attr.state_pressed}, createButton(bgColorPressed)); sld.addState(new int[] {}, createButton(bgColor)); setBackground(sld); } catch(Throwable t) {} finally { arr.recycle(); }} |
在xml中我们需要加入如下代码,一般是在attr.xml文件中。
|
1
2
3
4
5
6
7
8
|
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="FAB"> <!-- Background color --> <attr name="bg_color" format="color|reference"/> <attr name="bg_color_pressed" format="color|reference"/> </declare-styleable></resources> |
使用StateListDrawable来实现不同状态下的背景
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
private Drawable createButton(int color) { OvalShape oShape = new OvalShape(); ShapeDrawable sd = new ShapeDrawable(oShape); setWillNotDraw(false); sd.getPaint().setColor(color); OvalShape oShape1 = new OvalShape(); ShapeDrawable sd1 = new ShapeDrawable(oShape); sd1.setShaderFactory(new ShapeDrawable.ShaderFactory() { @Override public Shader resize(int width, int height) { LinearGradient lg = new LinearGradient(0,0,0, height, new int[] { Color.WHITE, Color.GRAY, Color.DKGRAY, Color.BLACK }, null, Shader.TileMode.REPEAT); return lg; } }); LayerDrawable ld = new LayerDrawable(new Drawable[] { sd1, sd }); ld.setLayerInset(0, 5, 5, 0, 0); ld.setLayerInset(1, 0, 0, 5, 5); return ld;} |
最后将控件放xml中:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyActivity">... <com.survivingwithandroid.fab.CustomFAB android:layout_width="56dp" android:layout_height="56dp" android:src="@android:drawable/ic_input_add" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:layout_marginBottom="16dp" custom:bg_color="@color/light_blue" android:tint="@android:color/white" /></RelativeLayout> |
项目的源码下载:github
android悬浮按钮(Floating action button)的两种实现方法的更多相关文章
- $Android启动界面(Splash)的两种实现方法
(一)用2个Activity实现 用Handler对象的postDelayed方法来实现延迟跳转的目的. 补充:Handler的常用方法: // 立即执行Runnable对象 public final ...
- 将文件放到Android模拟器的SD卡中的两种解决方法
两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...
- Android Design Support Library——Floating Action Button
Floating Action Button是一种悬浮操作的圆形按钮,继承自ImageView,可以通过android:src或者ImageView的任意方法,来设置FloatingActionBut ...
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
一.前言 MAUI,跨平台的 GUI 框架,基本介绍本文不再赘述. 话不多说,既然可以跨平台,那么我们就来实现一个在移动端很常用的控件:悬浮操作按钮(FAB,Floating Action Butto ...
- swift项目实战--微博的未登录界面的实现,和监听未登录界面两个按钮的两种实现方法
1.未登录界面的实现 微博项目中,用户不登录的话,显示的是未登录的界面.项目中TabBarVC的子控制器都是tableViewVC,所以抽取了父类,让父类判断用户是否登录,决定显示什么样的界面.loa ...
- Android中退出多个Activity的两个经典方法
这里介绍两种方法:一种把每个activity记住,然后逐一干掉:另一种思路是使用广播. 方法一.用list保存activity实例,然后逐一干掉 上代码: import java.util.Linke ...
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- android studio gradle 两种更新方法更新
android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...
随机推荐
- 获取目标字符串在字符串中第N次出现的位置
/** * 获取目标字符串在字符串中第N次出现的位置 * @file name * @author xiehongwei * @date 2017-8-2 下午3:29:09 * @param sou ...
- 安装rpm包时提示错误:依赖检测失败的解决方法
安装rpm包时提示错误:依赖检测失败 解决方法: 命令末尾加上--nodeps --force
- logging basic
logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等. 相比print,具备如下优点: 可以通过设置不同的日志等级, ...
- nginx反向代理配置及常见指令
nginx配置文件: /usr/local/nginx1.16.1/conf/nginx.conf nginx.conf默认的server配置: server{ listen 80; server_n ...
- celery task - 2
# celery task 前言 讨论一个定时任务,一般而言,需要的功能如下: 封装成对象,独立执行: 对象有一些接口,便于了解它的状态: 定时调用: 行为控制,包括重试,成功/失败回调等: 下面分别 ...
- tensorflow 学习记录
函数变动 tf.train.SummaryWriter 变为 tf.summary.Filewritter 函数功能相同,仅仅是简单的重命名 ``` writer = tf.summary.FileW ...
- about Base64
用webservice传送文件的时候发现,如果发送的文件中有0x00字符,会被认为是字符串结尾,后面的内容就发送不过去,因此需要对不是纯文本格式的文件做BASE64编码,这样文件中就不会有0x00这样 ...
- js函数声明外面使用小括号括起来再接一个小括号的写法
js函数声明外面使用小括号括起来再接一个小括号的写法 (function(){})(); (function(){}()); !function(){}(); 总结ps:意思将函数声明变成,直接执行的 ...
- CSS3实现魔方动画
本文将借助css3实现魔方动画效果,设计思路如下: HTML方面采用六个div容器形成六个立方面: CSS方面采用transform-style: preserve-3d;形成三维场景:transfo ...
- Django 实现下载功能时中文文件名问题
先上最终解决代码(有待验证各浏览器效果): def download_file(request, file_path): file_name = os.path.basename(file_path) ...