2016-10-01 3 views
0

Ich muss 2 Ansichten animieren und ich möchte die Animation zusammen starten. Hier sind meine zwei Animationen:Animation in verschiedenen Ansichten zusammen

ScaleAnimation scaleAnimation1 = new ScaleAnimation(image.getScaleX(), 1.0f, image.getScaleY(), 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
scaleAnimation.setDuration(300); 
scaleAnimation.setFillAfter(true); 
scaleAnimation.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}); 
image.startAnimation(scaleAnimation); 

ScaleAnimation scaleAnimation2 = new ScaleAnimation(logo.getScaleX(), 1.0f, logo.getScaleY(), 1.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
scaleAnimation.setDuration(300); 
scaleAnimation.setFillAfter(true); 
scaleAnimation.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}); 
logo.startAnimation(scaleAnimation); 

Wie kann ich es tun? Ich muss es programmatisch machen.

P.S. Ich habe nicht viel Erfahrung in der Animation.

+0

Schauen Sie sich diese Beispiel: http://cogitolearning.co.uk/?p=1194 –

+0

@ItzikSamara, das zum Ausführen von zwei Animationen sim ist auf einer einzigen Ansicht. Ich möchte 2 verschiedene Animationen auf 2 verschiedenen Ansichten gleichzeitig starten. –

+0

OK, schau dir dieses Beispiel an: http://Stackoverflow.com/questions/17926117/objectanimator-with-scale-property-makes-bg-black sein ObjectAnimator .. ein bisschen anders, aber ich benutze es funktioniert super. –

Antwort

0

Hier ist für Sie Probe)

public class AnimationUtils { 
    public static void applyAnimation(Context context, View view, int animationResource, int animationOffsetMilisec){ 
     Animation anim = android.view.animation.AnimationUtils.loadAnimation(context, animationResource); 
     anim.setInterpolator(new AccelerateDecelerateInterpolator()); 
     anim.setStartOffset(animationOffsetMilisec); 
     if(view != null) { 
      view.setAnimation(anim); 
      view.startAnimation(anim); 
     } 
    } 
} 

animation_tap.xml in res/anim sein muss/

<?xml version="1.0" encoding="utf-8"?> 
<set 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"> 
    <scale 
     android:duration="100" 
     android:fromXScale="1.0" 
     android:fromYScale="1.0" 
     android:toXScale="1.00" 
     android:toYScale="0.95" 
     android:pivotX="50%" 
     android:pivotY="50%"/> 
    <scale 
     android:duration="200" 
     android:fromXScale="1.0" 
     android:fromYScale="0.95" 
     android:toXScale="1.0" 
     android:toYScale="1.1" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:startOffset="100"/> 
</set> 

und was jetzt im Code können Sie verwenden:

AnimationUtils.applyAnimation(context,view1,R.anim.animation_tap,0); 
AnimationUtils.applyAnimation(context,view2,R.anim.animation_tap,0); 
AnimationUtils.applyAnimation(context,view3,R.anim.animation_tap,0); 
//... 
Verwandte Themen