android 高度动画,android-如何使用动画扩展布局高度?

android-如何使用动画扩展布局高度?

对于如何执行此操作,我找不到很好的例子。

我有一个x高度设置的RelativeLayout。

我想添加一个将高度扩展到x + y高度的按钮。

有人可以向我推荐一个有关如何以编程方式进行操作的好例子吗?

5个解决方案

65 votes

您标记了最接近的解决方案。 这是确切的解决方案。 我有同样的问题。 希望这个答案可以帮助其他人。

实例化ResizeAnimation

ResizeAnimation resizeAnimation = new ResizeAnimation(

view,

targetHeight,

startHeight

);

resizeAnimation.setDuration(duration);

view.startAnimation(resizeAnimation);

ResizeAnimation类应该看起来像这样

public class ResizeAnimation extends Animation {

final int targetHeight;

View view;

int startHeight;

public ResizeAnimation(View view, int targetHeight, int startHeight) {

this.view = view;

this.targetHeight = targetHeight;

this.startHeight = startHeight;

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

int newHeight = (int) (startHeight + targetHeight * interpolatedTime);

//to support decent animation, change new heigt as Nico S. recommended in comments

//int newHeight = (int) (startHeight+(targetHeight - startHeight) * interpolatedTime);

view.getLayoutParams().height = newHeight;

view.requestLayout();

}

@Override

public void initialize(int width, int height, int parentWidth, int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);

}

@Override

public boolean willChangeBounds() {

return true;

}

}

the_prole answered 2020-07-26T03:05:49Z

18 votes

您需要缩放动画,这是官方文档

这是在代码中

private void animate() {

ImageView imageView = (ImageView) findViewById(R.id.ImageView01);

ScaleAnimation scale = new ScaleAnimation((float)1.0, (float)1.5, (float)1.0, (float)1.5);

scale.setFillAfter(true);

scale.setDuration(500);

imageView.startAnimation(scale);

}

Lukap answered 2020-07-26T03:06:14Z

16 votes

请检查以下新编辑的答案,如下所示。 但是在这里,您需要知道确切的新高度。

public class LayoutAnimationActivity extends Activity {

RelativeLayout ril1;

Button btn;

int initialHeight;

int actualHeight;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main2);

ril1 = (RelativeLayout) findViewById(R.id.relativeLayout1);

btn = new Button(this);

btn.setWidth(100);

btn.setHeight(200);

btn.setText("Button");

actualHeight = 210;

Ani a = new Ani();

a.setDuration(2000);

ril1.startAnimation(a);

}

class Ani extends Animation {

public Ani() {}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

int newHeight;

newHeight = (int) (initialHeight * interpolatedTime);

ril1.removeAllViews();

btn.setWidth(100);

btn.setHeight(300);

btn.setText("as");

ril1.addView(btn);

ril1.getLayoutParams().height = newHeight;

ril1.requestLayout();

}

@Override

public void initialize(int width, int height, int parentWidth, int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);

initialHeight = actualHeight;

}

@Override

public boolean willChangeBounds() {

return true;

}

};

}

Maneesh answered 2020-07-26T03:05:21Z

4 votes

没有Animation类的两种简单方法:

1)在您的xml布局文件中设置android:animateLayoutChanges="true"

2)使用ViewProperty动画师

layout.setPivot(0);

layout.animate().scaleY(scaleFactor).setDuration(500);

枢轴告诉视图从何处缩放,默认在中间,根据我的经验,这几乎从来都不是您想要的。 持续时间是可选的(默认= 1000)。

GLee answered 2020-07-26T03:06:47Z

2 votes

final Button button1 = (Button) view.findViewById(R.id.button);

final CollapseAnimator animator = new CollapseAnimator(topLayout);

final ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

int mHeight = button1.getMeasuredHeight();

KLog.i("onGlobalLayout() mHeight:" + mHeight);

animator.setValues(mHeight*2, mHeight);

}

};

button1.getViewTreeObserver().addOnGlobalLayoutListener(listener);

button1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

view.post(new Runnable() {

@Override

public void run() {

button1.getViewTreeObserver().removeOnGlobalLayoutListener(listener);

animator.collapse();

}

});

}

});

和课

public class CollapseAnimator {

private View view;

private boolean collapse=true;

private int duation=300;

private int destHeight=300;

private ValueAnimator animator;

private int originHeight=0;

private int from=0;

private int to=0;

public CollapseAnimator(View view ) {

this.view = view;

}

public void setValues(int destHeight,int originHeight){

this.destHeight = destHeight;

this.originHeight=originHeight;

from=originHeight;

to=originHeight;

}

public void collapse(){

from=to;

if(collapse){

to=destHeight;

collapse=false;

}else{

to=originHeight;

collapse=true;

}

KLog.i("from:" + from+",to:"+to);

animator = ValueAnimator.ofInt(from, to);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator valueAnimator) {

int val = (Integer) valueAnimator.getAnimatedValue();

ViewGroup.LayoutParams layoutParams = view.getLayoutParams();

layoutParams.height = val;

view.setLayoutParams(layoutParams);

}

});

animator.setDuration(duation);

animator.start();

}

}

adsl99801 answered 2020-07-26T03:07:08Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值