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

ご覧のとおり、2 番目のコードで次のように使用しようとしています。

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

なんらかの理由で私のイメージが(アニメーションなしで)ロードされ、その直後にアニメーションが実行され、何らかの理由で私のイメージビューが見えなくなりました!

前もって感謝します。

4

1 に答える 1

-2

フェードインアニメーションのみを使用して解決しました。

于 2012-09-08T10:51:49.563 に答える