ValueAnimator Lint Error: This animation should be started with #start() [Recycle]
ValueAnimator Lint Error: This animation should be started with #start() [Recycle]
我在 init()
方法中创建 ValueAnimator,如下所示:
internal var animator: ValueAnimator? = null
@CallSuper
open fun init() {
animator = ValueAnimator.ofFloat(-1F, 2F)
animator?.duration = ANIMATION_DURATION
animator?.interpolator = LinearInterpolator()
animator?.repeatCount = ValueAnimator.INFINITE
animator?.repeatMode = ValueAnimator.REVERSE
animator?.addUpdateListener(this)
}
我是 starting/stopping 它取决于视图的可见性:
override fun onVisibilityChanged(changedView: View, visibility: Int) {
super.onVisibilityChanged(changedView, visibility)
when (visibility) {
VISIBLE -> animator?.start()
INVISIBLE, GONE -> animator?.cancel()
}
}
我收到以下 Lint 错误:
Error: This animation should be started with #start() [Recycle]
animator = ValueAnimator.ofFloat(-1F, 2F)
~~~~~~~
Explanation for issues of type "Recycle": Many resources, such
as TypedArrays, VelocityTrackers, etc., should be recycled (with a
recycle() call) after use. This lint check looks for missing
recycle() calls.
ValueAnimator
没有 recycle()
方法。每次我需要启动它时,我都尝试创建一个新的动画师,但我找不到一种方法来提供所需的属性,而不会出现此 Lint 错误。我该如何解决这个问题?
为避免 lint 错误,您可以在每次视图可见时创建一个值动画实例,如下所示:-
fun createAndStartAnim() {
animator = ValueAnimator.ofFloat(-1F, 2F).apply {
duration = ANIMATION_DURATION
interpolator = LinearInterpolator()
repeatCount = ValueAnimator.INFINITE
repeatMode = ValueAnimator.REVERSE
addUpdateListener(this@YourUpdateListener)
start() // this removes the lint error
}
}
我们需要取消:-
fun cancelAndNullifyAnim() {
animator?.cancel()
animator = null
}
现在可见性更改:-
when (visibility) {
VISIBLE -> createAndStartAnim()
INVISIBLE, GONE -> cancelAndNullifyAnim()
}
The lint error should be gone if we start the value anim before accessing any other recyclable properties of it.
我在 init()
方法中创建 ValueAnimator,如下所示:
internal var animator: ValueAnimator? = null
@CallSuper
open fun init() {
animator = ValueAnimator.ofFloat(-1F, 2F)
animator?.duration = ANIMATION_DURATION
animator?.interpolator = LinearInterpolator()
animator?.repeatCount = ValueAnimator.INFINITE
animator?.repeatMode = ValueAnimator.REVERSE
animator?.addUpdateListener(this)
}
我是 starting/stopping 它取决于视图的可见性:
override fun onVisibilityChanged(changedView: View, visibility: Int) {
super.onVisibilityChanged(changedView, visibility)
when (visibility) {
VISIBLE -> animator?.start()
INVISIBLE, GONE -> animator?.cancel()
}
}
我收到以下 Lint 错误:
Error: This animation should be started with #start() [Recycle] animator = ValueAnimator.ofFloat(-1F, 2F) ~~~~~~~
Explanation for issues of type "Recycle": Many resources, such as TypedArrays, VelocityTrackers, etc., should be recycled (with a recycle() call) after use. This lint check looks for missing recycle() calls.
ValueAnimator
没有 recycle()
方法。每次我需要启动它时,我都尝试创建一个新的动画师,但我找不到一种方法来提供所需的属性,而不会出现此 Lint 错误。我该如何解决这个问题?
为避免 lint 错误,您可以在每次视图可见时创建一个值动画实例,如下所示:-
fun createAndStartAnim() {
animator = ValueAnimator.ofFloat(-1F, 2F).apply {
duration = ANIMATION_DURATION
interpolator = LinearInterpolator()
repeatCount = ValueAnimator.INFINITE
repeatMode = ValueAnimator.REVERSE
addUpdateListener(this@YourUpdateListener)
start() // this removes the lint error
}
}
我们需要取消:-
fun cancelAndNullifyAnim() {
animator?.cancel()
animator = null
}
现在可见性更改:-
when (visibility) {
VISIBLE -> createAndStartAnim()
INVISIBLE, GONE -> cancelAndNullifyAnim()
}
The lint error should be gone if we start the value anim before accessing any other recyclable properties of it.