Android 2.3:动画视图?

时间:2011-07-10 18:54:00

标签: android animation

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

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;
}

1 个答案:

答案 0 :(得分:2)

我通过将主要布局覆盖在我的面板上,将按钮放在我的主布局的左上角,然后更改上面发布的代码来填充主布局的左边距以显示面板来修复此问题。

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