您好,登錄后才能下訂單哦!
這篇文章主要介紹如何通過Jetpack Compose實現雙擊點贊動畫效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Icon( Icons.Filled.Favorite, "愛心", Modifier .align(Alignment.Center) tint = Color.Red )
雙擊監聽
.pointerInput(Unit) { detectTapGestures( onDoubleTap = { ... } ) } #### **API 介紹** | API名稱 | 作用 | | --- | --- | | detectTapGestures | 監聽點擊手勢 | > 與 Clickable Modifier 不同的是,detectTapGestures 可以監聽更多的點擊事件。作為手機監聽的基礎 API,必然不會存在 Clickable Modifier 所拓展的漣漪效果 detectTapGestures 提供了四個可選參數 - onDoubleTap (可選):雙擊時回調 - onLongPress (可選):長按時回調 - onPress (可選):按下時回調 - onTap (可選):輕觸時回調 我們用到的就是`onDoubleTap`
用枚舉定義三個變量 開始、顯示和消失
enum class LikedStates { Initial, Liked, Disappeared }
remember保持數據狀態,mutableStateOf監聽狀態變化
var transitionState by remember { mutableStateOf(MutableTransitionState(LikedStates.Disappeared)) }
MutableTransitionState包含兩個字段:currentState和targetState。currentState初始化為提供的initialState,并且只能通過轉換進行變異。targetState也初始化為initialState。可以對其進行變異,以改變使用updateTransition使用MutableTransitionState創建的過渡動畫的過程。currentState和targetState都由狀態對象支持。
判斷攔截數據變化過程,用于實現對應的動畫
if (transitionState.currentState == LikedStates.Initial) { transitionState.targetState = LikedStates.Liked } else if (transitionState.currentState == LikedStates.Liked) { transitionState.targetState = LikedStates.Disappeared }
開始顯示的過度動畫
val transition = updateTransition(transitionState = transitionState, label = null) val alpha by transition.animateFloat( transitionSpec = { ... } ) { if (it == LikedStates.Liked) 1f else 0f }
我們要實現有彈性的放大動畫,所以利用graphicsLayer實現縮放
graphicsLayer可以應用于
縮放(scaleX
、scaleY
)
旋轉(rotationX、rotationY、rotationZ)
不透明度(alpha
)
陰影(shadowElevation、shape)
剪裁(clip、shape)
定義scale,XY
1 :1放大所以定義一個就行
val scale by transition.animateFloat( transitionSpec = { .... } ) { ... }
創建浮動動畫作為給定變換的一部分targetValueByState
用作從目標狀態到此動畫的目標值的映射
最后在定義三種狀態里嗎設置對應的參數
when (it) { LikedStates.Initial -> 0f LikedStates.Liked -> 4f LikedStates.Disappeared -> 2f }
@Suppress("TransitionPropertiesLabel") @Composable fun DoubleTapToLike() { var transitionState by remember { mutableStateOf(MutableTransitionState(LikedStates.Disappeared)) } Box( Modifier .fillMaxSize() .pointerInput(Unit) { detectTapGestures( onDoubleTap = { transitionState = MutableTransitionState(LikedStates.Initial) } ) } ) { if (transitionState.currentState == LikedStates.Initial) { transitionState.targetState = LikedStates.Liked } else if (transitionState.currentState == LikedStates.Liked) { transitionState.targetState = LikedStates.Disappeared } val transition = updateTransition(transitionState = transitionState, label = null) val alpha by transition.animateFloat( transitionSpec = { when { LikedStates.Initial isTransitioningTo LikedStates.Liked -> keyframes { durationMillis = 500 0f at 0 0.5f at 100 1f at 225 } LikedStates.Liked isTransitioningTo LikedStates.Disappeared -> tween(durationMillis = 200) else -> snap() } } ) { if (it == LikedStates.Liked) 1f else 0f } val scale by transition.animateFloat( transitionSpec = { when { LikedStates.Initial isTransitioningTo LikedStates.Liked -> spring(dampingRatio = Spring.DampingRatioHighBouncy) LikedStates.Liked isTransitioningTo LikedStates.Disappeared -> tween(200) else -> snap() } } ) { when (it) { LikedStates.Initial -> 0f LikedStates.Liked -> 4f LikedStates.Disappeared -> 2f } } Icon( Icons.Filled.Favorite, "點贊", Modifier .align(Alignment.Center) .graphicsLayer( alpha = alpha, scaleX = scale, scaleY = scale ), tint = Color.Red ) } } enum class LikedStates { Initial, Liked, Disappeared }
以上是“如何通過Jetpack Compose實現雙擊點贊動畫效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。