gpt4 book ai didi

android - 将 View 从 0dp 宽度动画到 MATCH_PARENT

转载 作者:行者123 更新时间:2023-12-02 12:34:22 28 4
gpt4 key购买 nike

我试图在单击时为我的 RecyclerView 项目设置动画,方法是使矩形从零宽度增长到 100% (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();
}
});
}
}

为什么它不起作用?

最佳答案

我发现使用 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();

如果 View 的宽度需要与父 View 匹配,可以通过以下方式获取父 View 的宽度

int parentWidth = ((View)view.getParent()).getMeasuredWidth();

关于android - 将 View 从 0dp 宽度动画到 MATCH_PARENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42563815/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com