1

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.

So, for example,

  1. I create an activity called HomeActivity. The content draws as defined in the XML. A View (myTextView) might appear at position0.

  2. I can use a TranslateAnimation to move myTextView to position1.

  3. I can click a button (or whatever) to start, say, MenuActivity.

  4. I exit MenuActivity and resume HomeActivity.

(What I'm seeing)
myTextView shows up at position0 again.

(What I'd like to see)
myTextView is drawn at 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? Or do I need to save their locations before starting the new activity and shift them in 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).

Thanks in advance.

4

1 に答える 1

1

First of all, don't use TranslateAnimation for this, try using ObjectAnimator as it actually move the views. When using TranslateAnimation the views never change place, it only seems so. If this is not enough, try saving the view's location in onSaveInstanceState and in onRestoreInstanceState you can position the view to this locations.

于 2013-01-21T15:08:59.300 に答える