2013-07-11 9 views
9

ich eine Tätigkeit zu tun habe, wo ich die Image am Wechsel regelmäßig, für die ich die folgende Codezeile geschrieben.Wie reibungslosen Übergang von einem Bild zum anderen

imageview.setImageUri(resId); 

Ich Erhöhung der Ressource-ID .Es funktioniert gut, aber es gibt plötzliche Übergang von einem Bild zum anderen. Ich will das nicht, ich möchte den reibungslosen Übergang der Bildansicht zu einem anderen Bild. Wie kann ich das machen?

Antwort

15

Versuchen Sie, diese

ImageView demoImage = (ImageView) findViewById(R.id.DemoImage); 
int imagesToShow[] = { R.drawable.image1, R.drawable.image2,R.drawable.image3 }; 

animate(demoImage, imagesToShow, 0,false); 



    private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) { 

    //imageView <-- The View which displays the images 
    //images[] <-- Holds R references to the images to display 
    //imageIndex <-- index of the first image to show in images[] 
    //forever <-- If equals true then after the last image it starts all over again with the first image resulting in an infinite loop. You have been warned. 

    int fadeInDuration = 500; // Configure time values here 
    int timeBetween = 3000; 
    int fadeOutDuration = 1000; 

    imageView.setVisibility(View.INVISIBLE); //Visible or invisible by default - this will apply when the animation ends 
    imageView.setImageResource(images[imageIndex]); 

    Animation fadeIn = new AlphaAnimation(0, 1); 
    fadeIn.setInterpolator(new DecelerateInterpolator()); // add this 
    fadeIn.setDuration(fadeInDuration); 

    Animation fadeOut = new AlphaAnimation(1, 0); 
    fadeOut.setInterpolator(new AccelerateInterpolator()); // and this 
    fadeOut.setStartOffset(fadeInDuration + timeBetween); 
    fadeOut.setDuration(fadeOutDuration); 

    AnimationSet animation = new AnimationSet(false); // change to false 
    animation.addAnimation(fadeIn); 
    animation.addAnimation(fadeOut); 
    animation.setRepeatCount(1); 
    imageView.setAnimation(animation); 

    animation.setAnimationListener(new AnimationListener() { 
     public void onAnimationEnd(Animation animation) { 
      if (images.length - 1 > imageIndex) { 
       animate(imageView, images, imageIndex + 1,forever); //Calls itself until it gets to the end of the array 
      } 
      else { 
       if (forever == true){ 
       animate(imageView, images, 0,forever); //Calls itself to start the animation all over again in a loop if forever = true 
       } 
      } 
     } 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 
     } 
     public void onAnimationStart(Animation animation) { 
      // TODO Auto-generated method stub 
     } 
    }); 
} 
+0

Danke für schnelle Antwort. ich habe dich gewählt. ich arbeite an Android 2.3, wo als animation in android 3 –

+0

hinzugefügt wurde Ich weiß nicht, ob ich zu spät komme, aber ich war auf der Suche nach einer Antwort wie diese und es war super. Jedenfalls habe ich einen anderen Weg gefunden, den Effekt erneut zu starten. Sie können 'animieren (imageView, images, imageIndex% images.length + 1, für immer);' so dass die Indizes immer innerhalb der Grenzen liegen. –

1

Für einen reibungslosen Übergang Sie Animationen in Android, starten durch das Lesen Sie den folgenden Link verwenden müssen: http://www.vogella.com/articles/AndroidAnimation/article.html

Es gibt viele ähnliche Fragen auf Stackoverflow über Animationen und viele Tutorials sind im Netz verfügbar zu diesem Thema. Eine einfache Suche auf Google bringt Ihnen Tonnen von Ergebnis

+0

i auf Android 2.3 arbeite, wo als Animation in Android 3 –

2

Versuchen Sie Alpha-Animation. Zuerst blenden Sie die Bildansicht aus, ändern Sie am Ende der Animation die Ressource und blenden Sie dann die Bildansicht ein.

+1

hinzugefügt wurde ich auf Android 2.3 arbeite, wo als Animation in Android 3 –

+0

Alpha Animation hinzugefügt wurde von API verfügbar ist Level 1. http://developer.android.com/reference/android/view/animation/AlphaAnimation.html –

Verwandte Themen