2011-01-06 8 views
1

Ich habe folgende Ansicht Setup in einem meiner Aktivitäten:ersten Frame Android-Animation zu früh auf Image angewendet

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photoLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/photoImageView"
android:src="@drawable/backyardPhoto"android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:scaleType="centerInside"
android:padding="45dip"
>
</ImageView>
</LinearLayout>

Ohne eine Animation-Set, das zeigt ganz gut. Ich möchte jedoch eine sehr einfache Animation anzeigen. Also meine Tätigkeit des onStart überschreiben, ich habe folgende:

@Override 
public void onStart() { 
super.onStart(); 

    mPhotoImageView = (ImageView) findViewById(R.id.photoImageView); 

float offset = -25; 
int top = mPhotoImageView.getTop(); 

TranslateAnimation anim1 = new TranslateAnimation(
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, 
Animation.ABSOLUTE, top, Animation.ABSOLUTE, offset); 
anim1.setInterpolator(new AnticipateInterpolator()); 
    anim1.setDuration(1500); 
    anim1.setStartOffset(5000); 

    TranslateAnimation anim2 = new TranslateAnimation(
     Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, 
     Animation.ABSOLUTE, offset, Animation.ABSOLUTE, top); 
anim2.setInterpolator(new BounceInterpolator()); 
    anim2.setDuration(3500); 
    anim2.setStartOffset(6500); 

    mBouncingAnimation = new AnimationSet(false); 
    mBouncingAnimation.addAnimation(anim1); 
    mBouncingAnimation.addAnimation(anim2); 

    mPhotoImageView.setAnimation(mBouncingAnimation); 
} 

Das Problem ist, dass, wenn die Aktivität zeigt zum ersten Mal, die Anfangsposition des Fotos ist nicht in der Mitte des Bildschirms mit Polsterung. Es scheint, als wäre der erste Frame der Animation bereits geladen. Erst nachdem die Animation abgeschlossen ist, "springt" die PhotoImageView an den gewünschten Ort zurück.

Ich habe geschaut und geschaut und konnte nicht finden, wie man dieses Problem vermeidet. Ich möchte, dass die photoImageView in der Mitte des Bildschirms beginnt und dann die Animation eintritt und in die Mitte des Bildschirms zurückkehrt. Die Animation sollte ohne Interaktion des Benutzers selbst erfolgen.

Antwort

0

mPhotoImageView.getTop() gibt 0 in onCreate() zurück, Sie müssen warten, bis nach dem ersten Layout/Zeichnen die korrekte Position Ihrer Ansicht erreicht wurde.

+0

Danke Romain Guy! Das hat es gelöst. – Robert

Verwandte Themen