2012-09-08 7 views
0

페이드 인 및 아웃 애니메이션으로 이미지를 전환하고 싶습니다. 를 표시두 이미지 사이의 애니메이션 페이드 아웃

package android.infra; 

import android.graphics.drawable.Drawable; 
import android.view.View; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.AlphaAnimation; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationSet; 
import android.view.animation.DecelerateInterpolator; 
import android.widget.ImageView; 

public class AnimationsHelper { 

    public static void switchImageAnimations(final ImageView imageView, final Drawable 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.setImageDrawable(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) { 
         switchImageAnimations(imageView, images, imageIndex + 1,forever); //Calls itself until it gets to the end of the array 
        } 
        else { 
         if (forever == true){ 
          switchImageAnimations(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 
       } 
      }); 
     } 
} 

그리고 다운로드 이미지에 대해이 코드 :

package android.infra; 

import java.util.Map; 
import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.Collections; 
import java.util.WeakHashMap; 
import java.lang.ref.SoftReference; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ExecutorService; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.widget.ImageView; 
import android.os.Handler; 
import android.os.Message; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.io.IOException; 
import java.net.URL; 
import java.net.URLConnection; 

public class DrawableBackgroundDownloader {  

private final Map<String, SoftReference<Drawable>> mCache = new HashMap<String, SoftReference<Drawable>>(); 
private final LinkedList <Drawable> mChacheController = new LinkedList <Drawable>(); 
private ExecutorService mThreadPool; 
private final Map<ImageView, String> mImageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>()); 

public static int MAX_CACHE_SIZE = 80; 
public int THREAD_POOL_SIZE = 3; 

/** 
* Constructor 
*/ 
public DrawableBackgroundDownloader() { 
    mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE); 
} 


/** 
* Clears all instance data and stops running threads 
*/ 
public void Reset() { 
    ExecutorService oldThreadPool = mThreadPool; 
    mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE); 
    oldThreadPool.shutdownNow(); 

    mChacheController.clear(); 
    mCache.clear(); 
    mImageViews.clear(); 
} 

public void loadDrawable(final String url, final ImageView imageView,Drawable placeholder) { 
    mImageViews.put(imageView, url); 
    Drawable drawable = getDrawableFromCache(url); 

    // check in UI thread, so no concurrency issues 
    if (drawable != null) { 
     //Log.d(null, "Item loaded from mCache: " + url); 
     // imageView.setImageDrawable(drawable); 
     Drawable imagesToShow[] = { drawable }; 
     android.infra.AnimationsHelper.switchImageAnimations(imageView, imagesToShow, 0, false); 
    } else { 
     imageView.setImageDrawable(placeholder); 
     queueJob(url, imageView, placeholder); 
    } 
} 


private Drawable getDrawableFromCache(String url) { 
    if (mCache.containsKey(url)) { 
     return mCache.get(url).get(); 
    } 

    return null; 
} 

private synchronized void putDrawableInCache(String url,Drawable drawable) { 
    int chacheControllerSize = mChacheController.size(); 
    if (chacheControllerSize > MAX_CACHE_SIZE) 
     mChacheController.subList(0, MAX_CACHE_SIZE/2).clear(); 

    mChacheController.addLast(drawable); 
    mCache.put(url, new SoftReference<Drawable>(drawable)); 

} 

private void queueJob(final String url, final ImageView imageView,final Drawable placeholder) { 
    /* Create handler in UI thread. */ 
    final Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      String tag = mImageViews.get(imageView); 
      if (tag != null && tag.equals(url)) { 
       if (imageView.isShown()) 
        if (msg.obj != null) { 
        // imageView.setImageDrawable((Drawable) msg.obj); 
         Drawable imagesToShow[] = { (Drawable) msg.obj }; 
         android.infra.AnimationsHelper.switchImageAnimations(imageView, imagesToShow, 0, false); 
        } else { 
         imageView.setImageDrawable(placeholder); 
         //Log.d(null, "fail " + url); 
        } 
      } 
     } 
    }; 

    mThreadPool.submit(new Runnable() { 
     @Override 
     public void run() { 
      final Drawable bmp = downloadDrawable(url); 
      // if the view is not visible anymore, the image will be ready for next time in cache 
      if (imageView.isShown()) 
      { 
       Message message = Message.obtain(); 
       message.obj = bmp; 
       //Log.d(null, "Item downloaded: " + url); 

       handler.sendMessage(message); 
      } 
     } 
    }); 
} 



private Drawable downloadDrawable(String url) { 
    try { 
     InputStream is = getInputStream(url);   
     Drawable drawable = Drawable.createFromStream(is, url); 
     putDrawableInCache(url,drawable); 
     return drawable; 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 


private InputStream getInputStream(String urlString) throws MalformedURLException, IOException { 
    URL url = new URL(urlString); 
    URLConnection connection; 
    connection = url.openConnection(); 
    connection.setUseCaches(true); 
    connection.connect(); 
    InputStream response = connection.getInputStream(); 

    return response; 
} 
} 

당신은 내가 그것을 사용하기 위해 노력하고있어 보는 바와 같이 나는 애니메이션이 코드를 사용하고

내 두 번째 코드에 의해 :

Drawable imagesToShow[] = { (Drawable) msg.obj }; 
android.infra.AnimationsHelper.switchImageAnimations(imageView, imagesToShow, 0, false); 

어떤 이유로, 내 이미지가로드 된 (애니메이션 없음) 및 바로 후미 애니메이션이 실행되고 어떤 이유로 든 내 이미지 뷰가 보이지 않게됩니다!

미리 감사드립니다.

답변

-2

페이드 인 애니메이션 만 사용하여 해결했습니다.

+10

솔루션 예를 게시하는 것이 좋습니다. 다른 사람들에게 유용 할 수 있습니다. – cwc

관련 문제