繁体   English   中英

如何设置View的高度动画?

[英]How to animate the height of a View?

我有一个我创建的方法,在我的项目中调用,我做了因为我有一个Crouton(吐司)告诉用户激活那个帐户,或者当没有有效的互联网连接时它也会提醒他们...我不希望它干扰我的视图顶部,因为那里的用户操作非常重要。 我使用RelativeLayout

首先,这段代码无论如何都不起作用,但是当我修复它时,我意识到我在底部之间切换不同的活动现在已经消失,因为它已经滑落了,我想我可以调整大小。

任何人都可以指出我正确的方向为两个事情,一个调整高度而不是滑动整个视图,两个,帮助我修复崩溃我得到,这发生在setLayoutParam

我称之为teknoloGenie.slideViewDown("100", findViewById(R.id.RootView));

public void slideViewDown(final String distX, final View view) {

        final TranslateAnimation slideDown = new TranslateAnimation(0, 0, 0, Float.parseFloat(distX));

        slideDown.setDuration(500);
        slideDown.setFillEnabled(true);
        slideDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
            @Override public void onAnimationEnd(Animation animation) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
                params.addRule(RelativeLayout.CENTER_HORIZONTAL, -1);
                params.setMargins(0, view.getTop()+Integer.parseInt(distX), 0, 0);
                view.setLayoutParams(params);
                view.requestLayout();
            }
        });
        view.startAnimation(slideDown);
    }

如果要为视图的高度设置动画,则需要编写自己的自定义动画并修改动画视图的LayoutParams 在此示例中,动画为动画视图的高度设置动画。

这是它的样子:

public class ResizeAnimation extends Animation {

    private int startHeight;
    private int deltaHeight; // distance between start and end height
    private View view;

    /**
     * constructor, do not forget to use the setParams(int, int) method before
     * starting the animation
     * @param v
     */
    public ResizeAnimation (View v) {
        this.view = v;
    }

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

        view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
        view.requestLayout();
    }

    /**
     * set the starting and ending height for the resize animation
     * starting height is usually the views current height, the end height is the height
     * we want to reach after the animation is completed
     * @param start height in pixels
     * @param end height in pixels
     */
    public void setParams(int start, int end) {

        this.startHeight = start;
        deltaHeight = end - startHeight;
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}   

在代码中, 创建一个新动画并将其应用于要设置动画的RelativeLayout

View v = findViewById(R.id.youranimatedview);

// getting the layoutparams might differ in your application, it depends on the parent layout
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();

ResizeAnimation a = new ResizeAnimation(v);
a.setDuration(500);

// set the starting height (the current height) and the new height that the view should have after the animation
a.setParams(lp.height, newHeight);

v.startAnimation(a);

对于LayoutParams问题:

我的猜测是你得到一个ClassCastException,因为你没有使用正确的LayoutParams类。 如果您的动画视图包含在RelativeLayout中,则只能将RelativeLayout.LayoutParams设置为它。 如果您的视图包含在LinearLayout中,则只能为View设置LinearLayout.LayoutParams

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM