0

我创建了一个在我的项目中调用的方法,我之所以这样做是因为我有一个 Crouton (toast),它告诉用户激活那里的帐户,或者当没有有效的互联网连接时它也会提醒他们......并且我不希望它干扰我的视图顶部,因为那里的用户操作很重要。我使用的是相对布局

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

谁能为我指出两件事的正确方向,一是调整高度而不是向下滑动整个视图,二是帮助我解决我遇到的崩溃,这发生在 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);
    }
4

1 回答 1

2

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

这就是它的样子:

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

在代码中,创建一个新的 Animation 并将其应用到要设置动画的 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对其进行设置。如果您的 View 包含在 LinearLayout 中,则只能LinearLayout.LayoutParams为您的 View 设置。

于 2013-09-20T08:29:36.427 回答