我正试图将按钮内的可绘制图标(如不确定的圆圈进度栏)动画为“左可绘制”,为此,我从MaterialButton中创建了一个自定义类,以显示和隐藏进度图标。
class LoadingButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.materialButtonStyle
) : MaterialButton(context, attrs, defStyleAttr) {
private val progress = CircularProgressDrawable(context).apply {
setStyle(CircularProgressDrawable.DEFAULT)
val a = context.obtainStyledAttributes(
attrs, R.styleable.MaterialButton, defStyleAttr, 0
)
val tint = a.getColor(R.styleable.MaterialButton_iconTint, 0)
setColorSchemeColors(tint)
}
fun showLoading() {
this.icon = progress
progress.start()
}
fun hideLoading() {
progress.stop()
this.icon = null
}
}要测试的碎片是这样的
Fragment:
class DemoFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.demo_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
demo_action.showLoading()
}
}和布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<XXX.XXXXX.XXXX.ui.custom.LoadingButton
android:id="@+id/demo_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call action"/>
</LinearLayout>当调用showLoading()时,会出现加载图标,但不会发生旋转动画。有人知道为什么吗?提前谢谢。
发布于 2022-02-04 21:40:18
看起来你的旋转动画正被MaterialButton的涟漪动画所吸引。见以下内容:

正如您所看到的,每当波动动画运行时,旋转动画就会运行。您可以深入了解源代码,找出动画波纹的启动和停止位置,也许还可以应用一些代码,以防止波纹干扰您的旋转动画;但是,我认为最好用旋转动画视图来覆盖按钮,这样可以根据需要使其可见/不可见/消失。效果将是一样的,一个覆盖是不太可能破坏,应该更易于维护。
https://stackoverflow.com/questions/70986662
复制相似问题