2014-07-24 12 views
10

Ich schrieb Code, der Hintergrundbild alle fünf Sekunden zufällig ändern kann. Jetzt möchte ich Fade in/out-Animation verwenden, um Hintergrundbild zu ändern, aber ich weiß nicht, wie ich verwenden kann diese Animation.Android Hintergrundbild mit Fade-In/Out-Animation ändern

Dies ist eine meiner Quelle:

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); 

} 

Im Moment alles in Ordnung ist. Ich kann das Hintergrundbild zufällig ändern, aber ich weiß nicht, wie ich Animation ein-/ausblenden kann.

Wenn jemand Lösung weiß, bitte helfen Sie mir, danke.

+0

werfen Sie einen Blick auf diese: http://stackoverflow.com/questions/2614545/animate-change-of-view-background-color-in-android – Fenix

Antwort

16

Sie müssen diese Codes aufrufen.

Zuerst müssen Sie zwei XML-Dateien für die Einblendung in & machen, wie diese Animation.

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> 

Zweitens haben Sie Animation von Imageview laufen wie unten und auch Sie haben AnimationListener einstellen fadeout zu ändern, wenn Finish fadein.

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

arbeitete wie ein Charme. angewendet sowohl Ein- als auch Ausblenden in meiner LinearLayout-Ansicht. Musste auf 500 ms wechseln. 2000 ist viel zu lang. – ApolloSoftware

+4

soll es imageView.startAnimation (fadeIn) sein; –

4

Für verblassen in der Animation, neue Ordner mit dem Namen ‚Anim‘ in Ihrem res Ordner und darin, erstellen ‚fade_in.xml‘ mit dem Code folgenden

<?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> 

nun diese Animation auf Ihrem Imageview, Gebrauch zu laufen folgenden Code in Ihrer Tätigkeit

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

für fadeout Animation, tauschen nur Werte von android: fromAlpha und android: toAlpha

Hoffnung, das hilft.

1

Sie können relativeLayout verwenden und eine Ebene der Hintergrundansicht hinzufügen, die Höhe und Breite match_parent festlegt. Alle anderen Elemente der Benutzeroberfläche sollten diese Ansicht überlagern. In deinem Code. Definieren Sie fadeOut und fadeIn Animation. Suchen Sie diese Hintergrundansicht nach ID, und führen Sie dann folgendes aus:

Sie können einen Interpolator verwenden, um die Leistung zu optimieren.

1

Sie benötigen AnimationDrawable mit Animation.

Erster Schritt AnimationDrawable

-Create eine Datei /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> 

-Add ein Image mit 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" /> 

Zweiter Schritt

-In Ihre Aktivität erstellen AnimationDrawable and Animation

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) { 
      } 
}); 

Sie nicht Handler brauchen.

Verwandte Themen