在安卓中动态改变视图高度

3

你好,我正在尝试在Android中每5秒动画一个视图的高度:

  1. 高度从0到5
  2. 高度从5到10
  3. 高度从10到3等等

我正在使用以下代码:

    public class ShowAnimation extends Animation{
    float finalHeight;
    View imageview;

    public ShowAnimation(View view,float deltaheight){
        this.imageview=view;
        this.finalHeight=deltaheight;
    }

    protected void applyTransformation(float interpolatedtime,Transformation t){
        imageview.getLayoutParams().height=(int)(finalHeight*interpolatedtime);
        imageview.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;
    }
    }

并且要像这样初始化:

Animation anidelta = new ShowAnimation(delta, deltaheight);
anidelta.setDuration(500/* animation time */);
delta.startAnimation(anidelta);

但是我得到以下结果:
  1. 高度从0变为5
  2. 高度从0变为10
  3. 高度从0变为3
我希望高度能够从先前的高度开始动画,而不是每次都从0开始。有人可以在这里帮助我吗?
编辑1: 我已经做到了。
Animation anidelta = new ShowAnimation(delta, deltaheight);
anidelta.setDuration(500/* animation time */);
anidelta.setFillAfter(true);
delta.startAnimation(anidelta);

但它仍会从0动画到新高度。

1
animation.setFillAfter(true); 这样就可以解决问题了。 - MysticMagicϡ
@ShreyaShah 我已经尝试过了,但是它没有起作用。请查看我的编辑。 - Rasmus
4个回答

8

好的,这是我最终解决它的方法:

    public class ResizeAnimation extends Animation 
{
   View view;
int startH;
int endH;
int diff;

public ResizeAnimation(View v, int newh)
{
    view = v;
    startH = v.getLayoutParams().height;
    endH = newh;
    diff = endH - startH;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) 
{
    view.getLayoutParams().height = startH + (int)(diff*interpolatedTime);
    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;
}}

0

我知道这篇帖子已经超过一年没有更新了,但我还是会发表我认为最简单的动画视图高度的方法。

不需要声明Animation类,你可以使用API level 11提供的View.setScaleY API。

使用它,你可以指定视图在所需秒数内从0到1的范围内缩放(0表示没有高度,1表示原始高度)。

在XML中将视图的高度设置为其最大高度,然后在填充视图时(例如在Activity#onCreate()或Fragment#onViewCreated()中),你可以

yourView.setScaleY(0);

然后在5秒钟后,您可以设置

yourView.setScaleY(0.5f);

这将立即缩放您的视图高度。如果您想使用动画缩放视图,您可以像这样做:

yourView.animate().scaleY(0.5f).setDuration(200).start();

希望它有所帮助。


这会缩放视图,因此文本会被拉伸或压缩。但这不是问题的关键,即更改ViewGroup的高度,以便子项根据该高度正确地布置。 - arberg

0

你需要一个额外的标志来告诉你的applyTransformation方法,你想要增加还是减少视图的高度。

在构造函数中添加一个标志,并将该标志保存为实例变量,然后使用该标志来决定是否增加或减少视图的高度:

public class ShowAnimation {
    // other instance variables
    private boolean increaseHeight;

    public ShowAnimation(View view, float deltaheight, boolean increaseHeight) {
        // other initializations
        this.increaseHeight = increaseHeight;
    }

    protected void applyTransformation(float interpolatedTime, Transformation t) {
        if (increaseHeight)
            imageview.getLayoutParams().height = (int) (finalHeight * interpolatedTime);
        else {
            imageview.getLayoutParams().height = (int) (finalHeight * (1 - interpolatedTime));
        }
        imageview.requestLayout();
    }

}

我认为这并没有帮助,因为即使我将increaseHeight设置为true,它仍然会从0动画到最终高度。 - Rasmus

-2

尝试使用动画集。我不确定这是否是您想要的。

动画1:高度从0到5
动画2:高度从0到10
动画3:高度从0到3

public void applyAnimation(ImageView ivDH) {
    System.out.println("Inside applyAnimation()");
    scaleAnim1 = new ScaleAnimation(0, 5, 0, 5);
    scaleAnim1.setDuration(5000);
    scaleAnim1.setRepeatCount(0);
    scaleAnim1.setInterpolator(new AccelerateDecelerateInterpolator());
    scaleAnim1.setFillAfter(true);

    scaleAnim2 = new ScaleAnimation(-5, 10, -5, 10);
    scaleAnim2.setDuration(5000);
    scaleAnim2.setRepeatCount(0);
    scaleAnim2.setInterpolator(new AccelerateDecelerateInterpolator());
    scaleAnim2.setFillAfter(true);

    scaleAnim3 = new ScaleAnimation(10, 3, 10, 3);
    scaleAnim3.setDuration(5000);
    scaleAnim3.setRepeatCount(0);
    scaleAnim3.setInterpolator(new AccelerateDecelerateInterpolator());
    scaleAnim3.setFillAfter(true);

    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(scaleAnim1);
    animationSet.addAnimation(scaleAnim2);
    animationSet.addAnimation(scaleAnim3);
    animationSet.setDuration(15000);
    animationSet.setFillAfter(true);

    ivDH.clearAnimation();
    ivDH.startAnimation(animationSet);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接