2013-09-25 39 views
26

Tôi có dịch Android hoạt hình. Tôi có một ImageView với vị trí được tạo ngẫu nhiên (next1, next2). Tôi kêu gọi vô hiệu cứ sau 3 giây. Nó tạo ra vị trí mới của Chế độ xem và sau đó tạo hoạt ảnh và di chuyển Xem đến vị trí đích. Dịch hình ảnh động đã AnimationListener được triển khai. Khi kết thúc hoạt ảnh, tôi di chuyển View vĩnh viễn đến vị trí mới (trong OnAnimationEnd). Vấn đề của tôi là vị trí hoạt ảnh không tương ứng với các vị trí thiết lập layoutParams. Khi hoạt ảnh kết thúc, nó chuyển sang vị trí mới, cách khoảng 50-100 pixel. Tôi nghĩ rằng các vị trí nên giống nhau bởi vì tôi sử dụng cùng một giá trị (next1, next2) trong cả hai tình huống. Bạn có thể chỉ cho tôi cách để tìm thấy sollution? Drawing of situation here hereHoạt ảnh dịch trên Android - di chuyển vĩnh viễn Xem đến vị trí mới bằng cách sử dụng AnimationListener

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

Đây là 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> 
+0

xin vui lòng xem câu trả lời của tôi, nó trực tiếp liên quan: https://stackoverflow.com/a/47593484/2606290 – Fer

Trả lời

42

Tôi thường thích làm việc với đồng bằng châu thổ trong dịch hoạt hình, vì nó tránh được rất nhiều sự nhầm lẫn.

Hãy thử điều này ra, xem nó có phù hợp với bạn:

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

Hãy chắc chắn sẽ làm amountToMoveRight/amountToMoveDownfinal

Hope this helps :)

+2

Cảm ơn Gil. Chỉnh sửa nhỏ: TranslateAnimation anim = new TranslateAnimation (0, amountToMoveRight, 0, amountToMoveDown); – Srikanth

+6

Điều này gây ra một nhấp nháy, xem: http://stackoverflow.com/questions/2650351/android-translateanimation-resets-after-animation –

+11

không cần phải thêm người nghe, chỉ cần sử dụng setFillAfter (true) – Valera

21

Bạn nên sử dụng thay ViewPropertyAnimator. Thao tác này sẽ hiển thị chế độ xem cho vị trí tương lai của nó và bạn không cần ép buộc bất kỳ thông số bố cục nào trên chế độ xem sau khi hoạt ảnh kết thúc. Và nó khá đơn giản.

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

myView.animate().translateX(pixelInScreen) 

Lưu ý: Pixel này không liên quan đến chế độ xem. Pixel này là vị trí pixel trên màn hình.

2

bạn chỉ có thể làm như

myView.animate().y(0f);//to move to the top of the screen.