3

Hi I am trying to animate the height of a view in android say every 5 seconds :-

  1. height goes from 0 to 5
  2. height goes from 5 to 10
  3. height goes from 10 to 3 etc

I am using the code below :-

    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;
    }
    }

and initialize it like this:-

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

But with this i get the below:-

  1. height goes from 0 to 5
  2. height goes from 0 to 10
  3. height goes from 0 to 3

i want the height to be animated from its previous height rather than from 0 everytime. Can someone please help me here

Edit1:- I did this

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

But it still animates from 0 to the newheight.

2
  • 1
    set animation.setFillAfter(true); It should solve the problem. Dec 7, 2012 at 5:13
  • @ShreyaShah I did that but it does not work. Please see my edit
    – Rasmus
    Dec 7, 2012 at 5:35

4 Answers 4

8

Ok so this is how I finally solved it:-

    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
0

You need an extra flag to tell your applyTransformation method whether you want to increase or decrease the view height.

Add a flag to the constructor and save that flag as an instance variable, and use that flag to decide whether to increase or decrease the view height:

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();
    }

}
1
  • I dont think that would help because even if I use increaseHeight as true, it still animates from 0 to the final height
    – Rasmus
    Dec 7, 2012 at 5:37
0

I know this post has not been active for over a year but I will post what I think is the easiest way to animate view's height anyway.

Instead of declaring Animation class, you could use View.setScaleY API available from API level 11.

Using this, you can specify view to scale within the range from 0-1 (0 means no height and 1 means original height) in desired second.

In XML set your view's height to its maximum height, then when inflating the view (e.g. in Activity#onCreate() or Fragment#onViewCreated()) you can

yourView.setScaleY(0);

Then after 5 seconds you can set

yourView.setScaleY(0.5f);

This will scale your view's height instantly. If you want to scale the view with animation, you can for example do something like this:

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

I hope it helps.

1
  • This scale the view, so text is stretched or compressed. That's not what this question is about, namely changing the height of ViewGroup so that children are laid out correctly relative to that height.
    – arberg
    Mar 8, 2022 at 14:13
-2

Try animation set. I am not sure whether this is what you want.

Animation 1 for : height goes from 0 to 5
Animation 2 for : height goes from 0 to 10
Animation 3 for : height goes from 0 to 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);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.