【问题标题】:Android 2.3: Animating a View?Android 2.3:动画视图?
【发布时间】:2011-07-12 06:24:53
【问题描述】:

如何在动画制作时更改视图的坐标?例如,如果我在屏幕上从左到右翻译一个视图,我希望视图在移动时将所有内容推到它的右侧?

public Animation expandHiddenPanel(final View v, final boolean expand) {
    panelExpanded = expand;
    v.measure(MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST), 
              MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    final int initialWidth = v.getMeasuredWidth();
    Log.i("test", "initialWidth = " + initialWidth);

    v.getLayoutParams().width = 0;
    v.setVisibility(View.VISIBLE);

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            int newWidth;

            if (expand) {
                newWidth = (int)(initialWidth * interpolatedTime);
                Log.i("test", "new Width = " + newWidth);
            }
            else {
                newWidth = (int)(initialWidth * (1 - interpolatedTime));
                Log.i("test", "new Width = " + newWidth);
            }

            v.getLayoutParams().width = newWidth;
            v.requestLayout();
        }

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

    a.setInterpolator(new AccelerateInterpolator());
    a.setDuration(2500);
    a.setFillAfter(true);
    v.startAnimation(a);

    return a;
}

【问题讨论】:

  • 您是否正在尝试从一项活动过渡到另一项活动?您能否向我们展示一些代码,而不是真正按照您的要求进行操作。
  • 基本上我想要做的是有一个默认情况下折叠的面板从左侧滑出(如滑动抽屉)并将相邻视图的内容推到右侧。跨度>
  • 您考虑过使用 ViewSwitcher 吗?
  • 如果我错了,请纠正我,但我认为 View Switcher 只允许您一次显示其 2 个子视图之一,而不是另一个相邻的部分视图。面板只有 200dip 宽。
  • 所以基本上你希望当前视图在“抽屉”滑出而不是覆盖它时向右移动?

标签: android animation


【解决方案1】:

我通过将主布局覆盖在面板上、将按钮放在主布局的左上角并更改我在上面发布的代码以填充主布局的左边距以显示面板来解决此问题。

为了获得滑动抽屉效果,我更改了面板的可见性并应用了一个平移动画,该动画运行相同的持续时间并使用相同的插值器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多