2014-07-24 14 views
10

Napisałem kod, który może zmieniać tło obrazu losowo co 5 sekund.nowię chcę korzystać z funkcji zmniejszania/zmniejszania animacji, aby zmienić obraz tła, ale nie wiem, jak mogę użyć ta animacja.Android zmienia obraz tła z zanikaniem animacji

To jest moje źródło:

void handlechange() { 

    Handler hand = new Handler(); 
    hand.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

      // change image here 
      change(); 

     } 

     private void change() { 
      // TODO Auto-generated method stub 

      Random rand = new Random(); 

      int index = rand.nextInt(image_rundow.length); 

      mapimg.setBackgroundResource(image_rundow[index]); 

      handlechange(); 
     } 
    }, 4000); 

} 

W tej chwili wszystko jest OK. Mogę zmienić losowy obraz tła, ale nie wiem, jak używać animacji do zanikania/zmniejszania animacji.

Jeśli ktoś zna rozwiązanie, pomóż mi, dzięki.

+0

spójrz na to: http://stackoverflow.com/questions/2614545/animate-change-of-view-background-color-in-android – Fenix

Odpowiedz

16

Musisz zadzwonić pod te kody.

Po pierwsze, musisz zrobić dwa pliki xml, aby zaniknąć w & z animacją w ten sposób.

fade_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<alpha 
     android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:fillAfter="true" 
     android:duration="2000" 
     /> 
</set> 

fade_out.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<alpha 
     android:fromAlpha="1.0" 
     android:toAlpha="0.0" 
     android:fillAfter="true" 
     android:duration="2000" 
     /> 
</set> 

Po drugie, trzeba uruchomić animację ImageView jak poniżej, a także trzeba ustawić AnimationListener zmienić fadeout gdy fadein wykończenie.

Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in); 
imageView.startAnimation(fadeIn); 

fadeIn.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out); 
      imageView.startAnimation(fadeOut); 
     } 
     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 
}); 
+2

pracował jak urok. zastosowane zarówno zanikanie jak i zanikanie do mojego widoku LinearLayout. Musiał zmienić się na 500 ms. Rok 2000 jest zdecydowanie za długi. – ApolloSoftware

+4

ma to być imageView.startAnimation (fadeIn); –

4

Dla fade in animacji, należy utworzyć nowy folder o nazwie 'anim' w folderze res i wewnątrz niego, tworzyć '' z fade_in.xml następujący kod

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

    <alpha 
     android:duration="@android:integer/config_mediumAnimTime" 
     android:fromAlpha="0.0" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     android:toAlpha="1.0" /> 

</set> 

teraz uruchomić animację na ImageView, użytkowania następujący kod w swojej działalności

Animation anim = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in); 
imageView.setAnimation(anim); 
anim.start(); 

dla animacji Fadeout, po prostu zamienić wartości android: fromAlpha i Android: toAlpha

nadzieję, że to pomaga.

1

Możesz użyć relativeLayout i dodać jedną warstwę widoku tła, która ustawia wysokość i szerokość match_parent. Cały inny element UI powinien umieścić na tym widoku. W twoim kodzie. Zdefiniuj animację fadeOut i fadeIn. Znajdź ten widok tła według id, a następnie wykonaj to:

xxxBackground.startAnimation(fadeOut); 
xxxBackground.setBackgroundResource(R.drawable.your_random_pic); 
xxxBackground.startAnimation(fadeIn); 

Możesz użyć niektórych interpolatorów, aby dostroić wydajność.

1

Potrzebujesz AnimationDrawable z animacją.

Pierwszy krok AnimationDrawable

-Tworzenie plik /res/anim/anim_android.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"> 
    <item android:drawable="@drawable/android_1" 
      android:duration="100"/> 
    <item android:drawable="@drawable/android_2" 
      android:duration="100"/> 
    <item android:drawable="@drawable/android_3" 
      android:duration="100"/> 
    <item android:drawable="@drawable/android_4" 
      android:duration="100"/> 
    <item android:drawable="@drawable/android_5" 
      android:duration="100"/> 
    <item android:drawable="@drawable/android_6" 
      android:duration="100"/> 
    <item android:drawable="@drawable/android_7" 
      android:duration="100"/> 
</animation-list> 

-Dodaj ImageView z android: src = "@ anim/anim_android".

<ImageView 
    android:id="@+id/myanimation" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@anim/anim_android" /> 

Drugi etap

-W swojej działalności i tworzenie AnimationDrawable Animacja

AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); 
      animationDrawable.setOneShot(true); 
      animationDrawable.start(); 

    Animation animation = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_in); 

    imageView.setAnimation(animation); 
     animation.start(); 
     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
      } 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_out); 
       imageView.startAnimation(fadeOut); 
      } 
      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 
}); 

nie trzeba Handler.