У меня есть следующая настройка просмотра в одном из моих действий:
<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>
Без набора анимации это отображается просто отлично. Однако я хочу показать очень простую анимацию. Поэтому в моей переопределении Activity onStart у меня есть следующее:
@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);
}
Проблема заключается в том, что когда Activity отображается в первый раз, начальная позиция фотографии не находится в центре экрана с дополнением вокруг. Кажется, что уже загружен первый кадр анимации. Только после завершения анимации фотокамера "привязывается" к предполагаемому местоположению.
Я посмотрел и посмотрел и не мог найти, как избежать этой проблемы. Я хочу, чтобы photoImageView начинался в центре экрана, а затем анимация, и возвращалась в центр экрана. Анимация должна происходить сама по себе без взаимодействия с пользователем.
mPhotoImageView.getTop() вернет 0 в onCreate(), вам нужно подождать, пока после первого макета/ничья не получится правильное положение вашего представления.