在LinearLayout中更改LayoutParams的动画

Chr*_*han 16 android android-animation android-layout layoutparams

在我的应用程序中有一个LinearLayout,其布局高度为0.当我单击按钮时,此布局高度应为LayoutParams.WRAP_CONTENT.这是我在onclicklistner中使用的代码.

LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
slider.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)

我想动画这个.如何将动画设置为滑块.

Kai*_*Kai 25

我想你只想动画从0高度到最终高度的视图,你可以用自定义动画做到这一点:

public class ShowAnim extends Animation {
    int targetHeight;
    View view;

    public ShowAnim(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (targetHeight * 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并在您的代码中执行此操作以启动动画:

Animation ani = new ShowAnim(headerView, 100/* target layout height */);
ani.setDuration(2000/* animation time */);
headerView.startAnimation(ani);
Run Code Online (Sandbox Code Playgroud)

  • 如果每次都从零高度开始,您如何做到这一点?如果我以 100 的目标布局高度执行一次,然后以 200 的目标布局时间再次执行此操作,我希望它从 0 -> 100 到 100 -> 200。 (2认同)
  • 这只是快速启动psudo代码,请根据自己的需要进行调整 (2认同)

Shr*_*jan -1

如果您想为您的布局实现滑块动画,请参阅此演示

更新

另请参阅http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html

希望它能帮助你。

如果没有,请告诉我。

谢谢。享受。:)