【问题标题】:Add animation for Changing Height of Layout为更改布局高度添加动画
【发布时间】:2022-01-21 12:09:30
【问题描述】:

我有一个小视图,一开始只包含一个加载指示器,这就是它相对较小的原因。但是,当实际内容被加载时,我想显示它而不是加载指示器,为此我添加了 2 个文本字段和一个 ImageView。目前我只是通过隐藏 ProgressBar 并显示我刚才提到的元素来做到这一点,但是这样在两个状态之间就有了一个硬切。我想改变这一点,以便首先在短时间内调整视图的高度,然后显示内容(可能淡入,但我更关心改变高度)。

总的来说,我已经有一些想法如何做到这一点,但我不知道如何计算新高度?有没有办法计算它,甚至是直接来自 Android 的函数来解决我的问题?

已经谢谢了:)^

片段的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".viewables.fragments.home.HomeFragment">

    <ListView
        android:id="@+id/home_vertretungsplan_preview"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/vertretungsplan_list_item"/>

    <include
        android:animateLayoutChanges="true"
        android:layout_weight="0"
        android:id="@+id/home_article_preview"
        layout="@layout/thomsline_main_recyclerview_article"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
</LinearLayout>

这里是包含的布局

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:animateLayoutChanges="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:clickable="true"
    android:focusable="true"
    app:cardElevation="10dp"
    app:cardCornerRadius="20dp"
    app:cardPreventCornerOverlap="false">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:id="@+id/thomsline_post_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="0dp"
            android:adjustViewBounds="true"
            android:padding="0dp"
            android:scaleType="centerCrop"
            android:src="@drawable/img_thomsline_article_image_default"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="20dp"
            android:id="@+id/container1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/thomsline_post_image"
            app:layout_constraintLeft_toLeftOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="19sp"
                android:id="@+id/thomsline_post_title"
                tools:text="Article Title"/>


            <TextView
                android:id="@+id/thomsline_post_excerpt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textAllCaps="false"
                android:textSize="12.5sp"
                tools:text="Post Excerpt" />

        </LinearLayout>

        <ProgressBar
            android:id="@+id/thomsline_post_loading_indicator"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="gone"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

【问题讨论】:

  • 你能分享你的布局文件吗?
  • 你有胆量 :)
  • 所以你想用动画扩展cardView??
  • 这正是我想要做的,抱歉没有澄清这一点

标签: android kotlin animation


【解决方案1】:

您可以尝试在父布局中使用 animateLayoutChanges 属性。

将此行添加到活动布局的根元素:

android:animateLayoutChanges="true"

或者您可以使用 TransitionManager 进行平滑过渡。 (API 19+)

将此代码添加到您设置内容的行之前的文件中:

TransitionManager.beginDelayedTransition(cardView); //Default transition

你可以像这样将你喜欢的过渡传递给这个函数:

Transition transition = new ChangeBounds(); 
TransitionManager.beginDelayedTransition(cardView,transition);

您可以选择任何过渡:

  • 更改界限
  • 爆炸
  • 幻灯片
  • 淡出
  • 自动转换
  • 转换集

如果您想了解更多关于 android 动画的信息,我建议您阅读以下内容:Simple Animations in Android

【讨论】:

  • 遗憾的是,这对我没有任何帮助:/
  • @isi_ko 我已经编辑了答案
  • 谢谢你,成功了!
猜你喜欢
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多