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虚拟机,其实这个问题就和为什么要学习数据结构和算法是一个道理,工欲善其事,必先利其器.曾经的我经常害怕处理内存溢 ...
随机推荐
- bzoj2431: [HAOI2009]逆序对数列
dp. f[i][j]表示放置第i个数有j个逆序对的方案数. s[i][j]维护前缀和(f[i][0]~f[i][j]). 状态转移方程 f[i][j]=s[i-1][j]-s[i-1][max(j- ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- 自己用的框架写了一个PHP模版解析类
<?php if(!defined('IS_HEARTPHP')) exit('Access Denied'); /** * template.class.php 模板解析类 * * @copy ...
- 3732 Ahui Writes Word
// N个物品 放进容量为C的背包里面 要求价值最大// 一看 第一反应是0 1背包 不过 N=100000 C=10000// 注意到 v,c在 10以内// 那么 最多就100种组合了 然后就转化 ...
- memcached 最大连接数及其内存大小的设置
memcached的基本设置: -p 监听的端口-l 连接的IP地址, 默认是本机-d start 启动memcached服务-d restart 重起memcached服务-d stop|shutd ...
- 【DFS+记忆搜索】NYOJ-10-Skiing
[题目链接:NYOJ-10] skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑 ...
- 【转】iPhone屏幕尺寸、分辨率及适配
原文网址:http://blog.csdn.net/phunxm/article/details/42174937 1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角 ...
- MySQL row模式binlog复制原理
http://www.360doc.com/content/14/1107/14/12904276_423333021.shtml
- Generate Parentheses java实现
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- C语言指针5分钟教程
指针.引用和取值 什么是指针?什么是内存地址?什么叫做指针的取值?指针是一个存储计算机内存地址的变量.在这份教程里“引用”表示计算机内存地址.从指针指向的内 存读取数据称作指针的取值.指针可以指向某些 ...