【问题标题】:How to change a view height to follow an other animation / a layout如何更改视图高度以跟随其他动画/布局
【发布时间】:2015-02-06 17:37:57
【问题描述】:

我有这样的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:id="@+id/relativeLayout">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/category_1_full"
        android:scaleType="centerCrop"
        android:id="@+id/categoryImageView" />
(...)
</RelativeLayout>

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/relativeLayout"
    >
    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView"
        android:numColumns="2"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

然后我像这样为第一个相对布局设置动画

float _height = 120.05f;
ObjectAnimator a1 = ObjectAnimator.ofFloat(relativeLayout, "translationY", -_height);
ObjectAnimator a2 = ObjectAnimator.ofFloat(swipeRefreshLayout, "translationY", -_height);
a2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
    @Override
    public void onAnimationUpdate(ValueAnimator animation)
    {
        swipeRefreshLayout.invalidate();
        gridView.invalidate();
    }
});
AnimatorSet animation = new AnimatorSet();
animation.play(a1).with(a2);
animation.start();

如您所见,我试图使相关视图无效到 updateListener 但它不起作用。

我认为由于布局上的关系(match_parent 和 alignXX),刷新/gridViews 会自动更新 但事实并非如此。我该怎么办?

【问题讨论】:

  • 你能详细解释一下,你想做什么。无法理解您的问题。
  • 我正在为@+id/relativeLayout 设置动画到顶部大约 120 像素,所以我希望 SwipeRefreshLayout 跟随该动画(因为 XML 上的规则指定了 android:layout_below="@+id/relativeLayout")但事实并非如此。当然,我也可以在滑动布局上制作动画,但它不会保留android:layout_alignParentBottom="true" 规则...
  • 另一种查看方式是:我如何为滑动高度(不是缩放)设置动画,使其保持在相对布局下和父底部
  • 在动画中将fillAfter="true" 设置为您的@+id/relativeLayout。不要为滑动布局设置动画。
  • 如您所见,我使用的是代码而不是 xml,所以我不确定该放在哪里?此外,该文档不清楚该功能,但据我了解,它似乎不相关。以防万一,我会朝着这个方向努力:)

标签: android


【解决方案1】:

这个答案会给你很多信息。 https://stackoverflow.com/a/20274698/3841420

它与将视图推离屏幕有关,但您可以只修改宽度(或 scaleX)而不是平移您保留在屏幕上的片段。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2022-01-14
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多