compose垂直進度條

簡介

在使用compose制作UI時發(fā)現(xiàn),compose只提供了水平方向的進度條,但是項目中需要使用垂直方向的進度條,于是動手自己組合了一個,支持拖拽;直接上代碼

@Composable
fun VerticalSlider(
    modifier: Modifier,
    progress: Int,
    maxProgress: Int = 100,
    onValueChange: (Int) -> Unit) {

    BoxWithConstraints(modifier = modifier) {

        val thumbSize = maxWidth
        val length = maxHeight
        val maxLengthPx = with(LocalDensity.current) {
            (length - thumbSize).toPx()
        }

        var offsetX by remember(progress) {
            mutableStateOf(maxLengthPx * (progress * 1f / maxProgress))
        }

        // 創(chuàng)建并獲取一個DraggableState實例
        val draggableState = rememberDraggableState {
            // 使用回調(diào)方法回傳的參數(shù)對狀態(tài)偏移量進行累加,并限制范圍
            val newValue = (offsetX - it).coerceIn(0f, maxLengthPx)
            if (newValue == offsetX) return@rememberDraggableState
            offsetX = newValue
            onValueChange.invoke((maxProgress * (abs(offsetX) / maxLengthPx)).roundToInt())
        }

        Spacer(
            modifier = Modifier
                .then(Modifier.pointerInput(this) {
                    detectTapGestures(
                        onTap = {
                            offsetX = (maxLengthPx - it.y)
                            onValueChange.invoke((maxProgress * (abs(offsetX) / maxLengthPx)).roundToInt())
                        }
                    )
                })
                .fillMaxHeight()
                .width(thumbSize / 2)
                .align(Alignment.BottomCenter)
                .background(Color(0xFFDDE1ED), shape = RoundedCornerShape(50)))

        Spacer(
            modifier = Modifier
                .height(with(LocalDensity.current) { offsetX.toDp() } + thumbSize / 2)
                .width(thumbSize / 2)
                .align(Alignment.BottomCenter)
                .background(Color(0xff6297FF), shape = RoundedCornerShape(50)))
        Card(
            modifier = Modifier
                .size(thumbSize)
                .align(Alignment.BottomCenter)
                .offset { IntOffset(0, -offsetX.roundToInt()) }
                .draggable(
                    orientation = Orientation.Vertical,
                    state = draggableState
                ),
            colors = CardDefaults.cardColors(containerColor = Color.White),
            shape = CircleShape,
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = 1.dp)) {
            Icon(
                painter = painterResource(id = R.drawable.v_ic_volume),
                tint = Color.Unspecified,
                contentDescription = null,
                modifier = Modifier
                    .padding(2.dp)
                    .fillMaxSize())
        }
    }
}


@Preview(showBackground = true)
@Composable
fun VerticalSliderPre() {
    VerticalSlider(
        modifier = Modifier
            .width(10.dp)
            .height(150.dp),
        0) {}
}

效果


進度條
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容