Android Can';t设置从getWindowManager()创建的视图的动画。addView

Android Can';t设置从getWindowManager()创建的视图的动画。addView,android,android-layout,android-animation,Android,Android Layout,Android Animation,我在做这个酒吧 它的工作很好。但是,我想动画它从顶部滑下来 我可以设置在布局中创建的其他对象的动画,但不能通过getWindowManager().addview 这是当前布局中我的对象的工作 switch(item.getItemId()) { case R.id.zoomInOut: TextView image = (TextView)findViewById(R.id.textView); Animation animation = Animat

我在做这个酒吧

它的工作很好。但是,我想动画它从顶部滑下来

我可以设置在布局中创建的其他对象的动画,但不能通过getWindowManager().addview

这是当前布局中我的对象的工作

switch(item.getItemId())
{
    case R.id.zoomInOut:
        TextView image = (TextView)findViewById(R.id.textView);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);
        image.startAnimation(animation);
        return true;
    case R.id.rotate360:
        TextView image1 = (TextView)findViewById(R.id.textView);
        Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
        image1.startAnimation(animation1);
        return true;
    case R.id.fadeInOut:
        TextView image2 = (TextView)findViewById(R.id.textView);
        Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
        image2.startAnimation(animation2);
        return true;
}
这不适用于通过getWindowManager()添加的我的视图。addview

我想设置动画,使其从顶部向下滑动

调用
WindowManager.addView
后,可以将
ViewTreeObserver.OnGlobalLayoutListener
附加到
视图
并使用该视图获取高度,然后使用
ObjectAnimator
执行转换。下面是一个例子:

mMessage.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        // Get the height of the view
        final int h = mMessage.getHeight();
        // Remove the OnGlobalLayoutListener callback
        mMessage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        // Slide down from the top
        final ObjectAnimator oa = ObjectAnimator.ofFloat(mMessage, "translationY", -h, 0f);
        oa.setDuration(250);
        oa.start();
    }

});
如果您想稍后将其滑回,请执行相反的转换,并附加一个
AnimatorListener
,以了解何时调用
WindowManager.removeViewImmediate
。例如:

private void removeLoadingMessage(boolean animate) {
    if (mMessage != null && mMessage.getWindowToken() != null) {
        // Detach the View immediately, don't wait for the animation to end
        if (!animate) {
            getWindowManager().removeViewImmediate(mMessage);
            mMessage = null;
            return;
        }

        // Slide back up
        final int h = mMessage.getHeight();
        final ObjectAnimator oa = ObjectAnimator.ofFloat(mMessage, "translationY", 0f, -h);
        oa.setDuration(250);
        // Wait until the end of the animation to detach the View
        oa.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                getWindowManager().removeViewImmediate(mMessage);
                mMessage = null;
            }
        });
        oa.start();
    }
}

为了避免任何泄漏,在
Activity.ondestory
中,您需要使用
removeLoadingMessage(false)
确保
视图
窗口正确分离

谢谢您的解决方案!!它起作用了!!然而,我为其他人添加了一个注意事项,为了使用api<11下面的ObjectAnimator,您需要使用这个链接中的额外库choose.jar选择一个并添加到libs forder use as library中
private void removeLoadingMessage(boolean animate) {
    if (mMessage != null && mMessage.getWindowToken() != null) {
        // Detach the View immediately, don't wait for the animation to end
        if (!animate) {
            getWindowManager().removeViewImmediate(mMessage);
            mMessage = null;
            return;
        }

        // Slide back up
        final int h = mMessage.getHeight();
        final ObjectAnimator oa = ObjectAnimator.ofFloat(mMessage, "translationY", 0f, -h);
        oa.setDuration(250);
        // Wait until the end of the animation to detach the View
        oa.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                getWindowManager().removeViewImmediate(mMessage);
                mMessage = null;
            }
        });
        oa.start();
    }
}