Android动画第一帧在ImageView上应用过早

发布于 2024-10-10 20:16:40 字数 1992 浏览 1 评论 0 原文

我的一项活动中有以下视图设置:


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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-10-17 20:16:40

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文