崩溃和扩展动画问题android

时间:2016-10-26 17:37:33

标签: android animation collapse expand

当我第一次点击这个动画时,它会突然崩溃并自我扩展,然后点击它然后就可以了。问题是为什么这个动画第一次扩展自己。这里有专家吗?

这是我的xml代码

<TextView
        android:drawableRight="@drawable/ic_arrow_down"
        android:background="#FFF12222"
        android:textColor="#060606"
        android:textSize="20sp"
        android:text="Required Field"
        android:id="@+id/section_required_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/layout_required_fields"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginTop="10dp"
            android:id="@+id/tile_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textSize="16sp"
            android:textColor="#060606"/>

        <EditText
            android:id="@+id/title"
            android:layout_width="270sp"
            android:layout_height="wrap_content"
            android:text="abcd"
            android:layout_marginTop="2dp"
            android:background="@drawable/rounded_edittext"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/description_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:text="Description"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <EditText
            android:id="@+id/video_description"
            android:layout_width="270sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2sp"
            android:text="abcd"
            android:background="@drawable/rounded_edittext"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/category_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:text="Category"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <TextView
            android:id="@+id/video_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2sp"
            android:text="abcd"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <TextView
            android:id="@+id/tags_head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:text="Tags"
            android:textSize="15sp"
            android:textColor="#060606"/>

        <EditText
            android:id="@+id/tags"
            android:layout_width="270sp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2sp"
            android:text="abcd"
            android:textSize="15sp"
            android:background="@drawable/rounded_edittext"
            android:textColor="#060606"/>

    </LinearLayout>

这是我的动画java类

public class AnimationUtils {

    public static void expand(final View v) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int)(targetHeight * interpolatedTime);
                v.requestLayout();
            }

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

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

    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);
    }
}

这是我的java类代码

tvRequiredField = (TextView) findViewById(R.id.section_required_field);
        requiredFieldsLayout = (LinearLayout) findViewById(R.id.layout_required_fields);
        tvRequiredField.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    AnimationUtils.collapse(requiredFieldsLayout);
                    v.setSelected(false);
                }
                else {
                    AnimationUtils.expand(requiredFieldsLayout);
                    v.setSelected(true);
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

我弄错了

tvRequiredField = (TextView) findViewById(R.id.section_required_field);
        requiredFieldsLayout = (LinearLayout) findViewById(R.id.layout_required_fields);
        tvRequiredField.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.isSelected()) {//this should be expand instead of collapse
                    AnimationUtils.collapse(requiredFieldsLayout);
                    v.setSelected(false);
                }
                else {//this should be collapse instead of expand 
                    AnimationUtils.expand(requiredFieldsLayout);
                    v.setSelected(true);
                }
            }
        });