我的一项活动中有以下视图设置:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photoLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
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"
>
如果没有动画集,则显示效果很好。不过我想显示一个非常简单的动画。因此,在我的 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 才会“捕捉”回预期位置。
我看了又看,找不到如何避免这个问题。我希望 photoImageView 从屏幕中心开始,然后发生动画,并将其返回到屏幕中心。动画应该自行发生,无需用户交互。
I have the following View setup in one of my Activities:
<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>
Without an animation set, this displays just fine. However I want to display a very simple animation. So in my Activity's onStart override, I have the following:
@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);
}
The problem is that when the Activity displays for the first time, the initial position of the photo is not in the center of the screen with padding around. It seems like the first frame of the animation is loaded already. Only after the animation is completed, does the photoImageView "snap" back to the intended location.
I've looked and looked and could not find how to avoid this problem. I want the photoImageView to start in the center of the screen, and then the animation to happen, and return it to the center of the screen. The animation should happen by itself without interaction from the user.
发布评论
评论(1)
mPhotoImageView.getTop() 将在 onCreate() 中返回 0,您需要等到第一次布局/绘制之后才能获取 View 的正确位置。
mPhotoImageView.getTop() will return 0 in onCreate(), you need to wait until after the first layout/draw has happened to get the correct position of your View.