如何以编程方式将setStateListAnimator用于我的复选框?

时间:2018-09-14 05:14:51

标签: android android-animation

我有一个要设置动画的复选框,例如当用户点击复选框时,它将显示动画。如果我使用xml进行设置,则它将在初始显示时运行动画,但是我希望动画仅在用户选中复选框时运行。只有以编程方式设置它才有可能。

我还尝试过的:-

 <CheckBox
        android:id="@+id/_like_heart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:background="@mipmap/ic_user_heart"
        android:button="@null"
        android:checked="false"
        android:stateListAnimator="@animator/enlarge" />

有没有其他选择可以支持较低版本的android。

2 个答案:

答案 0 :(得分:0)

Ref:How to use StateListAnimator?

Android L中,为View添加了新的xml属性:

android:stateListAnimator :设置视图的基于状态的动画制作器。 另外,用于实例化StateListAnimator对象programmatically的新方法是:

loadStateListAnimator(Context context, int id)

已添加到AnimatorInflater中。

这些可以在Android L开发者预览文档包中找到。

  

Ex

StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(this, 
 R.anim.enlarg);
 yourCheckbox.setStateListAnimator(stateListAnimator);

更多调查,请访问https://developer.android.com/reference/android/animation/StateListAnimator

答案 1 :(得分:0)

在您的复选框侦听器上添加此代码段

 StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(this, 
 R.anim.my_anim);
 yourCheckbox.setStateListAnimator(stateListAnimator );
相关问题