Android的翻译动画 - 使用AnimationListener永久移动浏览到新的位置到新、位置、动画、Android

2023-09-12 11:19:03 作者:斩嘲风

我已经翻译的android动画。我有一个随机生成的位置(NEXT1,​​NEXT2)的ImageView的。我打电话无效每3秒。它生成视图的新位置,然后让动画和移动浏览到目标位置。转换动画已经AnimationListener实施。当动画完成,我将永久地查看到新的位置(在OnAnimationEnd)。我的问题是动画的位置不与设定的LayoutParams位置对应。当动画结束时,它使跳转到一个新的位置,大约是50〜100个像素为止。我想是因为我使用的这两种情况下相同的值(NEXT1,​​NEXT2)的位置应该是相同的。请你能告诉我找到sollution的方式吗?

  FrameLayout.LayoutParams pozice_motyl =(FrameLayout.LayoutParams)pozadi_motyl.getLayoutParams();
            TranslateAnimation动画=新TranslateAnimation(Animation.ABSOLUTE,pozice_motyl.leftMargin,Animation.ABSOLUTE,NEXT1,​​Animation.ABSOLUTE,pozice_motyl.topMargin,Animation.ABSOLUTE,NEXT2);
            anim.setDuration(1000);
            anim.setFillAfter(真正的);
            anim.setFillEnabled(真正的);

            anim.setAnimationListener(新Animation.AnimationListener(){
                @覆盖
                公共无效onAnimationStart(动画动画){
                }

                @覆盖
                公共无效onAnimationEnd(动画动画){




                    FrameLayout.LayoutParams的LayoutParams =新FrameLayout.LayoutParams(100,100);

                    layoutParams.leftMargin =(INT)(NEXT1);
                    layoutParams.topMargin =(INT)(NEXT2);
                    pozadi_motyl.setLayoutParams(的LayoutParams);

                }

                @覆盖
                公共无效onAnimationRepeat(动画动画){

                }
            });

            pozadi_motyl.startAnimation(动画);
 

下面是XML布局:

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:广告=htt​​p://schemas.android.com/apk/lib/com.google.ads
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / pozadi0
    机器人:重力=center_vertical
    机器人:layout_gravity =center_vertical
    机器人:背景=@可绘制/ pozadi2
    >



<的LinearLayout
机器人:ID =@ + ID / pozadi_motyl
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        >

  < ImageView的机器人:ID =@ + ID / obrazek_motyl
             机器人:SRC =@可绘制/莫季尔
             机器人:layout_width =100dip
             机器人:layout_height =100dip
                         />

   < / LinearLayout中>


<的LinearLayout
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>

< RelativeLayout的
机器人:ID =@ + ID / obrazek_pozadi0
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>



 < ImageView的机器人:ID =@ + ID / zaba_obrazek0
机器人:SRC =@可绘制/ zaba_obrazek
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_centerInParent =真
/>

< / RelativeLayout的>
< / LinearLayout中>

<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>

< cz.zaba.Spojnice
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
/>

< / LinearLayout中>
< /的FrameLayout>
 

解决方案

我通常preFER与在翻译动画三角洲工作,因为它避免了很多混乱。尝试了这一点,看看它是否适合你:

  TranslateAnimation动画=新TranslateAnimation(0,amountToMoveRight,0,amountToMoveDown);
anim.setDuration(1000);

anim.setAnimationListener(新TranslateAnimation.AnimationListener(){

@覆盖
公共无效onAnimationStart(动画动画){}

@覆盖
公共无效onAnimationRepeat(动画动画){}

@覆盖
公共无效onAnimationEnd(动画动画)
{
    FrameLayout.LayoutParams PARAMS =(FrameLayout.LayoutParams)view.getLayoutParams();
    params.topMargin + = amountToMoveDown;
    params.leftMargin + = amountToMoveRight;
    view.setLayoutParams(PARAMS);
}
});

view.startAnimation(动画);
 
给你了哦

请务必使 amountToMoveRight / amountToMoveDown 最后

希望这有助于:)

I have android translate Animation. I have an ImageView with random generated position (next1, next2). I am calling void every 3 seconds. It generates new position of the View and then make animation and move View to destination position. Translate animation has AnimationListener implemented. When the animation finish, I move View permanently to the new position (in OnAnimationEnd). My problem is that animation position does not correspond with setting layoutParams positions. When animation end, it makes jump to a new position, which is about 50-100 pixels far. I think the positions should be the same because I use same values (next1, next2) in both situations. 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:

<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

Hope this helps :)