[英]how to change a height layout as animation (programmatically)
如何以編程方式將高度布局更改為動畫?
第一:
之后:
Heey Amigo,
在測試我的代碼之后,我看到了一個小問題。 因為我使用"scaleY"
它只是“拉伸”視圖。 這意味着如果視圖中有某些文本或某些內容,它只會拉伸它並且看起來不太好看。 嘗試使用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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.