【问题标题】:Android animate layout changed weight heightAndroid 动画布局改变了权重高度
【发布时间】:2015-06-18 23:28:19
【问题描述】:

我有两个布局

[layout 1]
[layout 2]

同等重量。当我向下滚动时,我会更改 [layout 1][layout 2] 的权重。

我想知道如何为布局更改权重设置动画。 我试过了

android:animateLayoutChanges="true"

LayoutTransition transition = new LayoutTransition();
mapLayout.setLayoutTransition(transition);`

但是这些方法都不起作用。

【问题讨论】:

  • 它是你的布局的一个普通属性,所以动画它的方式与每个 ObjectAnimator 相同(有关更多信息,请参阅 ObjectAnimator 文档)

标签: java android android-layout animation layout


【解决方案1】:

您可以执行类似于此调整大小动画的操作。

public class ResizeAnimation extends Animation
{
    int _startWidth;
    int _targetWidth;
    View _view;
    private int _targetHeight;
    private int _startHeight;

    public ResizeAnimation(View view, int targetWidth, int targetHeight)
    {
        _view = view;
        _targetWidth = targetWidth;
        _startWidth = view.getWidth();

        _targetHeight = targetHeight;
        _startHeight = view.getMeasuredHeight();
        setFillAfter(true);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t)
    {
        int newWidth = (int) (_startWidth + (_targetWidth - _startWidth) * interpolatedTime);
        int newHeight = (int) (_startHeight + (_targetHeight - _startHeight) * interpolatedTime);

        _view.getLayoutParams().width = newWidth;
        _view.getLayoutParams().height = newHeight;
        _view.requestLayout();
    }

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

【讨论】:

    【解决方案2】:
    private class ExpandAnimation extends Animation {
    
            private float mStartWeight;
            private float mDeltaWeight;
            private LinearLayout mContent;
            private boolean direction;// true - up; false - down
    
            public ExpandAnimation(LinearLayout content, float startWeight,
                    float endWeight) {
                mStartWeight = startWeight;
                mDeltaWeight = endWeight - startWeight;
                mContent = content;
            }
    
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContent
                        .getLayoutParams();
                mStartWeight = lp.weight;
                lp.weight = (mStartWeight + (mDeltaWeight * interpolatedTime));
                mContent.setLayoutParams(lp);
            }
    
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-05
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      相关资源
      最近更新 更多