【问题标题】:Android animation not animating despite calling start animation on the target button尽管在目标按钮上调用了开始动画,但 Android 动画没有动画
【发布时间】:2020-12-01 02:54:45
【问题描述】:

我正在尝试理解为什么我的按钮不会根据需要进行动画处理。我有这个主按钮btn_onebtn_one_background,这是我想要在单击btn_one 时制作动画的动作。出于某种原因,当我通过btn_one_background 时,动画无法启动我做错了什么。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
         setContentView( R.layout.activity_button)

        val btn : ImageButton = findViewById(R.id.btn_one)
        btn.setOnClickListener {

            startAnimation()

        }

    }
    
    private fun startAnimation(){
            val whiteBackAnin =
                    AnimatorInflater.loadAnimator(
                            context,
                            R.animator.button_anim
                    ) as AnimatorSet
           whiteBackAnin.setTarget(btn_one_background)
           whiteBackAnin.start()

    }

我的布局活动按钮

  <ImageButton
        android:id="@+id/btn_one"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@drawable/ic_circle_red"
        android:contentDescription="@string/button_content_description"
        android:elevation="4dp"
        android:src="@drawable/ic_options"
        android:visibility="invisible"
        tools:visibility="visible" />
    <ImageView
        android:id="@+id/btn_one_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_circle_white"
        android:importantForAccessibility="no"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@id/btn_one"
        app:layout_constraintEnd_toEndOf="@id/btn_one"
        app:layout_constraintStart_toStartOf="@id/btn_one"
        app:layout_constraintTop_toTopOf="@id/btn_one"
        />

动画师

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">
    <set android:ordering="together">
        <objectAnimator
            android:duration="1"
            android:propertyName="scaleX"
            android:valueTo="0.25"
            android:valueType="floatType" />
        <objectAnimator
            android:duration="1"
            android:propertyName="scaleY"
            android:valueTo="0.25"
            android:valueType="floatType" />
    </set>


<objectAnimator
        android:duration="1"
        android:propertyName="alpha"
        android:valueTo="1.0"
        android:valueType="floatType" />
    <objectAnimator
        android:duration="1000"
        android:propertyName="alpha"
        android:valueFrom="1f"
        android:valueTo="1f"
        android:valueType="floatType" />
    </set>

【问题讨论】:

    标签: android kotlin animation


    【解决方案1】:

    我发现了几个问题:

    1. 您已将 ImageView 的可见性设置为消失 - 您应该将 alpha 设置为 0
    2. ImageView 似乎没有大小,因为它设置为wrap_content,但它只有一个背景 - 然后你的动画会缩小它 - 我认为它可能会从无到有缩小 - 所以您应该为该组件设置一些宽度和高度

    【讨论】:

    • 我将 alpha 更新为 0,并为 imageview 提供了尺寸,但仍然没有动画。
    • 当我进行这些更改时,它对我有用 - 你是否从 ImageView 中删除了可见性属性?
    • 我实际上在想,因为我有一个动态布局,它可能也需要我添加它。它确实有效,但现在我让它在后台而不是前台显示stackoverflow.com/questions/65092442/…
    猜你喜欢
    • 2014-10-07
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多