O Android RecyclerView alcança efeitos de animação semelhantes a loteria de caça-níqueis, rolagem digital, etc.

1. Introdução ao projeto RecyclerViewLoopScrollAnimation

RecyclerViewLoopScrollAnimation Uma classe auxiliar para animações de rolagem circular para Android RecyclerView, que pode obter efeitos semelhantes a loteria de caça-níqueis, rolagem digital etc.

2. Exibição de efeito

O git é muito grande e a compactação é severa, resultando em uma baixa taxa de quadros. Recomenda-se baixar o projeto e executá-lo para verificar o efeito real.

3. Como usar:

Passo 1: No arquivo build.gradle em seu diretório raiz, adicione o repositório jitpack maven sob a tag repositories:

Adicione-o em seu root build.gradle no final dos repositórios:

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

Se você usar o Gradle 7.0, adicione à tag de repositórios de dependencyResolutionManagement em setting.gradle:

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

Etapa 2: adicionar dependências

Adicione a dependência

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

4. Exemplo de uso

Um exemplo de uso é o seguinte:

//创建一个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()

Para mais uso, consulte o aplicativo no projeto.

5. Outro

Classe de configuração RecyclerViewLoopScrollAnimation.Configuration:

/**
* 配置
*/
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

}

O uso de RecyclerViewLoopScrollAnimation.build é Literais de função com receptor :

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

Leitura adicional: Você pode implementar o padrão do construtor em Kotlin?

Outras 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
}

Licença

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.

Acho que você gosta

Origin blog.csdn.net/xiangang12202/article/details/122553658
Recomendado
Clasificación