如何用动画扩展布局高度? [英] How to expand a layout height with animation?

查看:26
本文介绍了如何用动画扩展布局高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I couldn't find a good example for how to do this.

我设置了一个高度为 x 的 RelativeLayout.

I have a RelativeLayout set with x height.

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

I want to add a button which expands the height to x+y height.

有人可以给我推荐一个关于如何以编程方式进行操作的好例子吗?

can someone refer me to a good example on how to do it programmatically?

推荐答案

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

Please check below new edited answer as below. But here you need to know the exact new height.

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

这篇关于如何用动画扩展布局高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆