feat(ui): 添加自定义 BottomSheet 并优化界面
- 新增 BottomSheet 类,实现自定义底部拖动效果和缩放动画- 在 EditActivity 中更新工具栏副标题,显示当前加载路径- 修改 mod_dialog.xml 布局,使用自定义 BottomSheet组件
This commit is contained in:
parent
1b69f434e8
commit
1e1b046d47
|
@ -624,6 +624,8 @@ class EditActivity : BaseActivity<ActivityEditBinding>() {
|
|||
//文件列表加载的路径改变
|
||||
editStartViewModel.loadPathLiveData.observe(this) {
|
||||
editStartViewModel.loadList(it)
|
||||
//获取文件所在的目录
|
||||
viewBinding.toolbar.subtitle = it
|
||||
}
|
||||
|
||||
//文件列表的数据改变
|
||||
|
|
100
app/src/main/java/com/coldmint/rust/pro/ui/BottomSheet.kt
Normal file
100
app/src/main/java/com/coldmint/rust/pro/ui/BottomSheet.kt
Normal file
|
@ -0,0 +1,100 @@
|
|||
package com.coldmint.rust.pro.ui
|
||||
|
||||
import android.animation.Animator
|
||||
import android.animation.AnimatorSet
|
||||
import android.animation.ObjectAnimator
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.animation.AccelerateDecelerateInterpolator
|
||||
import androidx.core.animation.addListener
|
||||
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
|
||||
import com.google.android.material.bottomsheet.BottomSheetDragHandleView
|
||||
import android.view.MotionEvent as MotionEvent1
|
||||
|
||||
class BottomSheet(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : BottomSheetDragHandleView(context) {
|
||||
|
||||
constructor(context: Context) : this(context, null)
|
||||
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
|
||||
|
||||
private var initialX = 0f
|
||||
private var lastTouchX = 0f
|
||||
private var deltaX = 0f
|
||||
private val maxXOffset = 50f // 最大偏移量
|
||||
var isScaling = false
|
||||
|
||||
|
||||
// 覆盖 onTouchEvent 方法以处理触摸事件
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onTouchEvent(event: MotionEvent1?): Boolean {
|
||||
event?.let {
|
||||
when (it.action) {
|
||||
MotionEvent1.ACTION_DOWN -> {
|
||||
// 触摸开始时的逻辑
|
||||
// 记录初始位置
|
||||
initialX = translationX
|
||||
lastTouchX = event.rawX
|
||||
// 触摸开始时放大
|
||||
|
||||
if (!isScaling) {
|
||||
animate().scaleX(1.2f).scaleY(1.2f)
|
||||
.setDuration(200)
|
||||
.setInterpolator(FastOutSlowInInterpolator())
|
||||
.withLayer()
|
||||
.start()
|
||||
isScaling = true
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
MotionEvent1.ACTION_MOVE -> {
|
||||
val dx = event.rawX - lastTouchX
|
||||
val fl = initialX + dx
|
||||
if (fl < maxXOffset && fl > -maxXOffset) {
|
||||
translationX = fl
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
MotionEvent1.ACTION_UP, MotionEvent1.ACTION_CANCEL -> {
|
||||
// 触摸结束时恢复大小和位置
|
||||
val animatorSet = AnimatorSet()
|
||||
|
||||
// 恢复大小
|
||||
val scaleAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1f)
|
||||
scaleAnimator.duration = 200
|
||||
scaleAnimator.interpolator = AccelerateDecelerateInterpolator()
|
||||
|
||||
val scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 1f)
|
||||
scaleYAnimator.duration = 200
|
||||
scaleYAnimator.interpolator = AccelerateDecelerateInterpolator()
|
||||
|
||||
// 恢复到原始位置
|
||||
val translationAnimator = ObjectAnimator.ofFloat(this, "translationX", 0f)
|
||||
translationAnimator.duration = 200
|
||||
translationAnimator.interpolator = AccelerateDecelerateInterpolator()
|
||||
|
||||
// 将所有动画添加到 AnimatorSet 中并启动
|
||||
animatorSet.playTogether(scaleAnimator, scaleYAnimator, translationAnimator)
|
||||
animatorSet.start()
|
||||
animatorSet.addListener(object : Animator.AnimatorListener {
|
||||
override fun onAnimationStart(animation: Animator) {}
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
isScaling = false
|
||||
translationX = 0f
|
||||
}
|
||||
override fun onAnimationCancel(animation: Animator) {}
|
||||
override fun onAnimationRepeat(animation: Animator) {}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
else -> {
|
||||
// 其他事件处理
|
||||
}
|
||||
}
|
||||
}
|
||||
// 返回 true 表示事件已经被消费,返回 false 表示事件没有被消费
|
||||
return true
|
||||
}
|
||||
}
|
|
@ -5,9 +5,10 @@
|
|||
android:orientation="vertical"
|
||||
android:padding="8dp">
|
||||
|
||||
<com.google.android.material.bottomsheet.BottomSheetDragHandleView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
<com.coldmint.rust.pro.ui.BottomSheet
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
|
|
Loading…
Reference in New Issue
Block a user