简体   繁体   English

Android 翻译动画 - 使用 AnimationListener 将 View 永久移动到新位置

[英]Android translate animation - permanently move View to new position using AnimationListener

I have android translate Animation.我有安卓翻译动画。 I have an ImageView with random generated position (next1, next2).我有一个随机生成位置(next1,next2)的ImageView。 I am calling void every 3 seconds.我每 3 秒调用一次 void。 It generates new position of the View and then make animation and move View to destination position.它生成视图的新位置,然后制作动画并将视图移动到目标位置。 Translate animation has AnimationListener implemented.翻译动画实现了 AnimationListener 。 When the animation finish, I move View permanently to the new position (in OnAnimationEnd).动画完成后,我将 View 永久移动到新位置(在 OnAnimationEnd 中)。 My problem is that animation position does not correspond with setting layoutParams positions.我的问题是动画位置与设置 layoutParams 位置不对应。 When animation end, it makes jump to a new position, which is about 50-100 pixels far.当动画结束时,它会跳转到一个新的位置,大约 50-100 像素远。 I think the positions should be the same because I use same values (next1, next2) in both situations.我认为位置应该相同,因为我在两种情况下都使用相同的值 (next1, next2)。 Please can you show me the way to find sollution ?请你能告诉我找到解决方案的方法吗?在这里绘制情况

 FrameLayout.LayoutParams pozice_motyl = (FrameLayout.LayoutParams)  pozadi_motyl.getLayoutParams();    
            TranslateAnimation anim = new TranslateAnimation(Animation.ABSOLUTE,pozice_motyl.leftMargin, Animation.ABSOLUTE, next1, Animation.ABSOLUTE, pozice_motyl.topMargin, Animation.ABSOLUTE, next2);
            anim.setDuration(1000);
            anim.setFillAfter(true);
            anim.setFillEnabled(true);

            anim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {




                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100, 100);

                    layoutParams.leftMargin = (int)(next1);
                    layoutParams.topMargin = (int)(next2);
                    pozadi_motyl.setLayoutParams(layoutParams);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            pozadi_motyl.startAnimation(anim);

Here is XML layout:这是 XML 布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/pozadi0"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"          
    android:background="@drawable/pozadi2"
    >



<LinearLayout
android:id="@+id/pozadi_motyl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

  <ImageView android:id="@+id/obrazek_motyl"
             android:src="@drawable/motyl"
             android:layout_width="100dip"
             android:layout_height="100dip"
                         />

   </LinearLayout>


<LinearLayout 
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<RelativeLayout
android:id="@+id/obrazek_pozadi0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>



 <ImageView android:id="@+id/zaba_obrazek0"
android:src="@drawable/zaba_obrazek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
/>

</RelativeLayout>   
</LinearLayout> 

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
>

<cz.zaba.Spojnice  
android:layout_width="fill_parent"          
android:layout_height="fill_parent" 
/>

</LinearLayout>
</FrameLayout>

I usually prefer to work with deltas in translate animation, since it avoids a lot of confusion.我通常更喜欢在翻译动画中使用增量,因为它避免了很多混乱。

Try this out, see if it works for you:试试这个,看看它是否适合你:

TranslateAnimation anim = new TranslateAnimation(0, amountToMoveRight, 0, amountToMoveDown);
anim.setDuration(1000);

anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) { }

    @Override
    public void onAnimationRepeat(Animation animation) { }

    @Override
    public void onAnimationEnd(Animation animation) 
    {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
        params.topMargin += amountToMoveDown;
        params.leftMargin += amountToMoveRight;
        view.setLayoutParams(params);
    }
});

view.startAnimation(anim);

Make sure to make amountToMoveRight / amountToMoveDown final确保将amountToMoveRight / amountToMoveDown final

Hope this helps :)希望这有帮助:)

You should rather use ViewPropertyAnimator .您应该使用ViewPropertyAnimator This animates the view to its future position and you don't need to force any layout params on the view after the animation ends.这会将视图动画到其未来位置,并且您不需要在动画结束后在视图上强制任何布局参数。 And it's rather simple.而且相当简单。

myView.animate().x(50f).y(100f);

myView.animate().translateX(pixelInScreen) 

Note: This pixel is not relative to the view.注意:此像素与视图无关。 This pixel is the pixel position in the screen.这个像素是屏幕中的像素位置。

Just Do like this就这样做

view.animate()
            .translationY(-((root.height - (view.height)) / 2).toFloat())
            .setInterpolator(AccelerateInterpolator()).duration = 1500

Here, view is your View which is animating from its origin position.在这里, view是您的 View,它从其原点位置开始动画化。 root is root View of your XML file. root是您的 XML 文件的根视图。

Calculation inside translationY is made for moving your view to the top but keeping it inside the screen, otherwise, it will go partially outside of the screen if you keep its value 0. translationY内部的计算是为了将您的视图移动到顶部但将其保持在屏幕内,否则,如果您将其值保持为 0,它将部分移出屏幕。

This is what worked for me perfectly:-这对我来说非常有用:-

// slide the view from its current position to below itself
public void slideUp(final View view, final View llDomestic){

    ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationY",0f);
    animation.setDuration(100);
    llDomestic.setVisibility(View.GONE);
    animation.start();

}

// slide the view from below itself to the current position
public void slideDown(View view,View llDomestic){
    llDomestic.setVisibility(View.VISIBLE);
    ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationY",   0f);
    animation.setDuration(100);
    animation.start();
}

llDomestic : The view which you want to hide. llDomestic :要隐藏的视图。 view: The view which you want to move down or up.视图:要向下或向上移动的视图。

You can try this way -你可以试试这个方法——

ObjectAnimator.ofFloat(view, "translationX", 100f).apply {
    duration = 2000
    start()
}

Note - view is your view where you want animation.注意- 视图是您想要动画的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM