java - 将 View 平移到屏幕边界之外并将其动画回来

标签 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);

最佳答案

我本来期待一些不同的东西

这会将 View 顶部从 0 转换为 -100px

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

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

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

在HoneyComb之前,如果 View 不可见,动画将不会启动,并且onAnimationStart也不会被触发,所以你应该调用

locationView.setVisibility(View.VISIBLE);

之前view.startAnimation(anim);

关于java - 将 View 平移到屏幕边界之外并将其动画回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355174/

相关文章:

java - Netbeans 8.0 全新安装无法静默启动

Java 打印图形均匀性

android - 如何停止在线程内运行的媒体播放器

java - 从 Kotlin 调用需要 Function 参数的 Java 方法

java - 将时钟格式的字符串解析为int

android - 如何解决 Android 12 中的 SecurityException 错误?

android - IntentService 和 Service 有什么区别?

java - 如何在eclipse中找到前向引用?

java - 在 Eclipse 中打开一个项目

java - 将-source设置为1.5,显然设置为1.3