请查看动画在最后的动画位置动画、请查看、位置

2023-09-05 05:49:12 作者:劳资真想跳ai(崖)

所以我使用的动画动画视图:

So I'm animating a view using animation:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="1000"
        android:fromYDelta="0%"
        android:toYDelta="-75%" />

</set>

它确实在视觉上是我想要的,但我也想冻结这个动画的结果。就像我需要以某种方式得到得到的布局参数(或别的东西吗?)动画完成后,并将其设置为布局。假设布局的Y坐标从0更改为100,但我真的不知道发生了什么开始COORDS什么都造成COORDS。我需要得到的COORDS并将其设置为布局的Cuz,如果不这样做的布局则返回到其动画后,最初的位置,但我需要使它留在返回回在新的位置代替。此外,我需要2.3.x兼容的解决方案。照片 更新1 我也试过NineOldAndroids:

It does visually what I want but I also I want to freeze result of this animation. Like I need somehow to get resulting layout parameters (or something else?) after animation and set it to layout. Assume layout Y-coord is changing from 0 to 100 but I don't really know what was starting coords and what are resulting coords. I need to get resulting coords and set it to the layout cuz if wouldn't do so layout returns back to its initial position after animation but I need to make it stay in new position instead of returning back. Also I need 2.3.x compatible solution. UPDATE 1 I also tried NineOldAndroids:

ObjectAnimator.ofFloat(rootWrapper, "translationY", -rootWrapper.getHeight()*75/100).start();

这动画的观点和看法停留在动画位置 - 那正是我想达到但是所有控件停留在他们的位置几乎。即使你布局只能由其底部的25%,剩下的屏幕75%看空明显,如果我点击那个空白的屏幕区域,然后按钮的(这是有B4动画)的onClick触发。同样的情况,使用XML-animtaions如果设置

this animates the view and view stays at animated position - thats exactly what I want to achieve. However all controls stays at their positions virtually. Even thou layout is only visible by 25% of its bottom part and 75% of rest screen looks empty if I tap that empty screen area then button's (which was there b4 animation) onClick triggered. The same happens with xml-animtaions if set

animation.setFillAfter(true); 

如何解决这个问题?所有的控制必须移动的观点。 更新2 在code:

How to fix that issue? All controls has to move with the view. UPDATE 2 The code:

    public void showAbout(){

        final Animation animShow = AnimationUtils.loadAnimation(this, R.anim.about_in_from_bottom);
        animShow.setFillAfter(true); 

        //ObjectAnimator.ofFloat(rootWrapper, "translationY", -rootWrapper.getHeight()*75/100).start();

        animShow.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                LinearLayout.LayoutParams rootlp = (LinearLayout.LayoutParams) rootWrapper.getLayoutParams();
                Log.i(this,"rootlp: h: "+rootlp.topMargin+ " / w: "+rootlp.bottomMargin);
                rootlp.topMargin = -rootlp.height*75/100;
                rootWrapper.setLayoutParams(rootlp);
            }
        });

        rootWrapper.startAnimation(animShow);
    }

我有问题 - rootlp.height 返回0的Cuz它像MATCH_PARENT或喜欢。它不会返回真实像素,但再presenting一些常有的价值!所以它看起来像我有2个游戏与 .getViewTreeObserver()。addOnGlobalLayoutListener 。 好吧,我得到了rootWrapper的实际高度通过getViewTreeObserver()。而现在使用公式 rootlp.topMargin = -rootlp.height * 75/100; 我得到更多的问题 1)在动画浏览向上移动。此外(由于设定LP)。 2)控制动画视图包含(按钮,它们必须与父视图向上移动)保持在其位置几乎(他们是无形的,但点击)!视觉上的控件移动与动画观,我不能达到它(它会关闭屏幕)。但是,如果我点击区域,他们是动画相应的onClick()之前触发!多么有趣! 更新3 最后,我已经实现了我的目标!问题是与移动浏览的子容器。我在想,如果我动一番查看,然后所有的子视图也动人。但那不是这样的,我是有动画完成修复鬼按钮后,手动移动孩子的意见。

I got the problem - rootlp.height returns 0 cuz its like MATCH_PARENT or like. It wont return the REAL pixels but some value representing some constant! So it looks like I have 2 play with .getViewTreeObserver().addOnGlobalLayoutListener. Ok, I got the real height of rootWrapper via getViewTreeObserver(). And now using the formula rootlp.topMargin = -rootlp.height*75/100; I'm getting even more issues. 1) After animation View moves up additionally (due to setting LP). 2) Controls that animated View contains (buttons which has to move up with the parent View) stays at their positions virtually (they are invisible but clickable)! Visually those controls are moved up with the animated View and I can't reach it (it goes off the screen). But if I tap area where they was before animation corresponding onClick() triggers! What a fun! UPDATE 3 Finally I had achieved my goal! The problem was with moving View's child containers. I was thinking that if I move some View then all of its child views are also moving. But thats not the case and I was had to move child views manually after animation completes to fix ghost buttons.

推荐答案

请考虑 setFillAfter(布尔); 将其设置为true,将会使视图留在动画位置

Please consider setFillAfter(boolean); setting it to true will make the view stay at the animated position.

Animation anim = new TranslateAnimation(0, 0, 0, 100);

// or like this
Animation anim = AnimationUtils.loadAnimation(this, R.anim.animation_name);

anim.setFillAfter(true); 
anim.setDuration(1000);

这是可以做到用更简单的 ViewPropertyAnimator ,可从API 14及以后:

This can be done even easier using the ViewPropertyAnimator, available from API 14 and onwards:

int animationpos = 500;
View.animate().y(animationpos).setDuration(1000); // this will also keep the view at the animated position

或者考虑一个 AnimationListener 获得的LayoutParams动画后,正是:

Or consider an AnimationListener to get the LayoutParams exactly after the animation:

anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                  // get layoutparams here and set it in this example your views 
                  // parent is a relative layout, please change that to your desires

                  RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) View.getLayoutParams();
                  lp.topMargin = yournewtopmargin // use topmargin for the y-property, left margin for the x-property of your view
                  View.setLayoutParams(lp);
                }
            });

    View.startAnimation(anim);

更新:

如何知道有多少像素的观点提出,根据   动画的百分比值?

How to know how many pixels the view moved, depending on the percentage values of the animation?

在你的.xml动画中的百分比值是相对于动画显示的尺寸(如宽度和高度)。所以,你只需要使用您查看的width和height属性(视天气u使用X或Y的动画取得像素的实际动画的距离)。

The percentage value in your .xml animation is relative to the animated View's dimensions (such as width and height). Therefore, you just need to use your View's width and height properties (depending on weather u use x or y animation to get the actual animation distance in pixels).

例如:

您查看具有400像素的宽度。动画动画的x属性从0%到75%,这意味着你的看法将移动300个像素(400 * 0.75)右侧。所以onAnimationEnd(),设置LayoutParams.leftMargin〜300,你的视图将具有所需的位置。

Your View has a width of 400 pixels. Your animation animates the x-property from 0% to 75%, meaning that your view will move 300 pixels (400 * 0.75) to the right side. So onAnimationEnd(), set the LayoutParams.leftMargin to 300 and your View will have the desired position.