如何解决android中的LayoutTransition / Animation仅在无限while循环中工作
我正在像这样在android中动态创建一些按钮。
private fun createButton() {
val randomHeight = (100..1000).random()
val button = Button(this,null,R.attr.borderlessButtonStyle)
val lp = RelativeLayout.LayoutParams((1080 / 20),randomHeight)
lp.setMargins(0,0)
button.id = btnId + 1
button.setBackgroundColor(ContextCompat.getColor(this,R.color.colorAccent))
if (btnId > 0)
lp.addRule(RelativeLayout.RIGHT_OF,btnId)
root_layout.addView(button,lp)
btnId++
button.text = lp.height.toString()
buttonList.add(button)
}
现在在onCreate方法中,我将循环调用此createButton函数,直到布局的宽度达到这样的设备的宽度为止。root_layout的宽度是自动换行的内容,因此当按钮强制布局扩展时,它会产生非常酷的效果。 ..
root_layout.layoutTransition.enableTransitionType(LayoutTransition.CHANGE_APPEARING)
val displayWidth = getDisplayWidth()
GlobalScope.launch(Dispatchers.Main) {
// while (root_layout.width < displayWidth -1) finite but does not give animation
//infinite but gives animation
while (root_layout.width <= displayWidth) {
Log.d("TAG","onCreate: ${root_layout.width} + $displayWidth ")
createButton()
delay(100)
}
}
也单击按钮,我会像这样随机更改所有按钮的高度
btn_ran -> {
for (i:Button in buttonList) {
if (!isRunningSearch) {
var r = (100..1000).random()
i.layoutParams.height = r
i.text = r.toString()
i.requestLayout()
}
}
}
现在的问题是onCreate中的while循环将变为无限,但是我的btn_ran函数可以很好地工作,因为按钮的高度随动画而变化,但是只有当我的while循环以某种方式中断了而我的while循环变为无限时,这种情况才会发生btn_ran函数有效,但没有任何动画。
我想将while循环运行到仅设备宽度或固定的时间,但是我也希望按钮像动画一样在无限循环中改变高度。
这里有两个gif,一个有无限循环和动画,另一个是没有无限循环而没有动画...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。