我有一个线性布局两种观点,我以编程方式改变自己的layout_weight财产。 有没有一种方法,我可以在动画重量这种变化,所以当重量迈向一个新的大小改变看法幻灯片?
Answer 1:
你可以简单地使用ObjectAnimator。
ObjectAnimator anim = ObjectAnimator.ofFloat(
viewToAnimate,
"weight",
startValue,
endValue);
anim.setDuration(2500);
anim.start();
一个问题是,查看类没有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();
Answer 2:
我一直在寻找这一点。 最后,我通过动画的父母,如果你有一个的LinearLayout两种观点,其工作原理很不错的weightsum财产解决它。
见: 动画使用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>
Answer 3:
注:我不知道这是最好的方式,但我想它和它的正常工作
只需用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();
更多关于ValueAnimator
Answer 4:
另一种方法是使用旧Animation
类,如在https://stackoverflow.com/a/20334557/2914140 。 在这种情况下,您可以同时更改若干意见的权重。
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;
}
}
Answer 5:
上述所有问题的答案均不是为我工作(他们会简单地“啪”,而不是动画),但在我加入weight_sum =“1”到父布局,它开始工作。 万一别人来了同样的问题。
文章来源: Anyway to programmatically animate layout weight property of linear layout