【问题标题】:Animating view out of parent layout bounds with a CardView使用 CardView 动画视图超出父布局边界
【发布时间】:2016-04-21 12:27:26
【问题描述】:

我正在尝试将我的视图设置为父布局之外的视图。正如我需要使用的其他问题所述:

        android:clipChildren="false"
        android:clipToPadding="false"

这对于普通视图非常有效!但是我需要从 CardView 中为视图设置动画。出于某种原因,当我的视图位于 CardView 内时,它不会从中产生动画,它只是停在边界处。

我的布局文件非常简单,只有一个 LinearLayout、一些视图和一个卡片视图,其中包含动画视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:clipChildren="false"
android:clipToPadding="false">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<android.support.v7.widget.CardView
    android:layout_width="350dp"
    android:layout_height="200dp"
    android:clipChildren="false"
    android:clipToPadding="false">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false">
        <View
            android:id="@+id/animate"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="@android:color/holo_red_light"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

我用于动画的代码是:

View view = findViewById(R.id.animate);
    ObjectAnimator animation2 = ObjectAnimator.ofFloat(view, "translationY", -500);
    animation2.setDuration(5000);
    animation2.setTarget(view);
    animation2.start();

现在的问题是,当我使用 CardView 时,视图在边框处停止动画。它只是消失了。当我使用普通的 FrameLayout 时,它确实会从父级中生成动画。

我在 CardView 和动画中缺少什么奇怪的行为吗?

【问题讨论】:

    标签: android animation android-cardview clip


    【解决方案1】:

    原来在 CardView 上

    cardView.setClipToOutline(false);
    

    需要调用。这确实可以防止项目在边界处消失。

    【讨论】:

    • 最低 API 21 是必需的,我需要 API 16 的解决方案
    猜你喜欢
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多