安卓:动画位置复位后完成位置、动画

2023-09-12 01:47:28 作者:相约奈何桥

我使用的是定义XML动画幻灯片视图关闭屏幕。现在的问题是,一旦在动画完成它重置到原来的位置。我需要知道如何解决这个问题。这里的XML:

I'm using an xml defined animation to slide a view off the screen. The problem is, as soon as the animation completes it resets to its original position. I need to know how to fix this. Here's the xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
   <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/></set>

下面是我用它来称呼它为Java的:

Here's the Java that I use to call it:

    homeScrn = (View)findViewById(R.id.homescreen);
    slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);

    //Set Click Listeners For Menu
    btnHelp.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            LayoutInflater.from(getApplicationContext()).inflate(R.layout.help, (ViewGroup)findViewById(R.id.subpage), true);
            homeScrn.startAnimation(slideLeftOut);
        }
    });

所以基本上会发生什么是我吹1下的一个观点。然后,我在动画顶视图到左边。只要它下车的屏幕和动画完成它重置回其位置。

So basically what happens is I inflate a view underneath one. Then I animate the view on top off to the left. As soon as it gets off screen and the animation is finished it resets its position back.

推荐答案

该动画是否按预期工作。仅仅因为你的动画的东西并不意味着你真的感动了。动画只动画本身,而不是部件的配置过程中会影响绘制的像素。

The animations are working as expected. Just because you animated something does not mean you actually moved it. An animation only affects drawn pixels during the animation itself, not the configuration of the widgets.

您需要添加一个 AnimationListener ,而在 onAnimationEnd()的方法,做一些让你的移动永久(例如,取消了对从其父顶视图,标志着在顶部视图具有可视性 GONE )。

You need to add an AnimationListener, and in the onAnimationEnd() method, do something that makes your move permanent (e.g., removes the view on top from its parent, marks the view on top as having visibility GONE).

 
精彩推荐
图片推荐