如何用动画扩展布局高度?

pio*_*ojo 44 layout animation android

我找不到一个如何做到这一点的好例子.

我有一个设置x高度的RelativeLayout.

我想添加一个按钮,将高度扩展到x + y高度.

有人可以通过编程方式向我介绍一个很好的例子吗?

the*_*ole 65

您标记了最接近的解决方案.这是确切的解决方案.我有同样的问题.希望这个答案可以帮助别人.

实例ResizeAnimation

ResizeAnimation resizeAnimation = new ResizeAnimation(
     view, 
     targetHeight, 
     startHeight
); 
resizeAnimation.setDuration(duration); 
view.startAnimation(resizeAnimation);
Run Code Online (Sandbox Code Playgroud)

ResizeAnimation 类应该看起来像这样

public class ResizeAnimation extends Animation {
    final int targetHeight;
    View view;
    int startHeight;

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

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newHeight = (int) (startHeight + targetHeight * interpolatedTime);
        //to support decent animation, change new heigt as Nico S. recommended in comments
        //int newHeight = (int) (startHeight+(targetHeight - startHeight) * interpolatedTime);
        view.getLayoutParams().height = newHeight;
        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)

  • 谢谢!我刚刚修改了newHeight计算,以支持下降高度动画!int newHeight =(int)(startHeight +(targetHeight - startHeight)*interpolatedTime); (21认同)
  • 它有效,但有滞后.可能是什么原因? (3认同)

Luk*_*kap 17

你需要一个比例动画这里是官方文档

这是代码

private void animate() {
    ImageView imageView = (ImageView) findViewById(R.id.ImageView01);
    ScaleAnimation scale = new ScaleAnimation((float)1.0, (float)1.5, (float)1.0, (float)1.5);
    scale.setFillAfter(true);
    scale.setDuration(500);
    imageView.startAnimation(scale); 
}
Run Code Online (Sandbox Code Playgroud)

  • 如果要增加尺寸,则需要缩放.如果你只想移动你需要翻译的东西. (3认同)
  • 好吧,如果您有100dip或100px,并且将比例因子设置为1,它将使视图变为100dip / 100px (2认同)

Man*_*esh 16

请在下面查看新编辑的答案.但在这里你需要知道确切的新高度.

public class LayoutAnimationActivity extends Activity {
    RelativeLayout ril1;
    Button btn;
    int initialHeight;
    int actualHeight;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        ril1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
        btn = new Button(this);
        btn.setWidth(100);
        btn.setHeight(200);
        btn.setText("Button");
        actualHeight = 210;
        Ani a = new Ani();
        a.setDuration(2000);
        ril1.startAnimation(a);
    }

    class Ani extends Animation {
        public Ani() {}

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            int newHeight;

            newHeight = (int) (initialHeight * interpolatedTime);

            ril1.removeAllViews();
            btn.setWidth(100);
            btn.setHeight(300);
            btn.setText("as");
            ril1.addView(btn);             
            ril1.getLayoutParams().height = newHeight;
            ril1.requestLayout();
        }

        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            initialHeight = actualHeight;
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

  • public class layout_animation extends Activity - 你是代码风格的连环杀手 (7认同)

GLe*_*Lee 5

无需Animation类即可完成此操作的两种简单方法:

1)android:animateLayoutChanges="true"在您的xml布局文件中设置

2)使用ViewProperty动画师

layout.setPivot(0);
layout.animate().scaleY(scaleFactor).setDuration(500);
Run Code Online (Sandbox Code Playgroud)

枢轴告诉视图从何处缩放,默认在中间,根据我的经验,这几乎从来都不是您想要的。持续时间是可选的(默认= 1000)。