如何设置View的高度动画?

时间:2013-09-20 08:24:07

标签: java android android-animation relativelayout layoutparams

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

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

任何人都可以指出我正确的方向为两件事,一个调整高度而不是滑动整个视图,两个,帮助我修复崩溃即时获取,发生在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);
    }

1 个答案:

答案 0 :(得分:2)

如果要为视图的高度设置动画,则需要编写自己的自定义动画并修改动画视图的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中,则只能为视图设置LinearLayout.LayoutParams