将视图从0dp宽度设置为MATCH_PARENT

时间:2017-03-02 19:02:59

标签: android android-animation

我试图通过制作一个矩形从零宽度增长到100%(RecyclerView)来点击MATCH_PARENT项目的动画,并成为项目的背景。

但是我看不到动画的效果。我的意思是,初始背景是白色的,但矩形是灰色的,因此点击的项目将变为灰色。但这种情况并没有发生。

这是项目xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="0dp"
    android:focusable="true"
    android:clickable="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:orientation="horizontal">

        <View
            android:id="@+id/colored_bar"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@drawable/colored_bar_bg1"></View>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <View
                android:id="@+id/option_background_container"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#e0e0e0"></View>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="16dp"
                android:paddingTop="16dp">

                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="13dp"
                    app:srcCompat="@drawable/ic_lock" />

                <TextView
                    android:id="@+id/card_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/icon"
                    android:layout_toRightOf="@+id/icon"
                    android:paddingBottom="16dp"
                    android:paddingLeft="8dp"
                    android:textColor="?android:attr/textColorPrimary"
                    android:textSize="16sp"
                    tools:text="@string/option_title_label" />

                <TextView
                    android:id="@+id/card_subtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/card_title"
                    android:layout_toEndOf="@+id/icon"
                    android:layout_toRightOf="@+id/icon"
                    android:paddingLeft="8dp"
                    android:textSize="14sp"
                    tools:text="@string/option_description_label" />
            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

制作动画的代码:

public class OptionListItemHolder extends RecyclerView.ViewHolder {

        private TextView cardTitle;
        private TextView cardSubtitle;
        private ImageView icon;
        private View coloredBar;
        private View optionBackground;

        public OptionListItemHolder(View v) {
            super(v);
            cardTitle = (TextView)v.findViewById(R.id.card_title);
            cardSubtitle = (TextView)v.findViewById(R.id.card_subtitle);
            icon = (ImageView)v.findViewById(R.id.icon);
            coloredBar = v.findViewById(R.id.colored_bar);
            optionBackground = v.findViewById(R.id.option_background_container);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ObjectAnimator animation = ObjectAnimator.ofInt(optionBackground, "width", 0, view.getWidth());
                    animation.setDuration(600);
                    animation.setInterpolator(new DecelerateInterpolator());

                    animation.start();
                }
            });
        }
    }

为什么它不起作用?

1 个答案:

答案 0 :(得分:5)

我发现使用ValueAnimator工作

ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), newWidth);
widthAnimator.setDuration(500);
widthAnimator.setInterpolator(new DecelerateInterpolator());
widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        view.getLayoutParams().width = (int) animation.getAnimatedValue();
        view.requestLayout();
    }
});
widthAnimator.start();

如果视图的宽度需要与父级匹配,则可以通过

获取父级的宽度
int parentWidth = ((View)view.getParent()).getMeasuredWidth();