我有一个线性布局的两个视图,我以编程方式更改他们的layout_weight属性。有没有一种方法可以使重量发生变化,因此当重量发生变化时,视图会向新的尺寸滑动?无论如何编程动画线性布局的布局权重属性
10
A
回答
4
我一直在看这个。最终,我通过动画父代的权重属性来解决它,如果您在LinearLayout中有两个视图,这非常好。
看到: Animating weightSum property using ObjectAnimator
在下面的例子中,如果从1.0动画weightSum到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>
22
您可以简单地使用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();
0
另一种方法是使用旧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;
}
}
0
注:我不知道,这是最好的方式,但我想它和它的正常工作
只需用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();
相关问题
- 1. Android线性布局权重编程
- 2. 动画的线性布局
- 3. Android动画线性布局
- 4. 如何动画ViewGroups的布局属性?
- 5. 如何居中布局? - 线性布局
- 6. 移动布局位置编程线性布局
- 7. 线性布局的线性布局无法正确显示
- 8. 的Android线性布局与动画
- 9. 线性布局的重力
- 10. 线性布局
- 11. Android线性布局z-order动画
- 12. 使用动画或线性布局
- 13. 线性布局和重量
- 14. 线性布局重叠
- 15. 如何布局?使用相对布局还是线性布局?
- 16. 如何在线性布局
- 17. 更多线性布局的Android布局
- 18. 框架布局内的线性布局
- 19. 在线性布局中添加多个线性布局编程崩溃
- 20. 线性布局不滚动
- 21. 如何重叠线性布局
- 22. 动态UI - 如何将3个线性布局添加到父线性布局
- 23. Android编程线性布局ArrayList整数
- 24. 线性布局centeralization
- 25. Android线性布局
- 26. 相对布局误差线性布局
- 27. 相对布局和线性布局
- 28. 线性布局不对齐基线和相对布局不允许权重
- 29. Xaml布局属性
- 30. Android布局属性
这种方法有一个限制,你不能很好地将第一个屏幕设置为动画0重量。你需要大量的重量来做到这一点,但动画会更快。 – headsvk 2016-05-23 13:20:41
任何方式可以做到相反?使左视图动画消失并重现? – 2017-02-28 00:08:37