Android Compose實(shí)現(xiàn)伸縮ToolBar的思路詳解
ScrollableAppBar
效果圖

- 當(dāng)列表向上移動(dòng)時(shí),會(huì)先帶動(dòng)ToolBar向上位移,等ToolBar向上移動(dòng)到最大位移量時(shí)列表向上滑動(dòng)
- 當(dāng)列表向下移動(dòng)時(shí),會(huì)先帶動(dòng)ToolBar向下位移,等ToolBar向下移動(dòng)到最大位移量時(shí)列表向下滑動(dòng)
主要思路
布局預(yù)覽
伸縮前布局:

伸縮后布局:

實(shí)現(xiàn)過(guò)程
布局實(shí)現(xiàn)
首先我們要定義兩個(gè)尺寸變量
// 應(yīng)用欄高度 private val toolBarHeight = 56.dp // 導(dǎo)航圖標(biāo)大小 private val navigationIconSize = 50.dp
我們采用Box作為根布局,里面主要包含三個(gè)部分,背景圖片,頂部的TooBar以及下面的Title部分,其實(shí)現(xiàn)如下
//整體布局實(shí)現(xiàn)
Box(modifier = Modifier
.height(scrollableAppBarHeight) //scrollableAppBarHeight 為高度參數(shù),為外部獲取
.fillMaxWidth()
) {
Image(painter = painterResource(id = backgroundImageId), contentDescription = "background", contentScale = ContentScale.FillBounds)
// 自定義應(yīng)用欄
Row(
modifier = modifier
.height(toolBarHeight) //設(shè)置高度為toolBarHeight
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically //設(shè)置垂直方向?yàn)榫又袑?duì)齊
) {
// 導(dǎo)航圖標(biāo)
Box(modifier = Modifier.size(navigationIconSize),contentAlignment = Alignment.Center) {
navigationIcon()
}
}
// title定義
Box(
modifier = Modifier
.height(toolBarHeight) //和ToolBar同高
.fillMaxWidth()
.align(Alignment.BottomStart),
contentAlignment = Alignment.CenterStart
) {
Text(text = title,
color = Color.White,
modifier = Modifier.padding(start = 20.dp).matchParentSize(), // 使用 matchParentSize 修飾符保證不影響父 Box尺寸
fontSize = 20.sp
)
}
}
我們主要講解title部分
// title定義
Box(
modifier = Modifier
.height(toolBarHeight) //和ToolBar同高
.fillMaxWidth()
.align(Alignment.BottomStart),
contentAlignment = Alignment.CenterStart
) {
Text(text = title,
color = Color.White,
modifier = Modifier.padding(start = 20.dp).matchParentSize(), // 使用 matchParentSize 修飾符保證不影響父 Box尺寸
fontSize = 20.sp
)
}
首先為了保證title部分在完全收縮后高度和toolBar部分一致,我們?cè)O(shè)置Box布局高度為toolBarHeight
modifier = Modifier
.height(toolBarHeight) //和ToolBar同高
.fillMaxWidth()
然后定義Box在根布局里面的對(duì)齊方式為Alignment.BottomStart
modifier = Modifier
.height(toolBarHeight) //和ToolBar同高
.fillMaxWidth()
.align(Alignment.BottomStart)
之所以這樣設(shè)置,是因?yàn)槲覀兺ㄟ^(guò)觀察伸縮前和伸縮后的預(yù)覽圖可以知道如果保證此部分是底部左邊對(duì)齊,那么在根布局向上移動(dòng)的過(guò)程中我們便可以只關(guān)心此部分在水平方向的位移即可
接著設(shè)置文本部分的對(duì)齊方式,保證title是居中靠左對(duì)齊的
contentAlignment = Alignment.CenterStart
位移實(shí)現(xiàn)
首先,我們要明確ScrollableAppBar最大向上偏移量等于其定義的高度和收縮后的高度,即toolBarHeight的差值,即:
// 應(yīng)用欄最大向上偏移量
val maxOffsetHeightPx = with(LocalDensity.current) { scrollableAppBarHeight.roundToPx().toFloat() - toolBarHeight.roundToPx().toFloat() }
其次,title部分在水平方向的位移距離其實(shí)就是導(dǎo)航圖標(biāo)的寬度,即:
// Title 偏移量參考值
val titleOffsetWidthReferenceValue = with(LocalDensity.current) { navigationIconSize.roundToPx().toFloat() }
同時(shí)需要定義從外部獲取到的偏移量
val toolbarOffsetHeightPx: MutableState<Float> //向上偏移量
最外層布局位移定義
為根布局添加垂直方向上的位移
@Composable
fun ScrollableAppBar(
modifier: Modifier = Modifier,
title: String = stringResource(id = R.string.app_name), //默認(rèn)為應(yīng)用名
navigationIcon: @Composable () -> Unit, //導(dǎo)航圖標(biāo)
@DrawableRes backgroundImageId: Int, // 背景圖片
scrollableAppBarHeight: Dp, //定義的ScrollableAppBar高度
toolbarOffsetHeightPx: MutableState<Float> //向上偏移量
) {
Box(modifier = Modifier
.height(scrollableAppBarHeight)
.offset {
IntOffset(
x = 0,
y = toolbarOffsetHeightPx.value.roundToInt() //設(shè)置偏移量
)
}
.fillMaxWidth()
) {
.... // 背景圖等內(nèi)容
}
}
toolBar垂直方向位置不變的實(shí)現(xiàn)
設(shè)置和父布局相反的位移量保證toolBar處于原位置,即:
// 自定義應(yīng)用欄
Row(
modifier = modifier
.offset {
IntOffset(
x = 0,
y = -toolbarOffsetHeightPx.value.roundToInt() //保證應(yīng)用欄是始終不動(dòng)的
)
}
.height(toolBarHeight)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
... // 導(dǎo)航圖標(biāo)
}
title水平位移的實(shí)現(xiàn)
為了保證title均勻向右位移,用根布局此時(shí)向上位移量和最大位移量的商再乘以水平方向上的總位移即可:
x = -((toolbarOffsetHeightPx.value / maxOffsetHeightPx) * titleOffsetWidthReferenceValue).roundToInt()
完整實(shí)現(xiàn)
// title部分
Box(
modifier = Modifier
.height(toolBarHeight) //和ToolBar同高
.fillMaxWidth()
.align(Alignment.BottomStart)
.offset {
IntOffset(
x = -((toolbarOffsetHeightPx.value / maxOffsetHeightPx) * titleOffsetWidthReferenceValue).roundToInt(), //水平方向位移
y = 0
)
},
contentAlignment = Alignment.CenterStart
) {
... //title部分
}
項(xiàng)目地址
ScrollableAppBar 如果項(xiàng)目對(duì)你有所幫助,如果有改進(jìn)意見(jiàn)還可以提交 issue
到此這篇關(guān)于Android Compose實(shí)現(xiàn)伸縮ToolBar的思路詳解的文章就介紹到這了,更多相關(guān)Android 伸縮ToolBar內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程學(xué)習(xí)之抽象類AbsListView用法實(shí)例分析
這篇文章主要介紹了Android編程學(xué)習(xí)之抽象類AbsListView用法,較為詳細(xì)的分析了抽象類AbsListView的功能、結(jié)構(gòu)、定義及使用注意事項(xiàng)等,需要的朋友可以參考下2015-10-10
Android中監(jiān)聽(tīng)軟鍵盤輸入的使用方式
今天我們來(lái)討論一下Android中監(jiān)聽(tīng)軟鍵盤輸入的使用方式,它允許用戶輸入文本和執(zhí)行其他操作,但是,有時(shí)候我們需要在用戶輸入文本時(shí)進(jìn)行一些特殊的處理,比如實(shí)時(shí)驗(yàn)證輸入內(nèi)容、限制輸入字符的類型等,因此,了解如何監(jiān)聽(tīng)軟鍵盤輸入是非常重要的2023-10-10
Android應(yīng)用開(kāi)發(fā)工程目錄作用介紹
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)工程目錄作用介紹,本文對(duì)Android項(xiàng)目中的每個(gè)目錄的作用作了總結(jié),需要的朋友可以參考下2014-10-10
android ViewPager實(shí)現(xiàn)自動(dòng)無(wú)限輪播和下方向?qū)A點(diǎn)
本篇文章主要介紹了android ViewPager實(shí)現(xiàn)自動(dòng)輪播和下方向?qū)A點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02

