Android RecyclerView achieves animation effects similar to slot machine lottery, digital scrolling, etc.

1. RecyclerViewLoopScrollAnimation project introduction

RecyclerViewLoopScrollAnimation A helper class for circular scrolling animations for Android RecyclerView, which can achieve effects similar to slot machine lottery, digital scrolling, etc.

2. Effect display

The git is too large and the compression is severe, resulting in a low frame rate. It is recommended to download the project and run it to check the actual effect.

3. How to use:

Step 1: In the build.gradle file in your root directory, add the jitpack maven repository under the repositories tag:

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
    	...
    	maven { url 'https://jitpack.io' }
    }
}

If you use Gradle 7.0, add to the repositories tag of dependencyResolutionManagement in setting.gradle:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
		...
        maven { url 'https://jitpack.io' }
        ...
    }
}
...

Step 2: Add dependencies

Add the dependency

dependencies {
	implementation 'com.github.xiangang:RecyclerViewLoopScrollAnimation:v1.0.0-alpha01'
}

4. Example of use

An example of use is as follows:

//创建一个RecyclerViewLoopScrollAnimation实例
val recyclerViewLoopScrollAnimation = RecyclerViewLoopScrollAnimation()
//创建一个Configuration
val recyclerViewLoopScrollAnimationConfiguration = RecyclerViewLoopScrollAnimation.build {
    	scrollAnimatorDuration = 4000L
        }
//设置Configuration
recyclerViewLoopScrollAnimation.setConfiguration(recyclerViewLoopScrollAnimationConfiguration)
//将recyclerViewLoopScrollAnimation关联到RecyclerView
recyclerViewLoopScrollAnimation.attachToRecyclerView(holder.recyclerView)
//配合LooperLinearLayoutManager实现无限滚动
val looperLinearLayoutManager = LooperLinearLayoutManager(context, RecyclerView.VERTICAL, false)
recyclerView.layoutManager = looperLinearLayoutManager
//设置滚动Action,用于滚动时返回当前显示View的Position
recyclerViewLoopScrollAnimation.setRecyclerViewScrollAction(object:RecyclerViewScrollAction {
    override fun findFirstVisibleItemPosition(): Int {
        return looperLinearLayoutManager.findFirstVisibleItemPosition()
    }

    override fun findLastVisibleItemPosition(): Int {
        return looperLinearLayoutManager.findLastVisibleItemPosition()
    }
})
//开始动画
recyclerViewLoopScrollAnimation.start()

For more usage, please refer to the app in the project.

5. Other

RecyclerViewLoopScrollAnimation.Configuration configuration class:

/**
* 配置
*/
class Configuration {
    /**
    * 是否禁止Item相应手指滑动
    */
    var disableItemTouchListener = true

    /**
    * 第一阶段的滚动动画时长
    */
    var scrollAnimatorDuration = 3000L

    /**
    * 第一阶段的滚动动画的插值器
    */
    var scrollAnimatorInterpolator = AccelerateDecelerateInterpolator()

    /**
    * 是否开启弹簧动画
    */
    var enableSpringAnimator = true

    /**
    * 第一阶段的滚动动画针对item的滚动步长(scrollBy方法的y参数)
    */
    var scrollAnimatorScrollByYStep = 30

    /**
    * 第二阶段的弹簧动画的SpringForce
     */
    var springAnimatorForce: SpringForce = SpringForce(0f)
    .setDampingRatio(SpringForce.DAMPING_RATIO_HIGH_BOUNCY)
    .setStiffness(SpringForce.STIFFNESS_VERY_LOW)
    .setDampingRatio(0.2f)
    .setStiffness(350f)

    /**
    * 弹簧动画的起始位置
    */
    var springAnimatorStartValue = -50f

}

The usage of RecyclerViewLoopScrollAnimation.build is Function literals with receiver :

/**
* 配置
*/
//创建一个RecyclerViewLoopScrollAnimation实例
val recyclerViewLoopScrollAnimation = RecyclerViewLoopScrollAnimation()
//创建一个Configuration
val recyclerViewLoopScrollAnimationConfiguration = RecyclerViewLoopScrollAnimation.build {
    	scrollAnimatorDuration = 4000L
        }
//设置Configuration
recyclerViewLoopScrollAnimation.setConfiguration(recyclerViewLoopScrollAnimationConfiguration)

Further reading: Can you implement the builder pattern in Kotlin?

Other interfaces:

/**
* RecyclerView滚动过程中需要执行的Action
*/
fun setRecyclerViewScrollAction(recyclerViewScrollAction: RecyclerViewScrollAction) {
	this.recyclerViewScrollAction = recyclerViewScrollAction
}

/**
* 设置滚动动画监听器
*/
fun setOnSpringAnimationEndListener(onScrollAnimatorListener: Animator.AnimatorListener) {
	this.onScrollAnimatorListener = onScrollAnimatorListener
}

/**
* 设置弹簧动画结束监听器
* 可用于动画结束后执行爆炸粒子效果
*/
fun setOnSpringAnimationEndListener(onAnimationEndListener: DynamicAnimation.OnAnimationEndListener) {
	this.onSpringAnimationEndListener = onAnimationEndListener
}

License

Copyright 2022 xiangang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Guess you like

Origin blog.csdn.net/xiangang12202/article/details/122553658