Android change background image with fade in/out animation

前端 未结 5 1603
心在旅途
心在旅途 2020-12-29 08:47

I wrote code which can change background image random every 5 second.now i want to use fade in/out animation to change background image,but I do not know how I can use this

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 09:11

    Answer by kimkevin animates a View (be it an ImageView, TextView etc.) and not a background of a View. Which means if you want to just highlight any View, you'll have to add another View behind it (lets call it backgroundView) and animate that backgroundView.

    Use the following code for animating background of a view

    val startColor = view.solidColor
    val endColor = ContextCompat.getColor(context, R.color.your_color)
    
    val colorAnim = ObjectAnimator.ofInt(view, "backgroundColor", startColor, endColor, startColor)
    colorAnim.duration = 2000
    colorAnim.setEvaluator(ArgbEvaluator())
    colorAnim.start()
    

提交回复
热议问题