软件项目经理面试技巧,这交互炸了系列(1),深入解析Android-AutoLayout

作者这个系列一共 4 篇,2 篇基础,2 篇实战,如果你能完全吸收,基本玩转嵌套滚动。

PS:感谢大家,昨天很给力,让我褥了好几年…

_1_概述

之前的《浅析NestedScrolling嵌套滑动机制之CoordinatorLayout.Behavior》带大家了解CoordinatorLayout.Behavior的原理和基本使用,这篇文章手把手基于自定义Behavior实现小米音乐歌手详情页。

效果预览


效果分析


布局主要有上图四部分组成,逻辑上的层级如图所示有2层。

滑动Content部分时利用View的TransitionY属性改变位置来消耗滑动值,而Face、TopBar、TitleBar部分的Behavior依赖Content,监听Content部分的TransitionY设定各种范围从而计算百分比来执行位移、Alpha效果。

下面来说明上图中变量的意义:

topBarHeight;//topBar高度

contentTransY;//滑动内容初始化TransY

downEndY;//content下滑的最大值

content部分的上滑范围=[topBarHeight,contentTransY]

content部分的下滑范围=[contentTransY,downEndY]

_2_代码实现

布局

下面是布局要点,侧重于控件的尺寸和位置,完整布局请参考:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<FrameLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

app:layout_behavior=“com.pengguanming.mimusicbehavior.behavior.FaceBehavior”>

<ImageView

android:id=“@+id/iv_face”

android:layout_width=“match_parent”

android:layout_height=“500dp”

android:scaleType=“centerCrop”

android:src=“@mipmap/jj”

android:tag=“iv_face”

android:translationY=“@dimen/face_trans_y” />

<View

android:id=“@+id/v_mask”

android:layout_width=“match_parent”

android:layout_height=“500dp” />

<com.pengguanming.mimusicbehavior.widget.TopBarLayout

android:id=“@+id/cl_top_bar”

android:layout_width=“match_parent”

android:layout_height=“@dimen/top_bar_height”

app:layout_behavior=“com.pengguanming.mimusicbehavior.behavior.TopBarBehavior”>

<ImageView

android:id=“@+id/iv_back”

… />

<TextView

android:id=“@+id/tv_top_bar_name”

a… />

<com.pengguanming.mimusicbehavior.widget.DrawableLeftTextView

android:id=“@+id/tv_top_bar_coll”

…/>

</com.pengguanming.mimusicbehavior.widget.TopBarLayout>

<android.support.constraint.ConstraintLayout

android:id=“@+id/cls_title_bar”

android:layout_width=“match_parent”

android:layout_height=“48dp”

app:layout_behavior=“com.pengguanming.mimusicbehavior.behavior.TitleBarBehavior”>

<TextView

…/>

<com.pengguanming.mimusicbehavior.widget.DrawableLeftTextView

…/>

</android.support.constraint.ConstraintLayout>

<LinearLayout

android:id=“@+id/ll_content”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

android:translationY=“@dimen/content_trans_y”

app:layout_behavior=“com.pengguanming.mimusicbehavior.behavior.ContentBehavior”>

<com.flyco.tablayout.SlidingTabLayout

android:id=“@+id/stl”

…/>

<android.support.v4.view.ViewPager

android:id=“@+id/vp”

… />

</android.support.design.widget.CoordinatorLayout>

ContentBehavior

这个Behavior主要处理Content部分的Measure、嵌套滑动。

绑定需要做效果的View、引入Dimens、测量Content部分的高度

从上面图片能够分析出:

折叠状态时,Content部分高度=满屏高度-TopBar部分的高度

public class ContentBehavior extends CoordinatorLayout.Behavior{

private int topBarHeight;//topBar内容高度

private float contentTransY;//滑动内容初始化TransY

private float downEndY;//下滑时终点值

private View mLlContent;//Content部分

public ContentBehavior(Context context) {

this(context, null);

}

public ContentBehavior(Context context, AttributeSet attrs) {

super(context, attrs);

//引入尺寸值

int resourceId = context.getResources().getIdentifier(“status_bar_height”, “dimen”, “android”);

int statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);

topBarHeight= (int) context.getResources().getDimension(R.dimen.top_bar_height)+statusBarHeight;

contentTransY= (int) context.getResources().getDimension(R.dimen.content_trans_y);

downEndY= (int) context.getResources().getDimension(R.dimen.content_trans_down_end_y);

}

@Override

public boolean onMeasureChild(@NonNull CoordinatorLayout parent, View child,

int parentWidthMeasureSpec, int widthUsed,

int parentHeightMeasureSpec,int heightUsed) {

final int childLpHeight = child.getLayoutParams().height;

if (childLpHeight == ViewGroup.LayoutParams.MATCH_PARENT

|| childLpHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {

//先获取CoordinatorLayout的测量规格信息,若不指定具体高度则使用CoordinatorLayout的高度

int availableHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);

if (availableHeight == 0) {

availableHeight = parent.getHeight();

}

//设置Content部分高度

final int height = availableHeight - topBarHeight;

final int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height,

childLpHeight == ViewGroup.LayoutParams.MATCH_PARENT

? View.MeasureSpec.EXACTLY
View.MeasureSpec.AT_MOST);

//执行指定高度的测量,并返回true表示使用Behavior来代理测量子View

parent.onMeasureChild(child, parentWidthMeasureSpec,

widthUsed, heightMeasureSpec, heightUsed);

return true;

}

return false;

}

@Override

public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child, int layoutDirection) {

boolean handleLayout = super.onLayoutChild(parent, child, layoutDirection);

//绑定Content View

mLlContent=child;

return handleLayout;

}

}

实现NestedScrollingParent2接口
onStartNestedScroll()

ContentBehavior只处理Content部分里可滑动View的垂直方向的滑动。

public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,

@NonNull View directTargetChild, @NonNull View target, int axes, int type) {

//只接受内容View的垂直滑动

return directTargetChild.getId() == R.id.ll_content

&&axes== ViewCompat.SCROLL_AXIS_VERTICAL;

}

onNestedPreScroll()

接下来就是处理滑动,上面效果分析提过:

Content部分的:

上滑范围=[topBarHeight,contentTransY]、

下滑范围=[contentTransY,downEndY]即滑动范围为[topBarHeight,downEndY];

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

【附】相关架构及资料

往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。**

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

  • 21
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值