2013-09-02 48 views
7

Tôi đang tạo hoạt ảnh trong đó hình ảnh dịch từ hướng này sang hướng khác, hoạt ảnh sẽ di chuyển một nửa hình ảnh ra ngoài màn hình nhưng hình ảnh hiển thị đầy đủ khi hoạt ảnh kết thúc. Tôi chỉ muốn hiển thị một nửa hình ảnh sau hoạt ảnh.Làm thế nào để hiển thị một phần hình ảnh trên màn hình ở phần cuối của hình động

Hiện tại tôi đang sử dụng hình ảnh đầy đủ ở chế độ xem hình ảnh khi hoạt ảnh bắt đầu và thay thế bằng nửa hình ảnh khi hoạt ảnh kết thúc nhưng nó cho thấy một sự thay đổi hình ảnh trông có vẻ khó xử.

Dưới đây là tệp xml và lớp học của tôi.

mã hoạt hình

<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration = "2000" 
    android:fillBefore="true" 
    android:fillEnabled="true" 
    android:fromXDelta = "-300%" 
    android:fromYDelta="200%" 
    android:toXDelta="60%"> 

</translate> 

animimv5.setAnimationListener(new AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
       imv5.setBackgroundResource(R.drawable.img5); 
      } 
      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       imv5.setBackgroundResource(R.drawable.img5_2); 
      } 
     }); 
+1

Kiểm tra bài đăng này: http://stackoverflow.com/questions/3345084/how-can-i-animate-a-view-in-android-and-have-it-stay-in-the-new-position-size fillAfter = true fillEnabled = true – Juangcg

+0

thử sử dụng 'fillAfter (true)' – Siddhesh

+0

Bạn đang nhắm mục tiêu SDK nào? Tôi có thể đăng cho bạn một giải pháp đầy đủ bằng cách sử dụng ObjectAnimator, nhưng nó chỉ hoạt động từ cấp API 11. – Ivelius

Trả lời

1

Bạn có thể thử như sau:

trong xml (thêm nó vào thiết lập tham số) android:fillAfter hoặc sử dụng phương pháp liên quan trong nguồn lớp setFillAfter(boolean)

Khi thiết lập là true , chuyển đổi hoạt ảnh được áp dụng sau khi hoạt ảnh kết thúc. Theo tài liệu here Khi được đặt thành true, chuyển đổi hoạt ảnh sẽ được áp dụng sau khi hoạt ảnh kết thúc. Giá trị mặc định là sai. Nếu fillEnabled không phải là được đặt thành true và hoạt ảnh không được đặt trên Chế độ xem, fillAfter là giả định là đúng.

Đối với các mẹo và lời giải thích kiểm tra Chet's blog post

Hy vọng nó giúp ..;)

0

Ở đây bạn đi, thưa ông:

MainActivity : 

    public class MainActivity extends Activity { 

    private View animatedView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     animatedView = findViewById(R.id.view); 
    } 

    public void animate(View view) { 
     int targetX = ((View) animatedView.getParent()).getWidth() - animatedView.getWidth()/2; 
     ObjectAnimator.ofFloat(animatedView, "x", 0, targetX).setDuration(1000).start(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

Layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".MainActivity"> 

    <TextView 
      android:id="@+id/view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#CC0000" 
      android:padding="30dp" 
      android:textColor="@android:color/white" 
      android:text="@string/hello_world"/> 
    <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Animate" 
      android:onClick="animate" 
      android:id="@+id/button" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true"/> 

</RelativeLayout> 
Các vấn đề liên quan