我有 fragment 滑动到屏幕高度的 0.3... 现在我想在按钮上将其从 0.3 动画到 0.8。但怎么办呢?我可以为布局权重设置动画吗?单击按钮后我需要显示更多信息,说明为什么要升高高度。 objectAnimator 不起作用。
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:gravity="bottom">
<LinearLayout
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:animateLayoutChanges="true"
android:background="@drawable/shape_details"
android:clickable="true"
android:orientation="vertical"
android:padding="15dp"
android:paddingBottom="32dp"
android:paddingTop="32dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text=""
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight=".1"
android:src="@drawable/time" />
<TextView
android:id="@+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".9"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
最佳答案
您可以通过使用 ValueAnimator
来实现此目的动画 float
从 0.3 到 0.8,然后使用 AnimatorUpdateListener
更新weight
详细信息 View 中的值 LayoutParams
.
LinearLayout details = findViewById(R.id.details);
ValueAnimator animator = ValueAnimator.ofFloat(0.3f, 0.8f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) details.getLayoutParams();
params.weight = value;
details.setLayoutParams(params);
}
});
现在您可以调用animator.start()
每当您想要将 View 从 30% 高度动画化到 80% 高度时。
关于java - 用动画将layout_weight从0.3扩大到0.8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265994/