【问题标题】:Horizontal android:animateLayoutChanges="true" Animation not smooth水平 android:animateLayoutChanges="true" 动画不流畅
【发布时间】:2017-06-20 12:39:15
【问题描述】:

我的布局如下所示

txt_billion 动态显示,android:animateLayoutChanges="true"(下面的布局代码)。

注意Hundred 正在跳跃(实际上所有人都在跳跃,但Hundred 更明显)。如何防止文字跳动?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#9f9"
        android:text="Hundreds" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#f9f"
        android:text="Thousands" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#0ff"
        android:text="Millions" />
    <TextView
        android:id="@+id/txt_billion"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:visibility="gone"
        android:background="#ff0"
        android:text="Billions" />
</LinearLayout>

您可以从https://github.com/elye/issue_horizontal_layout_animate 获取代码进行测试

【问题讨论】:

  • 在哪个设备上?哪个安卓版本?是否启用了硬件加速?
  • 任何设备。我试过 Nexus 5 的 Android 模拟器,Nougat
  • 模拟器不是设备。先在设备上试试。它应该有很大的不同。
  • 它也发生在实际设备上。所以我创建了一个小样本作品来证明这个问题。尝试使用文本重心创建水平布局,并且 animateLayouChange 为 true。您应该会看到这种情况发生。
  • 抱歉,无法重现,在我的 OnePlus One 上运行完全流畅

标签: android android-layout android-animation android-layout-weight android-layoutparams


【解决方案1】:

尝试改用支持过渡animateLayoutChanges

首先,从您的 XML 文件中删除 android:animateLayoutChanges="true"

之后,将compile 'com.android.support:transition:25.4.0' 添加到您的应用依赖项中。

然后,在更改可见性之前添加这一行(来自 android.support.transition 包的 TransitionManager)

TransitionManager.beginDelayedTransition(parentOfAnimatedView);

为您的代码

public void clickMe(View view) {
        TransitionManager.beginDelayedTransition((ViewGroup) billionText.getParent());
        if (billionText.getVisibility() == View.GONE) {
            billionText.setVisibility(View.VISIBLE);
        } else {
            billionText.setVisibility(View.GONE);
        }
    }

【讨论】:

  • 不,它不能解决问题。 “百人”仍在跳跃。试试横向模式,跳跃更明显。
【解决方案2】:

问题是animateLayoutChanges 只影响ViewGroup 的子类。 TextView 无法对布局更改动画做出反应,因此文本会跳转。有两种方法可以解决它:

1) 将每个TextView 包裹在FrameLayout 中,并将重量放在FrameLayout 上。您还必须在每个android:animateLayoutChanges="true" 上添加getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING),并在每个FrameLayout 上调用getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING)。这是一种粗略的布局方式,但它可以让您继续使用过渡动画。

2) 使用ValueAnimator 并为(dis)出现的项目的权重设置动画。动画可能有点不稳定,因为它需要在每一帧上布置LinearLayout,但它应该仍然可以通过。您还必须解决消失项目上的文本重排问题,可能是先对其淡出进行动画处理,然后再对权重变化进行动画处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 2010-11-07
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2018-07-28
    • 2015-11-28
    相关资源
    最近更新 更多