作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何以编程方式将高度布局更改为动画?
首先:
之后:
最佳答案
嘿 friend ,
测试我的代码后,我发现了一个小问题。因为我使用了 "scaleY"
它只是“拉伸(stretch)”了 View 。这意味着如果 View 中有一些文本或其他东西,它只会拉伸(stretch)它并且看起来不好看。尝试使用 ValueAnimator
,它的工作更流畅
public void onClick(View v)
{
if(!isBig){
ValueAnimator va = ValueAnimator.ofInt(100, 200);
va.setDuration(400);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
v.getLayoutParams().height = value.intValue();
v.requestLayout();
}
});
va.start();
isBig = true;
}
else{
ValueAnimator va = ValueAnimator.ofInt(200, 100);
va.setDuration(400);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
v.getLayoutParams().height = value.intValue();
v.requestLayout();
}
});
va.start();
isBig = false;
}
}
XML:
<RelativeLayout
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_red_dark"
android:onClick="onButtonClick"
android:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="My Layout"/>
</RelativeLayout>
旧答案您可以使用 ObjectAnimator,只需记住设置 pivotY(0)
使其仅在底部移动。自己玩它以满足您的需求:)
private boolean isBig = false;
...
public void onClick(View v)
{
v.setPivotY(0f);
if(!isBig){
ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 2f);
scaleY.setInterpolator(new DecelerateInterpolator());
scaleY.start();
isBig = true;
}
else{
ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1f);
scaleY.setInterpolator(new DecelerateInterpolator());
scaleY.start();
isBig = false;
}
}
关于android - 如何将高度布局更改为动画(以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959012/
我是一名优秀的程序员,十分优秀!