java - 如何动画 View 的高度?

标签 java android android-animation android-relativelayout layoutparams

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

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

任何人都可以为我指出两件事的正确方向,一是调整高度大小而不是向下滑动整个 View ,二是帮助我修复在 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);
    }

最佳答案

如果您想对 View 的高度进行动画处理,则需要编写自己的自定义动画并修改动画 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;
    }
}   

在代码中,创建一个新动画并将其应用到您想要设置动画的相对布局:

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 类。例如,如果您的动画 View 包含在RelativeLayout中,则只能为其设置RelativeLayout.LayoutParams。如果您的 View 包含在 LinearLayout 中,则只能为您的 View 设置 LinearLayout.LayoutParams

关于java - 如何动画 View 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18912040/

相关文章:

java - 添加到 JPanel 的项目未显示

java - 我可以使用来自 Javascript 的 V8 引擎来执行带有 Flash 用户界面的 C 或 C++ 或 JNI 函数吗?

android - AlertDialog 在关闭时专注于 EditText..导致键盘出现..该怎么办?

KTX 安卓动画

java - 在java中返回动态类型的方法

java - 在 java 中添加自定义 HTTP header

android - 动态删除 View 或覆盖 View Android

android - 量化密度如何影响图像资源选择和缩放?

android - 使用反向模式时 ValueAnimator 未达到最大值

android - 以随机时间间隔淡入/淡出