将视图转换到屏幕边界之外并将其设置为动画

时间:2014-10-14 07:36:45

标签: java android eclipse animation

我正在使用以下代码,我有一个高度为50dp的相对布局,我想让它动画并移动到屏幕上方。这个动画很好。

然而,当我把它带回来时,它会抽搐。

TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -100);
        anim.setDuration(1000);

        anim.setAnimationListener(new TranslateAnimation.AnimationListener() 
        {
            @Override
            public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) 
            {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) locationView.getLayoutParams();
                params.topMargin   -= 100;
                locationView.setLayoutParams(params);
                locationView.setVisibility(View.GONE);
            }
        });



Bringing back code


TranslateAnimation anim = new TranslateAnimation(0, 0, 0, +100);
        anim.setDuration(1000);

        anim.setAnimationListener(new TranslateAnimation.AnimationListener() 
        {
            @Override
            public void onAnimationStart(Animation animation) 
            {       
                locationView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) 
            {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) locationView.getLayoutParams();
                params.topMargin   += 100;
                locationView.setLayoutParams(params);               
            }
        });
        locationView.startAnimation(anim);

1 个答案:

答案 0 :(得分:2)

我本来期待不同的东西

这会将您的视图顶部从0转换为-100px

 TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -100);

onAnimationEnd只是将其可见性更改为GONE,而不更改其布局参数。要将其恢复,您必须将视图的顶部从-100px翻译为0

 TranslateAnimation anim = new TranslateAnimation(0, 0, -100, 0);
在HoneyComb之前的

,如果视图不可见,动画将无法启动,并且onAnimationStart将不会被触发,因此您应该调用

locationView.setVisibility(View.VISIBLE);

之前view.startAnimation(anim);