Android selector with fade in / fade out duration initially invisible

前端 未结 4 1507
无人及你
无人及你 2021-02-08 01:18

I\'m trying to achieve that an icon in ActionBar will not change states discretely, but by fading animation. When I add android:enterFadeDuration and android:

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 01:57

    My problem was similar as well, the issue was that after setting a background drawable on my view, it was in the wrong state (sometimes it even mixed the stroke and solid of two states...). This was only before the first interaction, like receiving focus or changing enabled state.

    I've found that if you call jumpToCurrentState() on the drawable (which has the fade duration properties) after setting it on the view, it will be set on the correct state, and you can keep on using the enter / exit fade duration properties. Here's how I did it:

    val stateList = (darkBackground as? RippleDrawable)?.findDrawableByLayerId(android.R.id.background) as? StateListDrawable
    background = darkBackground
    stateList?.jumpToCurrentState()
    

    In this example I had a ripple drawable which contained the selector which had the fade properties (I had to add an ID to the selector so I could look it up using the ID).

提交回复
热议问题