Java Android 属性动画:如何增加视图高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32835397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android property animation: how to increase view height?
提问by g.revolution
How to increase the view height using Property Animations in Android?
如何在 Android 中使用属性动画增加视图高度?
ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, "translationY", -100);
a.setInterpolator(new AccelerateDecelerateInterpolator());
a.setDuration(1000);
a.start();
The translationY actually moves the view not increase the height. How can I increase the height of the view?
translationY 实际上移动视图而不是增加高度。如何增加视图的高度?
采纳答案by rajan ks
ValueAnimator anim = ValueAnimator.ofInt(viewToIncreaseHeight.getMeasuredHeight(), -100);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = viewToIncreaseHeight.getLayoutParams();
layoutParams.height = val;
viewToIncreaseHeight.setLayoutParams(layoutParams);
}
});
anim.setDuration(DURATION);
anim.start();
回答by A Honey Bustard
You can use ViewPropertyAnimator, which can save you some lines of code :
您可以使用ViewPropertyAnimator,它可以为您节省一些代码行:
yourView.animate().scaleY(-100f).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(1000);
that should be all you need, be sure to check the documentation and all available methods for ViewPropertyAnimator.
这应该就是你所需要的,一定要检查文档和ViewPropertyAnimator 的所有可用方法。
回答by Amit Garg
It's pretty straight forward as you can see below.
正如您在下面看到的那样,它非常简单。
<LinearLayout android:id="@+id/container"
android:animateLayoutChanges="true"
.../>
More info below:
更多信息如下:
回答by W0rmH0le
This is not a direct answer to this question. However, it may help someone.
这不是对这个问题的直接回答。但是,它可能会帮助某人。
Sometimes, we want to increase/decrease view's height because some of its child is is being added/removed (or just becoming visible/gone).
有时,我们想要增加/减少视图的高度,因为它的一些子项正在被添加/删除(或只是变得可见/消失)。
On those cases, you really can use Android default animation. As mentioned in other answers, you can set via:
在这些情况下,您确实可以使用 Android 默认动画。正如其他答案中所述,您可以通过以下方式进行设置:
<LinearLayout
...
android:animateLayoutChanges="true"
.../>
Or in Java:
或者在 Java 中:
linearLayout.setLayoutTransition(new LayoutTransition());
If you created a custom view:
如果您创建了自定义视图:
public StickerPickerView(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
setLayoutTransition(new LayoutTransition());
}
For me, it was pretty satisfactory but I just tested on latest APIs. That will automatically add a fade in/out when your view becomes visible. It will also animate the height/width of your view when same changes (like when you change the visibility of one of the child views etc).
对我来说,这是相当令人满意的,但我只是在最新的 API 上进行了测试。当您的视图可见时,这将自动添加淡入/淡出。当相同的更改(例如更改子视图之一的可见性等)时,它还将为视图的高度/宽度设置动画。
So, I recommend to at least give a try.
所以,我建议至少尝试一下。
回答by Minion
Use this method in kotlin:
在 kotlin 中使用这个方法:
private fun increaseViewSize(view: View) {
val valueAnimator = ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight+20)
valueAnimator.duration = 500L
valueAnimator.addUpdateListener {
val animatedValue = valueAnimator.animatedValue as Int
val layoutParams = view.layoutParams
layoutParams.height = animatedValue
view.layoutParams = layoutParams
}
valueAnimator.start()
}
回答by Rahul Gaur
For kotlin
you can use this method
因为kotlin
你可以使用这个方法
private fun increaseViewSize(view: View, increaseValue: Int) {
val valueAnimator =
ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight + increaseValue)
valueAnimator.duration = 500L
valueAnimator.addUpdateListener {
val animatedValue = valueAnimator.animatedValue as Int
val layoutParams = view.layoutParams
layoutParams.height = animatedValue
view.layoutParams = layoutParams
}
valueAnimator.start()
}
It will increase view's size as defined in parameter
它将增加参数中定义的视图大小
based on @Minion answer
基于@Minion 的回答