2

我有一个自定义动画,我正在尝试将其应用于 RelativeLayout 以扩展其高度。当我开始动画时,它最初会缩小到 0 高度然后消失。

public class ExpandAnimation extends Animation {

int startHeight;
View mView;

public ExpandAnimation(View view) {
    this.mView = view;
    this.startHeight = view.getHeight();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    int newHeight;
    int targetHeight = (int) startHeight * 2;

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

    mView.getLayoutParams().height = newHeight;

    mView.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 a = new ExpandAnimation(rowView);
a.setInterpolator(new AccelerateInterpolator());
a.setDuration(this.getResources().getInteger(R.integer.animation_duration));
rowView.setAnimation(a);
rowView.startAnimation(a);                  
4

0 回答 0