FloatingActionButton的一点学习感悟
最近在学习android材料设计的新控件,前面一篇文章讲到 CoordinatorLayout 结合几个新控件可以实现的几个效果。其中第一个是,Coordinatorlayout + FloatingActionButton,配合使用,当弹出 Snackbar 的时候,FloatingActionBar会跟随上移和下移。这次再针对 FloatingActionButton 具体分析一下。先贴出布局文件和java代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btnSnackBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击弹出SnackBar"/> <android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@mipmap/ic_launcher" /> </android.support.design.widget.CoordinatorLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sixth);
((Button) findViewById(R.id.btnSnackBar)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v,"这是一个snackBar",Snackbar.LENGTH_SHORT).show();
}
});
}
}
代码很简洁,效果如下:

这个效果只有在CoordinatorLayout下,FloatingActionButton 配合 Snackbar 使用才会有。
我们知道,FloatingActionButton 间接继承自 ImagButton, 如果将 FloatingActionButton 换成常用的 ImagButton 或者 ImageView,效果将不存在。为什么会这样呢。在 FloatingActionButton 的定义中发现,通过反射,利用注解注入了一个 behavior 属性。FloatingActionButton 类中定义了一个内部类——Behaivor,继承自 CoordinatorLayout.Behavior<FloatingActionButton> 。里面有两个方法如下:
@Override
public boolean layoutDependsOn(CoordinatorLayout parent,FloatingActionButton child, View dependency) {
// We're dependent on all SnackbarLayouts (if enabled)
return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
} @Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,View dependency) {
if (dependency instanceof Snackbar.SnackbarLayout) {
updateFabTranslationForSnackbar(parent, child, dependency);
} else if (dependency instanceof AppBarLayout) {
// If we're depending on an AppBarLayout we will show/hide it automatically
// if the FAB is anchored to the AppBarLayout
updateFabVisibility(parent, (AppBarLayout) dependency, child);
}
return false;
} private float getFabTranslationYForSnackbar(CoordinatorLayout parent,FloatingActionButton fab) {
float minOffset = 0;
final List<View> dependencies = parent.getDependencies(fab);
for (int i = 0, z = dependencies.size(); i < z; i++) {
final View view = dependencies.get(i);
if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) {
minOffset = Math.min(minOffset,ViewCompat.getTranslationY(view) - view.getHeight());
}
}
return minOffset;
}
layoutDependsOn 方法,判断底下弹出的是不是snackbar,如果是,floatingactionbutton才会联动。
当需要联动的snackbar向上移动的过程中,会不断调用 onDependentViewChanged 方法。
getFabTranslationYForSnackbar 用来获取SnackBar逐渐弹出来的时候变化的高度。
明白了这三个方法之后,我们就可以对这个效果进行修改写。下面我们自己写一个类,让其继承自 CoordinatorLayout.Behavior<FloatingActionButton>,
让FloatingActionButton在上升过程中实现随高度变化旋转效果。
代码如下:
public class MyBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
/**
* Default constructor for instantiating Behaviors.
*/
public MyBehavior() {
}
/**
* Default constructor for inflating Behaviors from layout. The Behavior will have
* the opportunity to parse specially defined layout parameters. These parameters will
* appear on the child view tag.
*
* @param context
* @param attrs
*/
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 判断底下弹出的是不是snackbar,如果是,floatingactionbutton才会联动
*
* @param parent
* @param child
* @param dependency
* @return
*/
@Override
public boolean layoutDependsOn(CoordinatorLayout parent,
FloatingActionButton child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
/**
* 当需要联动的snackbar向上移动的过程中,会不断调用这个方法
*
* @param parent
* @param child
* @param dependency
* @return
*/
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,
View dependency) {
float fTransY = getFabTranslationYForSnackbar(parent, child);
float iRate = -fTransY / dependency.getHeight();
child.setRotation(iRate * 90);
child.setTranslationY(fTransY);
return false;
}
/**
* 用来获取SnackBar逐渐弹出来的时候变化的高度
*
* @param parent
* @param fab
* @return
*/
private float getFabTranslationYForSnackbar(CoordinatorLayout parent,
FloatingActionButton fab) {
float minOffset = 0;
final List<View> dependencies = parent.getDependencies(fab);
for (int i = 0, z = dependencies.size(); i < z; i++) {
final View view = dependencies.get(i);
if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) {
minOffset = Math.min(minOffset,
ViewCompat.getTranslationY(view) - view.getHeight());
}
}
return minOffset;
}
}
然后修改布局文件,为 FloatingActionButton 设置 layout_behavior 属性为自定义的MyBehavior,
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"> <Button
android:id="@+id/btnSnackBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击弹出SnackBar"/> <android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:layout_behavior="com.example.joy.coordinatorlayouttest.MyBehavior"
android:src="@mipmap/ic_launcher" /> </android.support.design.widget.CoordinatorLayout>
效果如下:

FloatingActionButton的一点学习感悟的更多相关文章
- C#与JAVA学习感悟
C#与JAVA学习感悟 学完C#与JAVA,感觉收获良多.C#与JAVA这两门语言相似度很高(了解它们早期历史的人可能知道为什么),也许很多人在学习JAVA(或C#)时会同时学习C#(或JAVA),因 ...
- 20155226田皓宇关于优秀技能经验以及c语言学习感悟和对JAVA的展望
读老师文章后关于一项优秀技能的经验有感 1.首先我自我剖析认为,我是没有哪一个方面能做到强于身边90%的人的,我只能说有些方面略强于身边的人.比如唱歌.办公软件的应用(word.excel)等.但我不 ...
- imooc-c++学习感悟
imooc--慕课网c++课程链接:[课程链接](http://www.imooc.com/course/list?c=C+puls+puls) Imooc 慕课网c++学习感悟 1.课程名称:c++ ...
- 关于Set和Map数据结构的一点学习
关于js的Set和Map结构的学习和记录 对阮一峰老师的ES6入门和网上有关资料的的一点学习和记录 1.Set数据结构 Set构造函数的参数是一个可遍历( iterator)对象 Set中的成员值是唯 ...
- 以小时候玩的贪吃蛇为例,对于Java图像界面的学习感悟
简介 正文 01.JFrame是啥? 02.JPanel 03. KeyListener 04.Runnable 05.游戏Running 06.游戏初始类编写 07.main 简介: 一直以来用代码 ...
- 我对android开发的一点小感悟小看法
“Android”,“Android开发”等等这些词成了时下最热的词,也是时下大众最关注最吸引人眼球的话题,当然,最热门的行业也意味着高薪,好的就业环境,但同时也意味着强大的竞争力! Android系 ...
- C#学习感悟
上周虽然没上课,课上的内容是部分同学展示大作业成果,但是对于我来说,看了一些同学辛勤劳动的成果,听了他们对C#学习的一些感悟,我受益匪浅. 在这里我想谈谈我的收获.老师给的模板是todolist,但是 ...
- 老学员的学习感悟 --prince2认证有什么用
2007年一月,我加入了荷兰Irdeto(中国)有限公司.刚入公司,我就结识了PRINCE2(受控环境下的项目管理),才知道prince2是英国政府在政府项目中使用的项目管理标准.这一标准早已在欧洲广 ...
- 深入理解Java虚拟机---学习感悟以及笔记
一.为什么要学习Java虚拟机? 这里我们使用举例来说明为什么要学习Java虚拟机,其实这个问题就和为什么要学习数据结构和算法是一个道理,工欲善其事,必先利其器.曾经的我经常害怕处理内存溢 ...
随机推荐
- LinQ综合应用实例
直接上代码,内容很浅显易懂,在这里就不做更多的解释,解释见代码注释. using System; using System.Collections.Generic; using System.Linq ...
- BZOJ_1028_[JSOI2007]_麻将_(模拟+贪心)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1028 同一种花色的牌,序数为\(1,2,...,n\).定义"和了"为手上 ...
- Android ashmem hacking
/********************************************************************** * Android ashmem hacking * 声 ...
- UVA 550 Multiplying by Rotation (简单递推)
题意:有些数字是可以这样的:abcd*k=dabc,例如179487 * 4 = 717948,仅仅将尾数7移动到前面,其他都不用改变位置及大小.这里会给出3个数字b.d.k,分别代表b进制.尾数.第 ...
- T-SQL备忘(1):表联接
测试用例表如下: 1.取2个成员表中的交集(A∩B) T-SQL: select Member1.Name,Member1.Age from Member1 join Member2 on Membe ...
- 通过userAgent判断手机浏览器类型
我们可以通过userAgent来判断,比如检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些浏览器的userAgent中并不包含AppleWe ...
- MyBatis学习 之 三、动态SQL语句
目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...
- 编程式事务、XML配置事务、注解实现事务
Spring2.0框架的事务处理有两大类: 1 编码式事务 , 这个不说. 2 声明式事务 , 就说这个. 声明式事务又有三种实现方法: 1 (第一种) 最早的方法,用TransactionProxy ...
- java移动/赋值文件 copy/move file
public class FileAccess { public static boolean Move(File srcFile, String destPath) { // Destination ...
- 修改Zabbix默认运行账户
默认Zabbix运行的账户是Zabbix,但在自动部署的时候,Agent与Server的先后顺序不定,而且官方不建议两者使用同一个账户. 所以,解压压缩包后,进入目录: vi configure ...