【问题标题】:Have onclick method change into a size with animations使用动画将 onclick 方法更改为大小
【发布时间】:2018-09-07 20:14:29
【问题描述】:

这可能会重复 我只是在这里找不到任何可以提供帮助的东西。我希望单击它时它不会只是捕捉到那个大小,我希望它慢慢变成目标大小。如果这是重复,请告诉我!如果你能提供帮助,请尝试解释它是如何工作的,这样我可以在有意义的情况下更快地学习。

  layoutSmallerButton.setOnClickListener{
        val layoutShrink = linearLayout2.layoutParams as RelativeLayout.LayoutParams
        layoutShrink.height = 0
        linearLayout2.layoutParams = layoutShrink

        val buttonSmaller = layoutSmallerButton.layoutParams as LinearLayout.LayoutParams
        buttonSmaller.height = 0
        layoutSmallerButton.layoutParams = buttonSmaller

        val buttonLarger = layoutLargerButton.layoutParams as RelativeLayout.LayoutParams
        buttonLarger.height = 50
        layoutLargerButton.layoutParams = buttonLarger

        val internetViewLarger = internetView.layoutParams as RelativeLayout.LayoutParams
        internetViewLarger.height = 1700
        internetView.layoutParams = internetViewLarger

    }

谢谢!添加任何 cmets 以改进我的代码或任何东西!谢谢,请不要无礼!

【问题讨论】:

  • 为什么不将其比例从 0 设置为 1?
  • @EpicPandaForce 我不知道该怎么做

标签: android xml android-studio kotlin onclick


【解决方案1】:

你应该检查Property Animation

您可以这样做:

layoutSmallerButton.setOnClickListener{
    animateHeightOfView(layoutShrink, 0)
    animateHeightOfView(buttonSmaller, 0)
    animateHeightOfView(buttonLarger, 50)
    animateHeightOfView(internetViewLarger, 1700)
}

fun animateHeightOfView(view: View, targetHeight: Int, animationDuration: Int = 1000){
    ObjectAnimator.ofInt(view, "height", targetHeight).apply{
        duration = animationDuration
        start()
    }
}

编辑:如果您使用的是 ConstraintLayout(推荐),您可以使用 ConstraintSet 来制作动画。它很容易实现,您可以在 XML 文件中预览整个布局(之前和之后)。

但它需要 Android KitKat 或更新版本。

【讨论】:

  • 嘿,我现在才看到这个。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多