gpt4 book ai didi

android - 以编程方式调整布局大小(作为动画)

转载 作者:IT王子 更新时间:2023-10-28 23:51:37 24 4
gpt4 key购买 nike

我想在我的 Activity 中调整一些布局的大小。

这里是主要 XML 的代码:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#3ee3e3" >
</LinearLayout>

<LinearLayout
android:id="@+id/middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>

<LinearLayout
android:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#fe51e6" >
</LinearLayout>
</LinearLayout>

如您所见,顶部和底部布局高度为 0,而中间布局涵盖所有地方。

我想以编程方式减小中间布局大小,同时增加顶部和底部布局大小,直到所有布局具有相同的高度。

我希望它看起来像动画。

我该怎么做?

谢谢

最佳答案

我出于类似目的编写了 ResizeAnimation。这很简单,但成本很高。

Java

    /**
* an animation for resizing the view.
*/
public class ResizeAnimation extends Animation {
private View mView;
private float mToHeight;
private float mFromHeight;

private float mToWidth;
private float mFromWidth;

public ResizeAnimation(View v, float fromWidth, float fromHeight, float toWidth, float toHeight) {
mToHeight = toHeight;
mToWidth = toWidth;
mFromHeight = fromHeight;
mFromWidth = fromWidth;
mView = v;
setDuration(300);
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float height =
(mToHeight - mFromHeight) * interpolatedTime + mFromHeight;
float width = (mToWidth - mFromWidth) * interpolatedTime + mFromWidth;
LayoutParams p = mView.getLayoutParams();
p.height = (int) height;
p.width = (int) width;
mView.requestLayout();
}
}

Kotlin

class ResizeAnimation(
private val view: View,
private val toHeight: Float,
private val fromHeight: Float,
private val toWidth: Float,
private val fromWidth: Float,
duration: Long
) : Animation() {

init {
this.duration = duration
}

override fun applyTransformation(
interpolatedTime: Float,
t: Transformation?
) {
val height = (toHeight - fromHeight) * interpolatedTime + fromHeight
val width = (toWidth - fromWidth) * interpolatedTime + fromWidth
val layoutParams = view.layoutParams
layoutParams.height = height.toInt()
layoutParams.width = width.toInt()
view.requestLayout()
}
}

关于android - 以编程方式调整布局大小(作为动画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8140571/

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