Я имею вложеннуюсисгролу внутри, у меня есть Recyclerview и другое изображение, текстовый контент. Я хочу отскочить и вверх (сбросить в исходное положение), подобный эффекту, когда прокрутка достигает сверху или внизу.
Я могу достичь его . < /p>
nestedScrollView.setOnScrollChangeListener { v, _, scrollY, _, _ ->
if (!isBouncing) {
when {
// At the top
scrollY == AnimationConstants.Integer.ZERO -> {
triggerBounce(nestedScrollView, -AnimationConstants.Float.EIGHTY)
}
// At the bottom
!v.canScrollVertically(AnimationConstants.Integer.ONE) -> {
triggerBounce(nestedScrollView, AnimationConstants.Float.EIGHTY)
}
}
}
}
private fun triggerBounce(
view: View, offset: Float,
duration: Long = AnimationConstants.Long.FOUR_HUNDRED,
bounceUpInterpolator: Interpolator = DecelerateInterpolator(AnimationConstants.Float.ONE),
bounceDownInterpolator: Interpolator = AccelerateInterpolator(AnimationConstants.Float.ONE)
) {
// If already bouncing, do not trigger again
if (isBouncing) return
isBouncing = true
// Create bounce animations
val bounceDown = AnimationUtil.createBounceAnimator(
view,
AnimationConstants.Float.ZERO,
duration,
bounceDownInterpolator
)
val bounceUp =
AnimationUtil.createBounceAnimator(view, offset, duration, bounceUpInterpolator)
// Animate both actions sequentially
AnimatorSet().apply {
playSequentially(bounceDown, bounceUp)
start()
// Reset flag after the entire bounce sequence is completed
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
isBouncing = false
}
})
}
}
fun createBounceAnimator(view: View, targetY: Float, duration: Long, interpolator: Interpolator): ObjectAnimator {
return ObjectAnimator.ofFloat(view, AnimationConstants.String.TRANSLATION_Y, targetY).apply {
this.duration = duration
this.interpolator = interpolator
}
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... ll-up-down
Android Как добиться гладкого/естественного эффекта отскока при прокрутке вниз ⇐ Android
Форум для тех, кто программирует под Android
1739799016
Anonymous
Я имею вложеннуюсисгролу внутри, у меня есть Recyclerview и другое изображение, текстовый контент. Я хочу отскочить и вверх (сбросить в исходное положение), подобный эффекту, когда прокрутка достигает сверху или внизу.
Я могу достичь его . < /p>
nestedScrollView.setOnScrollChangeListener { v, _, scrollY, _, _ ->
if (!isBouncing) {
when {
// At the top
scrollY == AnimationConstants.Integer.ZERO -> {
triggerBounce(nestedScrollView, -AnimationConstants.Float.EIGHTY)
}
// At the bottom
!v.canScrollVertically(AnimationConstants.Integer.ONE) -> {
triggerBounce(nestedScrollView, AnimationConstants.Float.EIGHTY)
}
}
}
}
private fun triggerBounce(
view: View, offset: Float,
duration: Long = AnimationConstants.Long.FOUR_HUNDRED,
bounceUpInterpolator: Interpolator = DecelerateInterpolator(AnimationConstants.Float.ONE),
bounceDownInterpolator: Interpolator = AccelerateInterpolator(AnimationConstants.Float.ONE)
) {
// If already bouncing, do not trigger again
if (isBouncing) return
isBouncing = true
// Create bounce animations
val bounceDown = AnimationUtil.createBounceAnimator(
view,
AnimationConstants.Float.ZERO,
duration,
bounceDownInterpolator
)
val bounceUp =
AnimationUtil.createBounceAnimator(view, offset, duration, bounceUpInterpolator)
// Animate both actions sequentially
AnimatorSet().apply {
playSequentially(bounceDown, bounceUp)
start()
// Reset flag after the entire bounce sequence is completed
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
isBouncing = false
}
})
}
}
fun createBounceAnimator(view: View, targetY: Float, duration: Long, interpolator: Interpolator): ObjectAnimator {
return ObjectAnimator.ofFloat(view, AnimationConstants.String.TRANSLATION_Y, targetY).apply {
this.duration = duration
this.interpolator = interpolator
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79445550/android-how-to-achieve-smooth-natural-bounce-effect-on-scroll-up-down[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия