Animating the height of a view android

删除回忆录丶 提交于 2020-01-02 13:36:09

问题


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.


回答1:


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



回答2:


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



回答3:


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.




回答4:


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

}


来源:https://stackoverflow.com/questions/13757249/animating-the-height-of-a-view-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!