android universalimageloader 几点改进
protected void initImageCache() {/** * 初始化图片加载 */Logger.d(TAG, "Initializing image loader.");File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "myApplication/Cache");ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(getApplicationContext());builder.threadPoolSize(3); // 设置线程数量为3builder.threadPriority(Thread.NORM_PRIORITY - 1); // 设定线程等级比普通低一点builder.memoryCacheExtraOptions(200, 200); // 设定缓存在内存的图片大小最大为200x200builder.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024));builder.discCache(new UnlimitedDiscCache(cacheDir)) ;builder.discCacheExtraOptions(displayMetrics.widthPixels, displayMetrics.heightPixels, CompressFormat.JPEG, 80, null);builder.denyCacheImageMultipleSizesInMemory(); // 拒绝缓存同一图片,有不同的大小builder.discCacheFileNameGenerator(new Md5FileNameGenerator());builder.imageDownloader(new MyImageLoader(getApplicationContext(),restClient));//new BaseImageDownloader(getApplicationContext()));//builder.imageDecoder(new MyImageDecoder(true));builder.enableLogging(); // 开启调试// 设置默认显示情况DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder();displayImageOptionsBuilder.showImageForEmptyUri(R.drawable.house_icon_photo4); // 空uri的情况displayImageOptionsBuilder.cacheInMemory(true); // 缓存在内存displayImageOptionsBuilder.cacheOnDisc(true); // 缓存在磁盘displayImageOptionsBuilder.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2);builder.defaultDisplayImageOptions(displayImageOptionsBuilder.build());ImageLoader.getInstance().init(builder.build());Logger.d(TAG, "Initialize image loader finished.");}
?(2)MyImageDecoder.java 扩展自?BaseImageDecoder
import java.io.IOException;import java.io.InputStream;import android.graphics.Bitmap;import android.graphics.BitmapFactory.Options;import myapp.sdk.io.CloseShieldInputStream;import com.nostra13.universalimageloader.core.assist.ImageSize;import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;import com.nostra13.universalimageloader.core.decode.ImageDecodingInfo;import com.nostra13.universalimageloader.utils.L;public class MyImageDecoder extends BaseImageDecoder{public MyImageDecoder(boolean loggingEnabled) {this.loggingEnabled = loggingEnabled;}@Overridepublic Bitmap decode(ImageDecodingInfo decodingInfo) throws IOException {InputStream imageStream = getImageStream(decodingInfo);// inputStream markimageStream.mark(imageStream.available()+1); InputStream imageStreamClone = new CloseShieldInputStream(imageStream);ImageFileInfo imageInfo = defineImageSizeAndRotation(imageStreamClone, decodingInfo.getImageUri());Options decodingOptions = prepareDecodingOptions(imageInfo.imageSize, decodingInfo);// imageStream = getImageStream(decodingInfo);//michael remove double load from server// inputStream resetimageStream.reset();Bitmap decodedBitmap = decodeStream(imageStream, decodingOptions);if (decodedBitmap == null) {L.e(ERROR_CANT_DECODE_IMAGE, decodingInfo.getImageKey());} else {decodedBitmap = considerExactScaleAndOrientaiton(decodedBitmap, decodingInfo, imageInfo.exif.rotation, imageInfo.exif.flipHorizontal);}return decodedBitmap;}}
?(3)CloseShieldInputStream.java 可以直接使用 apache common-io,但在 android 上为了克隆 InputStream 一点点功能,而引入整个 IO 包,100多KB,似乎不划算,所以单独复制了 IO 包中的几个文件:
import java.io.InputStream;/*** * Proxy stream that prevents the underlying input stream from being closed. * <p> * This class is typically used in cases where an input stream needs to be * passed to a component that wants to explicitly close the stream even if * more input would still be available to other components. * * @version $Id: CloseShieldInputStream.java 587913 2007-10-24 15:47:30Z niallp $ * @since Commons IO 1.4 */public class CloseShieldInputStream extends ProxyInputStream { /*** * Creates a proxy that shields the given input stream from being * closed. * * @param in underlying input stream */ public CloseShieldInputStream(InputStream in) { super(in); } /*** * Replaces the underlying input stream with a {@link ClosedInputStream} * sentinel. The original input stream will remain open, but this proxy * will appear closed. */ public void close() { in = new ClosedInputStream(); }}
?
import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;/*** * A Proxy stream which acts as expected, that is it passes the method * calls on to the proxied stream and doesn't change which methods are * being called. * <p> * It is an alternative base class to FilterInputStream * to increase reusability, because FilterInputStream changes the * methods being called, such as read(byte[]) to read(byte[], int, int). * * @author Stephen Colebourne * @version $Id: ProxyInputStream.java 610010 2008-01-08 14:50:59Z niallp $ */public abstract class ProxyInputStream extends FilterInputStream { /*** * Constructs a new ProxyInputStream. * * @param proxy the InputStream to delegate to */ public ProxyInputStream(InputStream proxy) { super(proxy); // the proxy is stored in a protected superclass variable named 'in' } /*** * Invokes the delegate's <code>read()</code> method. * @return the byte read or -1 if the end of stream * @throws IOException if an I/O error occurs */ public int read() throws IOException { return in.read(); } /*** * Invokes the delegate's <code>read(byte[])</code> method. * @param bts the buffer to read the bytes into * @return the number of bytes read or -1 if the end of stream * @throws IOException if an I/O error occurs */ public int read(byte[] bts) throws IOException { return in.read(bts); } /*** * Invokes the delegate's <code>read(byte[], int, int)</code> method. * @param bts the buffer to read the bytes into * @param st The start offset * @param end The number of bytes to read * @return the number of bytes read or -1 if the end of stream * @throws IOException if an I/O error occurs */ public int read(byte[] bts, int st, int end) throws IOException { return in.read(bts, st, end); } /*** * Invokes the delegate's <code>skip(long)</code> method. * @param ln the number of bytes to skip * @return the number of bytes to skipped or -1 if the end of stream * @throws IOException if an I/O error occurs */ public long skip(long ln) throws IOException { return in.skip(ln); } /*** * Invokes the delegate's <code>available()</code> method. * @return the number of available bytes * @throws IOException if an I/O error occurs */ public int available() throws IOException { return in.available(); } /*** * Invokes the delegate's <code>close()</code> method. * @throws IOException if an I/O error occurs */ public void close() throws IOException { in.close(); } /*** * Invokes the delegate's <code>mark(int)</code> method. * @param idx read ahead limit */ public synchronized void mark(int idx) { in.mark(idx); } /*** * Invokes the delegate's <code>reset()</code> method. * @throws IOException if an I/O error occurs */ public synchronized void reset() throws IOException { in.reset(); } /*** * Invokes the delegate's <code>markSupported()</code> method. * @return true if mark is supported, otherwise false */ public boolean markSupported() { return in.markSupported(); }}
?
import java.io.InputStream;/*** * Closed input stream. This stream returns -1 to all attempts to read * something from the stream. * <p> * Typically uses of this class include testing for corner cases in methods * that accept input streams and acting as a sentinel value instead of a * <code>null</code> input stream. * * @version $Id: ClosedInputStream.java 601751 2007-12-06 14:55:45Z niallp $ * @since Commons IO 1.4 */public class ClosedInputStream extends InputStream { /*** * A singleton. */ public static final ClosedInputStream CLOSED_INPUT_STREAM = new ClosedInputStream(); /*** * Returns -1 to indicate that the stream is closed. * * @return always -1 */ public int read() { return -1; }}
?
(4)ImageLoader 会多次反复发送加载请求,对网络也是个灾难,在扩展的 MyImageLoader 中修改网络连接超时和读取网络超时的时间值
import android.content.Context;import com.nostra13.universalimageloader.core.download.BaseImageDownloader;public class MyImageLoader extends BaseImageDownloader {public MyImageLoader(Context context) {super(context,10000,60000);}}
?