【问题标题】:How can I use setStateListAnimator programmatically to my checkbox?如何以编程方式将 setStateListAnimator 用于我的复选框?
【发布时间】:2018-09-14 05:14:51
【问题描述】:

我有一个要设置动画的复选框,例如当用户点击复选框时,它会显示动画。如果我使用 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。

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    参考:How to use StateListAnimator?

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

    android:stateListAnimator : 为视图设置基于状态的动画师。 另外为实例化 StateListAnimator 对象programmatically 一个新方法:

    loadStateListAnimator(Context context, int id)
    

    已添加到AnimatorInflater

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

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

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

    【讨论】:

    • 我已经制作了动画文件,我的问题是如何在运行时设置它?
    • 我知道使用 loadStateList 动画方法 @Tac_1
    • 是的,但我必须指定使用它的版本。比如 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(context, R.animator.scale);有没有办法支持低版本? holder.cbHeart.setStateListAnimator(stateListAnimator);有没有等价的方式来支持低版本?
    • Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 因为它添加在 API 21 中
    【解决方案2】:

    在你的复选框监听器上添加这个 sn-p

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

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 2023-03-30
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多