WindowManager上添加的动画视图

时间:2011-12-29 07:08:33

标签: android android-animation window-managers

我有一个视图(customView)添加到WindowManager。

WindowManager mWm = (WindowManager)activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, 0, PixelFormat.TRANSPARENT);
mWl.dimAmount = 0.0f;
mWm.addView(customView, mWl);

在自定义视图中,我会在按下关闭按钮时调用翻译动画。

////这是动画////

的处理程序
final Handler translateHandler = new Handler();

final Runnable mtranslateUp = new Runnable() {
    public void run() {
        Log.v("TEST","mtranslateUp Runnable");
        startAnimation(translateUp);
    }
};

////这是关闭按钮////

的监听器
View.OnClickListener closeButtonListener = new View.OnClickListener() {         

    public void onClick(View v) {
        translateHandler.post(mtranslateUp);
    }
};

////这是翻译动画////

translateUp = new TranslateAnimation(0,0,0,-200);
translateUp.setFillAfter(true);
translateUp.setDuration(1000);
translateUp.setAnimationListener(new AnimationListener(){
        @Override
        public void onAnimationEnd(Animation animation) {
            Log.v("TEST","translateUp onAnimationEnd");
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationStart(Animation animation) {
            Log.v("TEST","translateUp onAnimationStart");
        }}
    );

如果将customView添加到活动中,这些代码可以正常工作!

当将customView添加到WindowManager时,onAnimationStart中的Log没有显示,但可以显示Runnable中的Log。

有人可以告诉如何在添加到WindowManager的视图上做动画吗?

4 个答案:

答案 0 :(得分:10)

您应该为视图LayoutParameters设置动画。例如,我使用一种方法来更新视图布局:

    public void updateViewLayout(View view, Integer x, Integer y, Integer w, Integer h){
    if (view!=null) {
        WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();

        if(x != null)lp.x=x;
        if(y != null)lp.y=y;
        if(w != null && w>0)lp.width=w;
        if(h != null && h>0)lp.height=h;

        mWindowService.updateViewLayout(view, lp);
    }
}

显然 mWindowService 是context.getSystemService(Context.WINDOW_SERVICE)。 我在动画中触发了这个方法:

    public static void overlayAnimation(final View view2animate, int viewX, int endX) {
    ValueAnimator translateLeft = ValueAnimator.ofInt(viewX, endX);
    translateLeft.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            updateViewLayout(view2animate, val, null, null, null);

        }
    });
    translateLeft.setDuration(ANIMATION_DURATION);
    translateLeft.start();
}

答案 1 :(得分:8)

我遇到了与WindowManager相关的View的类似问题。尝试将ViewGroup直接添加到WindoManager而不是View。它应该工作。

答案 2 :(得分:0)

windowManager需要Android系统的动画。所以自定义动画不起作用

答案 3 :(得分:0)

我有问题。

当我在onAnimationUpdate中使用updateViewLayout并设置LayoutParams的宽度时,动画会掉帧。

但是我设置了LayoutParams的x或y,动画还可以。

类似于以下代码:

        mViewWidth = 800;
        mViewHeight = 800;
        final int oldX = mFloatWindowParams.x;
        final int oldWidth = mFloatWindowParams.width;
        final int oldHeight = mFloatWindowParams.height;
        final int deltaWidth = mViewWidth - oldWidth;
        final int deltaHeight = mViewHeight - oldHeight;
        final boolean isWidthLarger = deltaWidth > deltaHeight;
        int first = isWidthLarger ? oldWidth : oldHeight;
        int end = isWidthLarger ? mViewWidth : mViewHeight;
        ValueAnimator va = ValueAnimator.ofInt(first, end);

        va.setDuration(1000);
        va.setInterpolator(new LinearInterpolator());
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                float fraction = animation.getAnimatedFraction();
                Log.i("onAnimationUpdate", value + "");
                if (isWidthLarger) {
                    mFloatWindowParams.width = value;
                    mFloatWindowParams.height = oldHeight + (int) (deltaHeight * fraction);
                } else {
                    mFloatWindowParams.width = oldWidth + (int) (deltaWidth * fraction);
                    mFloatWindowParams.height = value;
                }
                mFloatWindowParams.x = oldX - (int) (deltaWidth * fraction);

                mWindowManager.updateViewLayout(mRootView, mFloatWindowParams);
            }
        });
        va.start();