【问题标题】:How to animate the height of a View?如何为视图的高度设置动画?
【发布时间】:2013-09-25 13:30:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: java android android-animation android-relativelayout layoutparams


    【解决方案1】:

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

    【讨论】:

    • 如果我更正了您的方法,会将其设置为设定的高度,而不是仅“砍掉”100dp 的高度或对我来说可能的任何情况。因此,要解决该部分,我是否只需执行 lp.height - 100 然后将其分配给 newHeight?在接受之前我需要一点点来实现并测试它,但到目前为止是可靠的答案。
    • 在 setParams(start, end) 方法中,开始变量应该始终是当前高度,结束变量可以是任何你想要的。这是动画结束后视图的最终高度。
    • 使用您的代码获取此信息 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.penguintech.atmeapp/com.penguintech.atmeapp.DashboardActivity}:java.lang.ClassCastException:android.widget。 FrameLayout$LayoutParams
    • 我警告过你,阅读完整答案并将 RelativeLayout.LayoutParams 更改为 FrameLayout.LayoutParams。
    • 这取决于容器布局。 LayoutParams 类需要是 Animated 视图所在的 Layout 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    相关资源
    最近更新 更多