2013-01-03 40 views
14

Tôi không thể đặt hoạt ảnh xem cho bố cục được tăng cường. tôi đã sử dụng đoạn mã sauXem hoạt ảnh từ phải sang trái android

pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml)); 

và xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shareInterpolator="false"> 
    <translate android:fromXDelta="0%" android:toXDelta="100%" 
      android:fromYDelta="0%" android:toYDelta="0%" 
     android:duration="700"/> 

</set> 

là bất cứ điều gì tôi mất tích?

Cảm ơn.

+0

Bạn nhận được gì khi bạn cố gắng chạy hoạt ảnh? – WarrenFaith

+0

Nó chỉ trượt sang trái sang phải rồi sang phải sang trái khi làm động một chế độ xem. – sd33din90

+0

và bạn muốn điều gì sẽ xảy ra thay thế? – WarrenFaith

Trả lời

55

Đây là mã cho hoạt hình trượt để xem.

1)inFromRightAnimation 

    private Animation inFromRightAnimation() { 

     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, +1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f); 
     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
     } 

2)outToLeftAnimation 
    private Animation outToLeftAnimation() { 
    Animation outtoLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, -1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoLeft.setDuration(500); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
    } 

3)inFromLeftAnimation 

    private Animation inFromLeftAnimation() { 
    Animation inFromLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, -1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f); 
    inFromLeft.setDuration(500); 
    inFromLeft.setInterpolator(new AccelerateInterpolator()); 
    return inFromLeft; 
    } 

4)outToRightAnimation 

    private Animation outToRightAnimation() { 
    Animation outtoRight = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, +1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoRight.setDuration(500); 
    outtoRight.setInterpolator(new AccelerateInterpolator()); 
    return outtoRight; 
    } 

và bây giờ bắt đầu Animation vào xem

pageView.startAnimation(inFromRightAnimation()); 

Cảm ơn,

+1

nhưng cách bắt đầu hoạt ảnh cho chế độ xem. –

+0

yourview.startAnimation (inFromRightAnimation()); –

+0

câu trả lời rất hay – Lokesh

1

Nếu bạn đang cố tạo hiệu ứng cho chế độ xem khi lần đầu tiên được tạo, thì bạn cần phải đặt thuộc tính XML layoutAnimation hoặc gọi setLayoutAnimation().

Nếu bạn chỉ muốn làm cho chế độ xem của bạn trông giống như đang di chuyển, bạn cần TranslateAnimation; xem câu trả lời này: https://stackoverflow.com/a/4214490/832776 Ngoài ra nếu bạn muốn lặp lại hoạt ảnh, sau đó gọi setAnimationListener() và trong onAnimationEnd() chỉ cần khởi động lại hoạt ảnh.

Nếu bạn đang cố gắng để di chuyển các điểm vĩnh viễn, thấy điều này: http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400

1

Tôi biết rằng bạn đã chấp nhận câu trả lời. Nhưng tôi nghĩ câu trả lời này sẽ hữu ích cho ai đó đọc điều này. Bạn có thể thử xóa .xml từ, pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));

Các vấn đề liên quan