2017-06-12 77 views
1

我已經使用imageView在屏幕上隨機顯示5個幾乎相同的圖像。事情是關閉每個圖像,並立即顯示下一個。因此,查看器可以很容易地找到該圖像正在改變。是否有可能將動畫效果添加到imageView? (比如,一個影像慢慢淡出,在此同時另一個圖像淡入)對android中的imageView動畫效果

+0

https://stackoverflow.com/a/6822116/5505680存在漸現將其合併成具有所需的溶液 – matheszabi

回答

0

U可以修改烏爾自我

ImageView img_view= (ImageView) findViewById(R.id.DemoImage); 

int images[] = { R.drawable.image1, R.drawable.image2,R.drawable.image3 }; 

animate(img_view, images, 0,false); 

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

    int fadeInDuration = 1000; 
    int timeBetween = 5000; 
    int fadeOutDuration = 1000; 

    imageView.setVisibility(View.INVISIBLE);  
    imageView.setImageResource(images[imageIndex]); 

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

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

    AnimationSet animation = new AnimationSet(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); 
      } 
      else { 
       if (forever){ 
       animate(imageView, images, 0,forever); 
       } 
      } 
     } 

    }); 
}