|
1 | | -# BehaviorDemo |
2 | | -a A nice effect |
| 1 | +## Behavior 实现的漂亮的效果 |
| 2 | + |
| 3 | +### 简介 |
| 4 | + |
| 5 | +#### 建议 |
| 6 | +请先阅读[这篇文章](http://www.jianshu.com/p/f7989a2a3ec2) |
| 7 | +> 好多东西抄自这里 |
| 8 | +
|
| 9 | +#### 效果来源 |
| 10 | +[这里是地址](https://material.uplabs.com/posts/profile-4f03fc6b-1a82-42ab-8a3e-f50dcbc10253) |
| 11 | + |
| 12 | +[原图](!https://assets.materialup.com/uploads/210a9886-c3fd-4dd9-9077-628779eda61a/preview.gif) |
| 13 | + |
| 14 | +[实现的效果](!https://github.com/CSnowStack/BehaviorDemo/blob/master/img/c.gif) |
| 15 | + |
| 16 | + |
| 17 | +[项目地址](https://github.com/CSnowStack/BehaviorDemo) |
| 18 | + |
| 19 | +> 一些细节没有实现 ,见谅,录制的gif效果也不太好 :-( |
| 20 | +
|
| 21 | + |
| 22 | +### 实现 |
| 23 | + |
| 24 | +#### 依赖关系 |
| 25 | + `Tab` 监听 `onNestedPreScroll`来进行滑动,`Toolbar` 依赖 `FytContent`,其余的依赖 `Tab` |
| 26 | + |
| 27 | +#### 变化 |
| 28 | +- `Tab` 的移动是手指滑动距离的 1/2 ,会根据停下来的位置判断是应该回到原位置还是下一个状态并进行移动 |
| 29 | + |
| 30 | +- `VP` 跟随 `Tab`, `HeaderScrollingViewBehavior`什么的请看建议,移动则没什么难度 |
| 31 | + |
| 32 | +- `BGContent` 跟随 `Tab`,根据`Tab`运动的比例,缩放,移动,修改 里面`View`的`Alpha` |
| 33 | + |
| 34 | +- `BG` 跟随 `Tab`,首先向下移动到 `BGContent` 的高度的 1/2的地方 |
| 35 | + |
| 36 | +- `Editor` 跟随 `Tab`,首先移动到 `BGContent`下面加上预留的`Padding`,随着比例移动并设置alpha |
| 37 | + |
| 38 | +- `Icon` 跟随 `Tab`,根据`Tab`运动的比例进行移动和调整大小 |
| 39 | + |
| 40 | +- `Name` 同上 |
| 41 | + |
| 42 | +- `Socre` 同上,没有缩放 |
| 43 | + |
| 44 | + |
| 45 | +- `ToolBarIcon` 跟随`BGContent` ,根据`BGContent`移动的比例修改图标的`Alpha` |
| 46 | + |
| 47 | + |
| 48 | +#### 部分代码 |
| 49 | + |
| 50 | +```java |
| 51 | +//TabBehavior |
| 52 | +@Override |
| 53 | +public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) { |
| 54 | + super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed); |
| 55 | + //只要开始拦截,就需要把所有Scroll事件消费掉 |
| 56 | + consumed[1]=dy; |
| 57 | + int distance=-dy/2;//降低移动的速度 |
| 58 | + mUp=dy>0; |
| 59 | + |
| 60 | + if(child.getTranslationY()+distance<-mMaxDistance){ |
| 61 | + distance=-mMaxDistance; |
| 62 | + }else if(child.getTranslationY()+distance>0){ |
| 63 | + distance=0; |
| 64 | + }else { |
| 65 | + distance= (int) (child.getTranslationY()+distance); |
| 66 | + } |
| 67 | + child.setTranslationY(distance); |
| 68 | +} |
| 69 | + |
| 70 | +//ListBehavior |
| 71 | +@Override |
| 72 | +int getScrollRange(View v) { |
| 73 | + if (isDependOn(v)) { |
| 74 | + return -mHeightToolbar; |
| 75 | + } else { |
| 76 | + return super.getScrollRange(v); |
| 77 | + } |
| 78 | +} |
| 79 | +// HeaderScrollingViewBehavior$onMeasure 所以要返回 - mHeightToolbar |
| 80 | +final int height = availableHeight - header.getMeasuredHeight() |
| 81 | + + getScrollRange(header); |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +>代码是蛮简单的 ,直接看项目即可,就那几行代码 |
0 commit comments