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

问题描述 投票:0回答:7

我有android翻译动画。我有一个带有随机生成位置(next1,next2)的 ImageView。我每 3 秒调用 void 一次。它生成视图的新位置,然后制作动画并将视图移动到目标位置。翻译动画已经实现了AnimationListener。动画完成后,我将 View 永久移动到新位置(在 OnAnimationEnd 中)。我的问题是动画位置与设置布局参数位置不对应。当动画结束时,它跳转到一个新的位置,大约50-100像素远。我认为位置应该相同,因为我在两种情况下使用相同的值(next1,next2)。请您告诉我找到解决方案的方法吗? Drawing of situation here here

 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);

这是 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>
android android-animation translate-animation
7个回答
57
投票

我通常更喜欢在翻译动画中使用增量,因为它可以避免很多混乱。

尝试一下,看看它是否适合您:

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);

确保制作

amountToMoveRight
/
amountToMoveDown
final

希望这有帮助:)


49
投票

您应该使用ViewPropertyAnimator。这会将视图动画到其未来位置,并且动画结束后您无需在视图上强制使用任何布局参数。而且相当简单。

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

myView.animate().translateX(pixelInScreen) 

注意:该像素与视图无关。这个像素就是这个像素 在屏幕中的位置。


12
投票

就这样做

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

这里,

view
是您的视图,它从其原始位置开始动画。
root
是 XML 文件的根视图。

translationY
内部的计算是为了将视图移动到顶部但将其保留在屏幕内,否则,如果将其值保留为0,它将部分移到屏幕外。


2
投票

这对我来说非常有效:-

// 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 :您要隐藏的视图。 view:您想要向下或向上移动的视图。


2
投票

你可以试试这个方法 -

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

注意 - 视图是您想要动画的视图。


1
投票

列出3种实现此目的的方法以供将来参考

fun translateView(viewToAnimate: View, positionX: Int) {
        

直接动画视图

        viewToAnimate.animate()
            .x(positionX)
            .setDuration(600L)
            .setInterpolator(defaultAnimationInterpolator)
            .start()

对象动画师

         ObjectAnimator.ofFloat(
            viewToAnimate,
            View.X,
            positionX
        ).apply {
            duration = 600
            interpolator = defaultAnimationInterpolator
        }.start()

价值动画师

        ValueAnimator.ofFloat(viewToAnimate.x, positionX).apply {
            duration = 2000
            interpolator = defaultAnimationInterpolator
            addUpdateListener {
                viewToAnimate.x = it.animatedValue as Float
            }
            start()
        }
    }

0
投票

添加这个功能就可以了。我们很高兴去..🫶🏻

    fun moveView(viewToMove: View, targetView: View, repeatCount: Int? = 10) {
        val viewToMovePos = IntArray(2)
        viewToMove.getLocationOnScreen(viewToMovePos)

        val targetViewPos = IntArray(2)
        targetView.getLocationOnScreen(targetViewPos)

        // Calculate the distance to move
        val moveX = targetViewPos[0] - viewToMovePos[0]
        val moveY = targetViewPos[1] - viewToMovePos[1]

        // Create a TranslateAnimation for the movement
        val animation = TranslateAnimation(0f, moveX.toFloat(), 0f, moveY.toFloat())
        animation.duration = 300 // Set the duration of the animation (in milliseconds)

        // Set animation properties (optional)
        animation.fillAfter = true
        animation.repeatCount = repeatCount ?: 10


        // Start the animation on the source view
        viewToMove.startAnimation(animation)
    }
© www.soinside.com 2019 - 2024. All rights reserved.