【问题标题】:Dynamic Animation - Android动态动画 - Android
【发布时间】:2016-05-25 15:38:48
【问题描述】:

我目前有一个按钮,当单击该按钮时,动画开始显示按钮上方的 LinearLayout。 LinearLayout 就在它的正上方。在 xml 文件中,LinearLayouts 可见性设置为 GONE。因此,当单击按钮时,可见性设置为 VISIBLE。然后动画开始。该动画是一个向下滑动的动画。一切都很完美。但是当单击按钮时,按钮会跳到 LinearLayout 结束的底部。即使 LinearLayout 仍在播放动画。如何使按钮与 LinearLayout 动画一起移动?我希望一切都能顺利过渡。但是按钮跳起来,看起来不是很流畅。

线性布局动画

<?xml version="1.0" encoding="utf-8"?>
<!-- slide down -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

【问题讨论】:

    标签: android android-layout animation


    【解决方案1】:

    我今天遇到了一个非常相似的情况。

    我使用了一个自定义动画来调整视图的大小。

    测量并保存所需的高度。最初高度设置为 0,并随着动画的进行而增长。最后它达到测量的高度,然后设置为原始包装内容。

    public static void expand(final View v) {
        v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
    
        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? 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));
        v.startAnimation(a);
    }
    

    v 应该是您想要制作动画的 LinearLayout。 如果您愿意,可以选择固定的持续时间。

    来源:https://stackoverflow.com/a/13381228/1646326

    【讨论】:

      【解决方案2】:

      使用自定义动画,很容易你可以在applytransformation中改变你想要的任何东西, interpolatedTime - 这是从开始到结束动画的当前位置(以 % 为单位)(并且具有从 0 到 1 的浮点值,所以在这里使用这个 interpolatedTime 你可以迭代任何你能想象到的东西);)

      static class HeightAnimation extends Animation{
              private View view;
              private int mViewHeightFrom;
              private int mViewHeightTo;
      
      
      
              public HeightAnimation(View view, int heightFrom, int heightTo){
                  this.view = view;
                  this.mViewHeightFrom = heightFrom;
                  this.mViewHeightTo = heightTo;
      
              }
      
              @Override
              protected void applyTransformation(float interpolatedTime, Transformation t) {
                  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)((mViewHeightTo - mViewHeightFrom)*interpolatedTime+0.5));
                  view.setLayoutParams(params);
              }
      
              @Override
              public boolean willChangeBounds() {
                  return true;
              }
      
              @Override
              public boolean isFillEnabled() {
                  return true;
              }
          }
      

      及用法:

      public static void applyAnimationHeightTransformation(Context context, View view, int viewHeightFrom, int viewHeightTo, int duration, int animationOffsetMilisec){
              HeightAnimation anim = new HeightAnimation(view, viewHeightFrom, viewHeightTo);
              anim.setStartOffset(animationOffsetMilisec);
              anim.setDuration(duration);
              //anim.setInterpolator(new OvershootInterpolator()); // here interpolators can be used
              if(view != null) {
                  view.setAnimation(anim);
                  view.startAnimation(anim);
              }
          }
      

      为了让高度更容易使用 - 通过转换为像素来使用 dp 中的值:

      public static int dpToPx(int dp) {
              return (int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, YourApplication.getMainContext().getResources().getDisplayMetrics()));
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 2017-12-26
        • 1970-01-01
        相关资源
        最近更新 更多