简体   繁体   English

如何使动画的效果在活动之间持续存在?

[英]How do I get an animation's effect to persist between activities?

I've been successfully using animation in my project, with TranslateAnimation objects, but when I return to the activity whose views have been animated, they redraw at their default positions. 我已经在我的项目中成功使用了带有TranslateAnimation对象的动画,但是当我返回视图已被动画化的活动时,它们将在其默认位置重新绘制。

So, for example, 因此,例如

  1. I create an activity called HomeActivity. 我创建一个名为HomeActivity的活动。 The content draws as defined in the XML. 内容按照XML中的定义绘制。 A View (myTextView) might appear at position0. 视图(myTextView)可能会出现在position0。

  2. I can use a TranslateAnimation to move myTextView to position1. 我可以使用TranslateAnimation将myTextView移到position1。

  3. I can click a button (or whatever) to start, say, MenuActivity. 我可以单击一个按钮(或其他按钮)以启动,例如MenuActivity。

  4. I exit MenuActivity and resume HomeActivity. 我退出MenuActivity并恢复HomeActivity。

(What I'm seeing) (我所看到的)
myTextView shows up at position0 again. myTextView再次显示在position0处。

(What I'd like to see) (我想看到的)
myTextView is drawn at position1. myTextView在position1绘制。

Here's how I perform the animation. 这是我执行动画的方法。

TextView myTextview = (TextView)findViewById(R.id.mytextview);

private void animate()
{
Animation anim = new TranslateAnimation(0, displacementX, 0, displacementY);
anim.setDuration(200);
anim.setAnimationListener(this); // This activity implements AnimationListener
anim.setFillEnabled(true);
anim.setFillAfter(true);
myTextview.startAnimation(anim);
}

@Override
public void onAnimationEnd( Animation animation )
{
 myTextview.clearAnimation();
}

Are there any flags I should set, maybe in the above procedure, onPause or in onAnimationEnd, that would cause the Views to retain their post-animated state? 我是否应该在上述过程中的onPause或onAnimationEnd中设置任何标志,这些标志将导致View保留其动画后状态? Or do I need to save their locations before starting the new activity and shift them in onResume? 还是我需要在开始新活动并将其移入onResume之前保存其位置? Or is there another way altogether? 还是完全有另一种方式?

Please let me know if you need any clarification. 如果需要任何说明,请告诉我。

Forgot to specify, minimum SDK is 2.2 (API 8). 忘记指定,最低SDK为2.2(API 8)。

Thanks in advance. 提前致谢。

First of all, don't use TranslateAnimation for this, try using ObjectAnimator as it actually move the views. 首先,不要为此使用TranslateAnimation ,请尝试使用ObjectAnimator因为它实际上会移动视图。 When using TranslateAnimation the views never change place, it only seems so. 使用TranslateAnimation ,视图永远不会改变位置,只是看起来是这样。 If this is not enough, try saving the view's location in onSaveInstanceState and in onRestoreInstanceState you can position the view to this locations. 如果这还不够,请尝试将视图的位置保存在onSaveInstanceState然后在onRestoreInstanceState中将视图定位到该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM