我有一个线性布局中有两个视图,我以编程方式更改它们的layout_weight属性。是否有一种方法可以动画地改变它们的权重,使得当权重发生变化时,视图向新大小滑动?
我有一个线性布局中有两个视图,我以编程方式更改它们的layout_weight属性。是否有一种方法可以动画地改变它们的权重,使得当权重发生变化时,视图向新大小滑动?
您可以简单地使用ObjectAnimator。
ObjectAnimator anim = ObjectAnimator.ofFloat(
viewToAnimate,
"weight",
startValue,
endValue);
anim.setDuration(2500);
anim.start();
唯一的问题是View类没有setWeight()方法(ObjectAnimator所需)。 为了解决这个问题,我编写了一个简单的包装器,帮助实现视图权重动画。
public class ViewWeightAnimationWrapper {
private View view;
public ViewWeightAnimationWrapper(View view) {
if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
this.view = view;
} else {
throw new IllegalArgumentException("The view should have LinearLayout as parent");
}
}
public void setWeight(float weight) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.weight = weight;
view.getParent().requestLayout();
}
public float getWeight() {
return ((LinearLayout.LayoutParams) view.getLayoutParams()).weight;
}
}
使用方法如下:
ViewWeightAnimationWrapper animationWrapper = new ViewWeightAnimationWrapper(view);
ObjectAnimator anim = ObjectAnimator.ofFloat(animationWrapper,
"weight",
animationWrapper.getWeight(),
weight);
anim.setDuration(2500);
anim.start();
注意:我不确定这是最好的方法,但我尝试了并且它运行良好
仅使用ValueAnimator
即可。
ValueAnimator m1 = ValueAnimator.ofFloat(0.2f, 0.5f); //fromWeight, toWeight
m1.setDuration(400);
m1.setStartDelay(100); //Optional Delay
m1.setInterpolator(new LinearInterpolator());
m1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
((LinearLayout.LayoutParams) viewToAnimate.getLayoutParams()).weight = (float) animation.getAnimatedValue();
viewToAnimate.requestLayout();
}
});
m1.start();
我也一直在研究这个问题。最终,我通过动画化父视图的weightsum属性来解决它,如果你在LinearLayout中有两个视图,这种方法非常好用。
参见:使用ObjectAnimator动画化weightSum属性
在下面的示例中,如果你将weightSum从1.0动画到2.0,屏幕2将会很好地出现动画效果。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dual_pane"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<!-- Screen 1 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff0000"
android:layout_weight="1">
</LinearLayout>
<!-- Screen 2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff6600"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
另一种方法是使用旧的Animation
类,如https://dev59.com/5WMl5IYBdhLWcg3w5aeQ#20334557所述。在这种情况下,您可以同时更改多个视图的权重。
private static class ExpandAnimation extends Animation {
private final View[] views;
private final float startWeight;
private final float deltaWeight;
ExpandAnimation(View[] views, float startWeight, float endWeight) {
this.views = views;
this.startWeight = startWeight;
this.deltaWeight = endWeight - startWeight;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float weight = startWeight + (deltaWeight * interpolatedTime);
for (View view : views) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
lp.weight = weight;
view.setLayoutParams(lp);
}
views[0].getParent().requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
}
view.getParent().requestLayout();
。 - CoolMind