android - 将 onclick 方法更改为带有动画的大小

标签 android xml android-studio kotlin onclick

这可能会重复 我在这里找不到任何可以帮助的东西。我希望当它被点击时它不会只是捕捉到那个尺寸,我希望它慢慢变成目标尺寸。如果这是重复,请告诉我!如果你能帮忙,请尝试解释它是如何工作的,这样我就可以更快地学习,如果它有意义的话。

  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

    }

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

最佳答案

你应该检查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 或更新版本。

关于android - 将 onclick 方法更改为带有动画的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52229115/

相关文章:

javascript - Adobe InDesign JavaScript XML : How to add XML structure tags programmatically?

javascript - 使用 moment.js 转换 XML 解析时间和日期的时区

android - 在 Android Studio 中使用 github 库

android - Phonegap 文件下载到设备 - 采用哪个设备路径?

android - (FRIDA) 生成失败 : unable to access zygote64 while preparing for app launch; try disabling Magisk Hide in case it is active

javascript:在 xml 中搜索节点并从该节点获取数组

android - 如何使用 gradle 为 3rd 方部门管理不同的支持库版本?

java - 发布前如何知道应用程序的URL?

java - 在 Android Studio 中设置 Parse 时出错

android - 使用Firebase进行自定义登录将完整监听器添加到 Activity 中