问题描述
我有我作为一个容器一些按钮使用的LinearLayout和TextView的是,我想动画的高度给予了布局的IM pression向下滑动时,用户presses秀按钮。
I have a LinearLayout that I use as a container for some buttons and textview's that I would like to animate the height of to give an impression of the layout sliding down when the user presses a "show" button.
我已经设置的LinearLayout为layout_height =0dp和知名度=水涨船高,在我的XML。然后,我想将其设置为可见的,任何高度需要总结的内容。目前,我有问题,甚至动画它在所有,没关系的包裹内容的高度。
I have set the LinearLayout to layout_height="0dp" and visibility="gone" in my xml. I then wish to set it to be visible and whatever height is need to wrap the content. At the moment I'm having issues even animating it at all, nevermind the wrap content height.
下面是我的方法的动画:
Here's my method for animating:
private void toggle(final LinearLayout v) {
v.setVisibility(View.VISIBLE);
ValueAnimator va = ValueAnimator.ofInt(0, 300);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
v.getLayoutParams().height = value.intValue();
v.invalidate();
}
});
va.start();
}
也许,这个问题是我如何设置的LinearLayout的高度?还是我误解了ValueAnimator的功能?我环视了一下博客文章的被切特·哈泽,但它们不包含任何具体的高度动画的例子。无论是有我能找到和如何与高度动画使用的API从3.0+工作的好例子。也许需要一些帮助,在此,非常感谢!
Perhaps the problem is how I am setting the height of the LinearLayout? Or am I misunderstanding the function of the ValueAnimator? I've looked around at the blog post's by Chet Haase but they do not contain any specific height animation examples. Neither have I been able to find and good examples of how to work with animations of height using API's from 3.0+. Would love some help on this, thanks!
推荐答案
看着这个博客帖子:http://tech.chitgoks.com/2011/10/29/android-animation-to-expand-collapse-view-its-children/我发现我不应该使用 view.invalidate()
,以使布局重绘。我应该使用 view.requestLayout()
。
Looking at this blog post: http://tech.chitgoks.com/2011/10/29/android-animation-to-expand-collapse-view-its-children/ I found that I shouldn't use view.invalidate()
to have the layout redrawn. I should use view.requestLayout()
.
因此,code变成这样的:
Thus the code becomes something like this:
ValueAnimator va = ValueAnimator.ofInt(0, height);
va.setDuration(700);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
v.getLayoutParams().height = value.intValue();
v.requestLayout();
}
});
我只是想补充了一份关于如何获得的LinearLayout的高度也可以使动画的动态。这个高度需要各方面的意见,先绘制。 Therfor我们必须侦听的事件,它告诉我们的是,附图完成。这可以从onResume()这样的方法来完成(请注意,在我的XML我宣布容器WRAP_CONTENT的高度,这也是可见的,因为我想从一开始我做的efter测量它隐藏):
I just wanted to add a note on how to get the height of the LinearLayout as well to make the animation dynamic. To get the height all the views need to be drawn first. Therfor we have to listen for an event that tells us that the drawing is done. This can be done from the onResume() method like this (note that in my xml I have declared the container to wrap_content for height and it is also visible, since I want to hide it from the start I do that efter measuring it):
@Override
public void onResume() {
super.onResume();
final ViewTreeObserver vto = filterContainer.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
height = filterContainer.getHeight();
filterContainer.setVisibility(View.GONE);
filterContainer.getLayoutParams().height = 0;
filterContainer.requestLayout();
ViewTreeObserver obs = filterContainer.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
}
这篇关于的LinearLayout容器ValueAnimator的高度的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!