2013-03-21 37 views
18

Tôi có 2 hoạt ảnh đang hoạt động, tôi muốn làm mờ chuyến tàu của mình + tween tàu của tôi cùng một lúc. Nếu tôi thực thi 1 trong các dòng này, nó hoạt động. Nhưng nếu tôi cố gắng thực hiện cả hai, chỉ 1 sẽ hoạt động .. Tôi thực sự không thể tìm thấy giải pháp ở đây.Nhiều hoạt ảnh trên 1 imageview android

Có thể bạn có thể trợ giúp?

final ImageView mytrain = (ImageView) findViewById(R.id.train); 
    final Animation traintween = AnimationUtils.loadAnimation(this,R.anim.treinanimation); 
    final Animation trainfade = AnimationUtils.loadAnimation(this,R.anim.trainfade); 


    mytrain.startAnimation(trainfade); 
mytrain.startAnimation(trainntween); 

Tôi muốn mytrain để thực hiện cả hình ảnh động ..

Cảm ơn bạn đã giúp đỡ!

Trả lời

55

Sử dụng các lớp AnimationSet:

AnimationSet s = new AnimationSet(false);//false means don't share interpolators 
s.addAnimation(traintween); 
s.addAnimation(trainfad); 
mytrain.startAnimation(s); 
+0

cảm ơn người đàn ông !! – RobinHo

+0

bạn được chào đón! :) –

+0

Không có phương thức 'addAnimation()' trong 'AnimationSet' –

3

Bạn cần sử dụng AnimationSet, hãy xem docs. Chỉ cần gọi addAnimation() cho mỗi Hoạt ảnh bạn muốn phát.

+1

addAnimation (trainfade); addAnimation (traintween); công cộng void addAnimation (Animation a) { mytrain.startanimation (a); } Có phải như thế này không? – RobinHo

0

đây là ví dụ của tất cả các hình ảnh động trong một file xml đơn ...

này sẽ giúp bạn nhưng trước tiên bạn nên đọc các tài liệu của AnimationSet

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 

<scale android:fromXScale="1.0" android:toXScale="3.0" 
    android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%" 
    android:pivotY="50%" android:duration="5000" /> 
<set> 
    <alpha xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fromAlpha="0.2" android:toAlpha="1.0"  android:duration="3000" /> 
    <rotate android:fromDegrees="0" android:toDegrees="-360" 
     android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" 
     android:startOffset="700" android:duration="4000" /> 
<!--  <translate android:fromXDelta="0%" android:toXDelta="0%" --> 
<!--   android:fromYDelta="0%" android:toYDelta="100%"  android:duration="3000" --> 
</set> 

</set> 
0

bạn cũng có thể sử dụng ImageSwitcher, tôi nghĩ điều này là tốt hơn thì AnimationSet trong trường hợp của bạn

3

có thể được thực hiện programatically sử dụng AnimatorSet lớp của android:

final AnimatorSet mAnimatorSet = new AnimatorSet(); 
    mAnimatorSet.playTogether(
       ObjectAnimator.ofFloat(img_view,"scaleX",1,0.9f,0.9f,1.1f,1.1f,1), 
       ObjectAnimator.ofFloat(img_view,"scaleY",1,0.9f,0.9f,1.1f,1.1f,1), 
       ObjectAnimator.ofFloat(img_view,"rotation",0 ,-3 , -3, 3, -3, 3, -3,3,-3,3,-3,0) 
     ); 

//define any animation you want,like above 

mAnimatorSet.setDuration(2000); //set duration for animations 
    mAnimatorSet.start(); 

ví dụ này sẽ bắt đầu tất cả 3 hình ảnh động trên quan điểm mục tiêu (imgView) tại cùng một thời gian, bạn cũng có thể sử dụng playSequentially .....

Để có ví dụ hoàn chỉnh check this out..

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