为什么展开/折叠动画会滞后?

时间:2014-08-04 11:30:34

标签: android android-animation

我在SO上找到了这个动画代码。一切都很好,除了动画不流畅。在之前的申请中,它是当前的 - 没有。因此,动画或我的布局实际上存在问题。这是动画代码:

package de.silkcodeapps.lookup.utils;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class AnimationUtils {

    /**
     * Expand the view.
     * 
     * @param v
     */
    public static void expand(final View v) {
        v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        final int targtetHeight = v.getMeasuredHeight();

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT
                        : (int) (targtetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int) (targtetHeight / v.getContext().getResources()
                .getDisplayMetrics().density));
        v.startAnimation(a);
    }

    /**
     * Collapse the view.
     * 
     * @param v
     *            View to collapse.
     */
    public static void collapse(final View v) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                if (interpolatedTime == 1) {
                    v.setVisibility(View.GONE);
                } else {
                    v.getLayoutParams().height = initialHeight
                            - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int) (initialHeight / v.getContext().getResources()
                .getDisplayMetrics().density));
        v.startAnimation(a);
    }
}

这是我的DialogFragment's布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/search_title"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="@dimen/text_size_huge" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginBottom="@dimen/margin"
            android:layout_marginTop="@dimen/margin"
            android:background="@android:color/darker_gray" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/fragment_search_search_input"
                style="@style/AccScreenEditText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:imeOptions="actionSearch" />

            <de.silkcodeapps.lookup.ui.view.CheckableImageView
                android:id="@+id/fragment_search_expand_collapse_additional_actions"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="false"
                android:padding="@dimen/margin_small"
                android:src="@drawable/btn_expand_collapse" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/fragment_search_additional_actions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin"
            android:gravity="center"
            android:orientation="horizontal"
            android:visibility="gone" >

            <TextView
                android:id="@+id/fragment_search_help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/padding_very_small"
                android:text="@string/search_help"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <Space
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/fragment_search_and"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/padding_very_small"
                android:text="@string/search_and"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_or"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/padding_very_small"
                android:text="@string/search_or"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_not"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/padding_very_small"
                android:text="@string/search_not"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_single_placeholder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_single_placeholder"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_multiple_placeholders"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_multiple_placeholders"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_exact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_exact"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_group"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_fuzzy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_fuzzy"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_must_contain"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_must_contain"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />

            <TextView
                android:id="@+id/fragment_search_relevance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:paddingBottom="@dimen/padding_very_small"
                android:paddingLeft="@dimen/padding_small"
                android:paddingRight="@dimen/padding_small"
                android:paddingTop="@dimen/padding_very_small"
                android:text="@string/search_relevance"
                android:textColor="@color/search_actions_text"
                android:textSize="@dimen/text_size_huge" />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/fragment_search_progress"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="@dimen/margin" />

        <RadioGroup
            android:id="@+id/fragment_search_radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/margin"
            android:background="@drawable/bg_black_normal"
            android:orientation="horizontal"
            android:padding="0dp" >

            <RadioButton
                android:id="@+id/fragment_search_in_version"
                style="@style/ToggleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/search_work" />

            <RadioButton
                android:id="@+id/fragment_search_on_desk"
                style="@style/ToggleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/search_desk" />
        </RadioGroup>

        <TextView
            android:id="@+id/fragment_search_results_overview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/margin"
            android:textSize="@dimen/text_size_huge"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/fragment_search_display_recent_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/search_last_queries"
                android:textSize="@dimen/text_size_huge"
                android:textStyle="bold" />

            <de.silkcodeapps.lookup.ui.view.CheckableImageView
                android:id="@+id/fragment_search_expand_collapse_last_queries"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="false"
                android:padding="@dimen/margin_small"
                android:src="@drawable/btn_expand_collapse" />
        </LinearLayout>

        <ListView
            android:id="@+id/fragment_search_last_queries"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin"
            android:layout_weight="1"
            android:footerDividersEnabled="true"
            android:overScrollMode="never" />

        <ListView
            android:id="@+id/fragment_search_results"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin"
            android:layout_weight="1"
            android:footerDividersEnabled="true"
            android:overScrollMode="never" />

        <LinearLayout
            android:id="@+id/fragment_search_external_search_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/fragment_search_google"
                style="@style/OrangeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/margin_small"
                android:drawableLeft="@drawable/btn_google"
                android:text="@string/search_google" />

            <TextView
                android:id="@+id/fragment_search_wikipedia"
                style="@style/OrangeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/btn_wikipedia"
                android:text="@string/search_wikipedia" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

我试图动画fragment_search_additional_actions视图。 此外,在播放动画时,我发现我的视图内容没有被裁剪,但是在我想要裁剪的时候缩小了它的高度。

1 个答案:

答案 0 :(得分:0)

applyTransformation中你正在调用v.requestLayout();这是一项复杂的操作(它必须重新测量你的所有视图并重做布局)。

也许在它流动之前,因为你的布局更简单。