【问题标题】:Android RelativeLayout height animationAndroid RelativeLayout 高度动画
【发布时间】:2015-10-02 22:21:48
【问题描述】:

我正在寻找RelativeLayout 的高度动画。通常它应该从基值开始到布局的高度。我试图用EasyAndroidAnimation 库写。但未能成功。下面附上代码。它正在从基地弹起。我需要一些光滑的东西。

new BounceAnimation(this.LayoutGraph)       
    .setBounceDistance(50)
    .setNumOfBounces(5)
    .setDuration(500)
    .animate();

我也在寻找像这样简单的东西,因为我必须将这种方法用于许多布局:

layout.startfrom(basevalue)
layout.to(height of the layout)
layout.animate()

任何帮助将不胜感激。

代码添加*

根据要求,请找到我正在尝试的以下代码。

我通过调用下面的代码来调用动画。

  expand(this.mFirstGraph);

展开方法

   public void expand(final View v) {

            v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            int i = (int)(0.075F * this.mWidthScreen);
            //this.mFirstGraph.getLayoutParams().height = i;
            int length=(int)(0.425F * this.mWidthScreen * (this.maxTemp - this.mWeatherModel.mWeekHighTemperatures[0]) / this.ratioTemp);
            final int targetHeight = i+ length;

            Log.e("BASIC5", "targetHeight: "+targetHeight);

            // Older versions of android (pre API 21) cancel animations for views with a height of 0.
            v.getLayoutParams().height = 1;
            v.setVisibility(View.VISIBLE);
            Animation a = new Animation()
            {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    v.getLayoutParams().height = interpolatedTime == 1
                            ? LinearLayout.LayoutParams.WRAP_CONTENT
                            : (int)(targetHeight * interpolatedTime);
                    v.requestLayout();
                }

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

                public void animate() {
                    // TODO Auto-generated method stub

                }
            };

            // 1dp/ms
            //a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
            a.setDuration(2000);
            Log.e("BASIC5", "Duration value: "+ (int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
            a.setInterpolator(new AccelerateInterpolator(3));
            v.startAnimation(a);
        }




         public void collapse(final View v) {

            final int initialHeight = v.getMeasuredHeight();

            Animation a = new Animation()
            {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    if(interpolatedTime == 1){
                        v.setVisibility(View.GONE);
                    }else{
                        v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                        v.requestLayout();
                    }


                }



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



                public void animate() {
                    // TODO Auto-generated method stub

                }
            };


            // 1dp/ms
             //a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
            a.setDuration(200);
            //a.setInterpolator(new DecelerateInterpolator());

            v.startAnimation(a);
        }

目前发现的问题

  1. 动画不流畅。
  2. 高度不合适。我目前正在研究它。

【问题讨论】:

    标签: android android-layout android-animation android-relativelayout


    【解决方案1】:

    也许这个基本示例可以帮助你:

       public void expand(final View v) {
    
        v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
    
        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? LinearLayout.LayoutParams.WRAP_CONTENT
                        : (int)(targetHeight * interpolatedTime);
                v.requestLayout();
            }
    
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
    
        // 1dp/ms
        a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
        //a.setInterpolator(new AccelerateInterpolator(3));
        v.startAnimation(a);
    }
    
    
    
    
     public void collapse(final View v) {
    
        final int initialHeight = v.getMeasuredHeight();
    
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1){
                    v.setVisibility(View.GONE);
                }else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
    
    
            }
    
    
    
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
    
    
        // 1dp/ms
         a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
        //a.setInterpolator(new DecelerateInterpolator());
        v.startAnimation(a);
    }
    

    【讨论】:

    • 感谢您的帮助。运行时出现一些错误。将向您发布结果。
    • 您好 Augusto,感谢您的努力。错误处理后,我可以工作了。基本上我发现了2个问题。 1. 加载太慢。走得很慢 2. 布局高度超过相对布局高度。
    • 正如您在我发布的代码中看到的,动画持续时间是每毫秒 1dp。您可以根据需要设置持续时间。例如: a.setDuration(200) 并注释掉前面的。希望这是你想要的;)
    • 嗨@augusto-picciani,我已经编辑了问题并更新了代码。基本上。我需要一次使用这个动画超过 7 个图形,总共 50-60 次。所以我一直在寻找一些现成的图书馆。再次感谢您的宝贵时间
    • 你好奥古斯托,平!
    猜你喜欢
    • 1970-01-01
    • 2011-04-05
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多