建造者模式的简写方式分析

  我们知道建造者模式一般由Product、Builder、Director三个模块组成。但是在一般的开发中,Director角色经常被忽略。而直接使用一个Builder来进行对象的组装,这个Builder通常是链式调用,它的关键点是每个setter方法都返回自身,也就是return this,这样就使得setter方法可以链式调用,代码大致如下:

new TestBuilder().setA("A").setB("B").create();

通过这种形式,不紧去除了Director角色,整个结构也更加简单,也能对Product对象的组装过程有更精细的控制。

例子:

/**
 * Created by niehongtao on 16/7/4.
 */
public class Product {
    public String partA;
    public String partB;

    // 设置成私有的,强制要求只能从通过Bulider来建造这个对象,不能new
    private Product(Builder builder) {
        this.partA = builder.partA;
        this.partB = builder.partB;
    }


    public static class Builder {
        public String partA;
        public String partB;


        public Product.Builder buildPartA(String partA) {
            this.partA = partA;
            return this;
        }


        public Product.Builder buildPartB(String partB) {
            this.partB = partB;
            return this;
        }


        public Product build() {
            return new Product(this);
        }

    }


}

Client

/**
 * Created by niehongtao on 16/7/4.
 */
public class Client {
    public static void main(String[] args) {
        Product product = new Product.Builder().buildPartA("aaaa")
                .buildPartB("bbbb")
                .build();
        System.out.print(product.partA + "::" + product.partB);
    }
}

输出结果:

aaaa::bbbb

应用场景:

AlertDialog

    /**
     * 展示一个对话框,带确定和取消2个按钮(确定 取消)
     * 
     * @param context
     *            上下文,用来初始化Builder
     * @param message
     *            要在对话框上展示的内容
     */
    public static void showOkCancelAlertDialog(Context context, String message,String ok,String cancel) {
        AlertDialog.Builder builder = new Builder(context);
        builder.setTitle("提示:");
        builder.setMessage("" + message);
        builder.setPositiveButton(ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        builder.setNegativeButton(cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }

universal imageloader的ImageLoaderConfiguration

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.nostra13.universalimageloader.core;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.MemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.FuzzyKeyMemoryCache;
import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FlushedInputStream;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.ImageDecoder;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;
import com.nostra13.universalimageloader.core.process.BitmapProcessor;
import com.nostra13.universalimageloader.utils.L;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;

public final class ImageLoaderConfiguration {
    final Resources resources;
    final int maxImageWidthForMemoryCache;
    final int maxImageHeightForMemoryCache;
    final int maxImageWidthForDiskCache;
    final int maxImageHeightForDiskCache;
    final BitmapProcessor processorForDiskCache;
    final Executor taskExecutor;
    final Executor taskExecutorForCachedImages;
    final boolean customExecutor;
    final boolean customExecutorForCachedImages;
    final int threadPoolSize;
    final int threadPriority;
    final QueueProcessingType tasksProcessingType;
    final MemoryCache memoryCache;
    final DiskCache diskCache;
    final ImageDownloader downloader;
    final ImageDecoder decoder;
    final DisplayImageOptions defaultDisplayImageOptions;
    final ImageDownloader networkDeniedDownloader;
    final ImageDownloader slowNetworkDownloader;

    private ImageLoaderConfiguration(ImageLoaderConfiguration.Builder builder) {
        this.resources = builder.context.getResources();
        this.maxImageWidthForMemoryCache = builder.maxImageWidthForMemoryCache;
        this.maxImageHeightForMemoryCache = builder.maxImageHeightForMemoryCache;
        this.maxImageWidthForDiskCache = builder.maxImageWidthForDiskCache;
        this.maxImageHeightForDiskCache = builder.maxImageHeightForDiskCache;
        this.processorForDiskCache = builder.processorForDiskCache;
        this.taskExecutor = builder.taskExecutor;
        this.taskExecutorForCachedImages = builder.taskExecutorForCachedImages;
        this.threadPoolSize = builder.threadPoolSize;
        this.threadPriority = builder.threadPriority;
        this.tasksProcessingType = builder.tasksProcessingType;
        this.diskCache = builder.diskCache;
        this.memoryCache = builder.memoryCache;
        this.defaultDisplayImageOptions = builder.defaultDisplayImageOptions;
        this.downloader = builder.downloader;
        this.decoder = builder.decoder;
        this.customExecutor = builder.customExecutor;
        this.customExecutorForCachedImages = builder.customExecutorForCachedImages;
        this.networkDeniedDownloader = new ImageLoaderConfiguration.NetworkDeniedImageDownloader(this.downloader);
        this.slowNetworkDownloader = new ImageLoaderConfiguration.SlowNetworkImageDownloader(this.downloader);
        L.writeDebugLogs(builder.writeLogs);
    }

    public static ImageLoaderConfiguration createDefault(Context context) {
        return (new ImageLoaderConfiguration.Builder(context)).build();
    }

    ImageSize getMaxImageSize() {
        DisplayMetrics displayMetrics = this.resources.getDisplayMetrics();
        int width = this.maxImageWidthForMemoryCache;
        if(width <= 0) {
            width = displayMetrics.widthPixels;
        }

        int height = this.maxImageHeightForMemoryCache;
        if(height <= 0) {
            height = displayMetrics.heightPixels;
        }

        return new ImageSize(width, height);
    }

    private static class SlowNetworkImageDownloader implements ImageDownloader {
        private final ImageDownloader wrappedDownloader;

        public SlowNetworkImageDownloader(ImageDownloader wrappedDownloader) {
            this.wrappedDownloader = wrappedDownloader;
        }

        public InputStream getStream(String imageUri, Object extra) throws IOException {
            InputStream imageStream = this.wrappedDownloader.getStream(imageUri, extra);
            switch(ImageLoaderConfiguration.SyntheticClass_1.$SwitchMap$com$nostra13$universalimageloader$core$download$ImageDownloader$Scheme[Scheme.ofUri(imageUri).ordinal()]) {
            case 1:
            case 2:
                return new FlushedInputStream(imageStream);
            default:
                return imageStream;
            }
        }
    }

    private static class NetworkDeniedImageDownloader implements ImageDownloader {
        private final ImageDownloader wrappedDownloader;

        public NetworkDeniedImageDownloader(ImageDownloader wrappedDownloader) {
            this.wrappedDownloader = wrappedDownloader;
        }

        public InputStream getStream(String imageUri, Object extra) throws IOException {
            switch(ImageLoaderConfiguration.SyntheticClass_1.$SwitchMap$com$nostra13$universalimageloader$core$download$ImageDownloader$Scheme[Scheme.ofUri(imageUri).ordinal()]) {
            case 1:
            case 2:
                throw new IllegalStateException();
            default:
                return this.wrappedDownloader.getStream(imageUri, extra);
            }
        }
    }

    public static class Builder {
        private static final String WARNING_OVERLAP_DISK_CACHE_PARAMS = "diskCache(), diskCacheSize() and diskCacheFileCount calls overlap each other";
        private static final String WARNING_OVERLAP_DISK_CACHE_NAME_GENERATOR = "diskCache() and diskCacheFileNameGenerator() calls overlap each other";
        private static final String WARNING_OVERLAP_MEMORY_CACHE = "memoryCache() and memoryCacheSize() calls overlap each other";
        private static final String WARNING_OVERLAP_EXECUTOR = "threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.";
        public static final int DEFAULT_THREAD_POOL_SIZE = 3;
        public static final int DEFAULT_THREAD_PRIORITY = 3;
        public static final QueueProcessingType DEFAULT_TASK_PROCESSING_TYPE;
        private Context context;
        private int maxImageWidthForMemoryCache = 0;
        private int maxImageHeightForMemoryCache = 0;
        private int maxImageWidthForDiskCache = 0;
        private int maxImageHeightForDiskCache = 0;
        private BitmapProcessor processorForDiskCache = null;
        private Executor taskExecutor = null;
        private Executor taskExecutorForCachedImages = null;
        private boolean customExecutor = false;
        private boolean customExecutorForCachedImages = false;
        private int threadPoolSize = 3;
        private int threadPriority = 3;
        private boolean denyCacheImageMultipleSizesInMemory = false;
        private QueueProcessingType tasksProcessingType;
        private int memoryCacheSize;
        private long diskCacheSize;
        private int diskCacheFileCount;
        private MemoryCache memoryCache;
        private DiskCache diskCache;
        private FileNameGenerator diskCacheFileNameGenerator;
        private ImageDownloader downloader;
        private ImageDecoder decoder;
        private DisplayImageOptions defaultDisplayImageOptions;
        private boolean writeLogs;

        public Builder(Context context) {
            this.tasksProcessingType = DEFAULT_TASK_PROCESSING_TYPE;
            this.memoryCacheSize = 0;
            this.diskCacheSize = 0L;
            this.diskCacheFileCount = 0;
            this.memoryCache = null;
            this.diskCache = null;
            this.diskCacheFileNameGenerator = null;
            this.downloader = null;
            this.defaultDisplayImageOptions = null;
            this.writeLogs = false;
            this.context = context.getApplicationContext();
        }

        public ImageLoaderConfiguration.Builder memoryCacheExtraOptions(int maxImageWidthForMemoryCache, int maxImageHeightForMemoryCache) {
            this.maxImageWidthForMemoryCache = maxImageWidthForMemoryCache;
            this.maxImageHeightForMemoryCache = maxImageHeightForMemoryCache;
            return this;
        }

        /** @deprecated */
        @Deprecated
        public ImageLoaderConfiguration.Builder discCacheExtraOptions(int maxImageWidthForDiskCache, int maxImageHeightForDiskCache, BitmapProcessor processorForDiskCache) {
            return this.diskCacheExtraOptions(maxImageWidthForDiskCache, maxImageHeightForDiskCache, processorForDiskCache);
        }

        public ImageLoaderConfiguration.Builder diskCacheExtraOptions(int maxImageWidthForDiskCache, int maxImageHeightForDiskCache, BitmapProcessor processorForDiskCache) {
            this.maxImageWidthForDiskCache = maxImageWidthForDiskCache;
            this.maxImageHeightForDiskCache = maxImageHeightForDiskCache;
            this.processorForDiskCache = processorForDiskCache;
            return this;
        }

        public ImageLoaderConfiguration.Builder taskExecutor(Executor executor) {
            if(this.threadPoolSize != 3 || this.threadPriority != 3 || this.tasksProcessingType != DEFAULT_TASK_PROCESSING_TYPE) {
                L.w("threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.", new Object[0]);
            }

            this.taskExecutor = executor;
            return this;
        }

        public ImageLoaderConfiguration.Builder taskExecutorForCachedImages(Executor executorForCachedImages) {
            if(this.threadPoolSize != 3 || this.threadPriority != 3 || this.tasksProcessingType != DEFAULT_TASK_PROCESSING_TYPE) {
                L.w("threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.", new Object[0]);
            }

            this.taskExecutorForCachedImages = executorForCachedImages;
            return this;
        }

        public ImageLoaderConfiguration.Builder threadPoolSize(int threadPoolSize) {
            if(this.taskExecutor != null || this.taskExecutorForCachedImages != null) {
                L.w("threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.", new Object[0]);
            }

            this.threadPoolSize = threadPoolSize;
            return this;
        }

        public ImageLoaderConfiguration.Builder threadPriority(int threadPriority) {
            if(this.taskExecutor != null || this.taskExecutorForCachedImages != null) {
                L.w("threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.", new Object[0]);
            }

            if(threadPriority < 1) {
                this.threadPriority = 1;
            } else if(threadPriority > 10) {
                this.threadPriority = 10;
            } else {
                this.threadPriority = threadPriority;
            }

            return this;
        }

        public ImageLoaderConfiguration.Builder denyCacheImageMultipleSizesInMemory() {
            this.denyCacheImageMultipleSizesInMemory = true;
            return this;
        }

        public ImageLoaderConfiguration.Builder tasksProcessingOrder(QueueProcessingType tasksProcessingType) {
            if(this.taskExecutor != null || this.taskExecutorForCachedImages != null) {
                L.w("threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.", new Object[0]);
            }

            this.tasksProcessingType = tasksProcessingType;
            return this;
        }

        public ImageLoaderConfiguration.Builder memoryCacheSize(int memoryCacheSize) {
            if(memoryCacheSize <= 0) {
                throw new IllegalArgumentException("memoryCacheSize must be a positive number");
            } else {
                if(this.memoryCache != null) {
                    L.w("memoryCache() and memoryCacheSize() calls overlap each other", new Object[0]);
                }

                this.memoryCacheSize = memoryCacheSize;
                return this;
            }
        }

        public ImageLoaderConfiguration.Builder memoryCacheSizePercentage(int availableMemoryPercent) {
            if(availableMemoryPercent > 0 && availableMemoryPercent < 100) {
                if(this.memoryCache != null) {
                    L.w("memoryCache() and memoryCacheSize() calls overlap each other", new Object[0]);
                }

                long availableMemory = Runtime.getRuntime().maxMemory();
                this.memoryCacheSize = (int)((float)availableMemory * ((float)availableMemoryPercent / 100.0F));
                return this;
            } else {
                throw new IllegalArgumentException("availableMemoryPercent must be in range (0 < % < 100)");
            }
        }

        public ImageLoaderConfiguration.Builder memoryCache(MemoryCache memoryCache) {
            if(this.memoryCacheSize != 0) {
                L.w("memoryCache() and memoryCacheSize() calls overlap each other", new Object[0]);
            }

            this.memoryCache = memoryCache;
            return this;
        }

        /** @deprecated */
        @Deprecated
        public ImageLoaderConfiguration.Builder discCacheSize(int maxCacheSize) {
            return this.diskCacheSize(maxCacheSize);
        }

        public ImageLoaderConfiguration.Builder diskCacheSize(int maxCacheSize) {
            if(maxCacheSize <= 0) {
                throw new IllegalArgumentException("maxCacheSize must be a positive number");
            } else {
                if(this.diskCache != null) {
                    L.w("diskCache(), diskCacheSize() and diskCacheFileCount calls overlap each other", new Object[0]);
                }

                this.diskCacheSize = (long)maxCacheSize;
                return this;
            }
        }

        /** @deprecated */
        @Deprecated
        public ImageLoaderConfiguration.Builder discCacheFileCount(int maxFileCount) {
            return this.diskCacheFileCount(maxFileCount);
        }

        public ImageLoaderConfiguration.Builder diskCacheFileCount(int maxFileCount) {
            if(maxFileCount <= 0) {
                throw new IllegalArgumentException("maxFileCount must be a positive number");
            } else {
                if(this.diskCache != null) {
                    L.w("diskCache(), diskCacheSize() and diskCacheFileCount calls overlap each other", new Object[0]);
                }

                this.diskCacheFileCount = maxFileCount;
                return this;
            }
        }

        /** @deprecated */
        @Deprecated
        public ImageLoaderConfiguration.Builder discCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
            return this.diskCacheFileNameGenerator(fileNameGenerator);
        }

        public ImageLoaderConfiguration.Builder diskCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
            if(this.diskCache != null) {
                L.w("diskCache() and diskCacheFileNameGenerator() calls overlap each other", new Object[0]);
            }

            this.diskCacheFileNameGenerator = fileNameGenerator;
            return this;
        }

        /** @deprecated */
        @Deprecated
        public ImageLoaderConfiguration.Builder discCache(DiskCache diskCache) {
            return this.diskCache(diskCache);
        }

        public ImageLoaderConfiguration.Builder diskCache(DiskCache diskCache) {
            if(this.diskCacheSize > 0L || this.diskCacheFileCount > 0) {
                L.w("diskCache(), diskCacheSize() and diskCacheFileCount calls overlap each other", new Object[0]);
            }

            if(this.diskCacheFileNameGenerator != null) {
                L.w("diskCache() and diskCacheFileNameGenerator() calls overlap each other", new Object[0]);
            }

            this.diskCache = diskCache;
            return this;
        }

        public ImageLoaderConfiguration.Builder imageDownloader(ImageDownloader imageDownloader) {
            this.downloader = imageDownloader;
            return this;
        }

        public ImageLoaderConfiguration.Builder imageDecoder(ImageDecoder imageDecoder) {
            this.decoder = imageDecoder;
            return this;
        }

        public ImageLoaderConfiguration.Builder defaultDisplayImageOptions(DisplayImageOptions defaultDisplayImageOptions) {
            this.defaultDisplayImageOptions = defaultDisplayImageOptions;
            return this;
        }

        public ImageLoaderConfiguration.Builder writeDebugLogs() {
            this.writeLogs = true;
            return this;
        }

        public ImageLoaderConfiguration build() {
            this.initEmptyFieldsWithDefaultValues();
            return new ImageLoaderConfiguration(this, (ImageLoaderConfiguration.SyntheticClass_1)null);
        }

        private void initEmptyFieldsWithDefaultValues() {
            if(this.taskExecutor == null) {
                this.taskExecutor = DefaultConfigurationFactory.createExecutor(this.threadPoolSize, this.threadPriority, this.tasksProcessingType);
            } else {
                this.customExecutor = true;
            }

            if(this.taskExecutorForCachedImages == null) {
                this.taskExecutorForCachedImages = DefaultConfigurationFactory.createExecutor(this.threadPoolSize, this.threadPriority, this.tasksProcessingType);
            } else {
                this.customExecutorForCachedImages = true;
            }

            if(this.diskCache == null) {
                if(this.diskCacheFileNameGenerator == null) {
                    this.diskCacheFileNameGenerator = DefaultConfigurationFactory.createFileNameGenerator();
                }

                this.diskCache = DefaultConfigurationFactory.createDiskCache(this.context, this.diskCacheFileNameGenerator, this.diskCacheSize, this.diskCacheFileCount);
            }

            if(this.memoryCache == null) {
                this.memoryCache = DefaultConfigurationFactory.createMemoryCache(this.context, this.memoryCacheSize);
            }

            if(this.denyCacheImageMultipleSizesInMemory) {
                this.memoryCache = new FuzzyKeyMemoryCache(this.memoryCache, MemoryCacheUtils.createFuzzyKeyComparator());
            }

            if(this.downloader == null) {
                this.downloader = DefaultConfigurationFactory.createImageDownloader(this.context);
            }

            if(this.decoder == null) {
                this.decoder = DefaultConfigurationFactory.createImageDecoder(this.writeLogs);
            }

            if(this.defaultDisplayImageOptions == null) {
                this.defaultDisplayImageOptions = DisplayImageOptions.createSimple();
            }

        }

        static {
            DEFAULT_TASK_PROCESSING_TYPE = QueueProcessingType.FIFO;
        }
    }
}

private void initImageLoader() {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(50 * 1024 * 1024) // 50 Mb
                .diskCacheFileCount(300)
                .imageDownloader(new BaseImageDownloader(context))
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(config);
    }

universal imageloader的DisplayImageOptions

//两像素圆角
    private DisplayImageOptions optionsRounded = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.ic_launcher)
            .showImageForEmptyUri(R.mipmap.ic_launcher)
            .showImageOnFail(R.mipmap.ic_launcher)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .displayer(new RoundedBitmapDisplayer(2))
            .build();
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.nostra13.universalimageloader.core;

import android.content.res.Resources;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.process.BitmapProcessor;

public final class DisplayImageOptions {
    private final int imageResOnLoading;
    private final int imageResForEmptyUri;
    private final int imageResOnFail;
    private final Drawable imageOnLoading;
    private final Drawable imageForEmptyUri;
    private final Drawable imageOnFail;
    private final boolean resetViewBeforeLoading;
    private final boolean cacheInMemory;
    private final boolean cacheOnDisk;
    private final ImageScaleType imageScaleType;
    private final Options decodingOptions;
    private final int delayBeforeLoading;
    private final boolean considerExifParams;
    private final Object extraForDownloader;
    private final BitmapProcessor preProcessor;
    private final BitmapProcessor postProcessor;
    private final BitmapDisplayer displayer;
    private final Handler handler;
    private final boolean isSyncLoading;

    private DisplayImageOptions(DisplayImageOptions.Builder builder) {
        this.imageResOnLoading = builder.imageResOnLoading;
        this.imageResForEmptyUri = builder.imageResForEmptyUri;
        this.imageResOnFail = builder.imageResOnFail;
        this.imageOnLoading = builder.imageOnLoading;
        this.imageForEmptyUri = builder.imageForEmptyUri;
        this.imageOnFail = builder.imageOnFail;
        this.resetViewBeforeLoading = builder.resetViewBeforeLoading;
        this.cacheInMemory = builder.cacheInMemory;
        this.cacheOnDisk = builder.cacheOnDisk;
        this.imageScaleType = builder.imageScaleType;
        this.decodingOptions = builder.decodingOptions;
        this.delayBeforeLoading = builder.delayBeforeLoading;
        this.considerExifParams = builder.considerExifParams;
        this.extraForDownloader = builder.extraForDownloader;
        this.preProcessor = builder.preProcessor;
        this.postProcessor = builder.postProcessor;
        this.displayer = builder.displayer;
        this.handler = builder.handler;
        this.isSyncLoading = builder.isSyncLoading;
    }

    public boolean shouldShowImageOnLoading() {
        return this.imageOnLoading != null || this.imageResOnLoading != 0;
    }

    public boolean shouldShowImageForEmptyUri() {
        return this.imageForEmptyUri != null || this.imageResForEmptyUri != 0;
    }

    public boolean shouldShowImageOnFail() {
        return this.imageOnFail != null || this.imageResOnFail != 0;
    }

    public boolean shouldPreProcess() {
        return this.preProcessor != null;
    }

    public boolean shouldPostProcess() {
        return this.postProcessor != null;
    }

    public boolean shouldDelayBeforeLoading() {
        return this.delayBeforeLoading > 0;
    }

    public Drawable getImageOnLoading(Resources res) {
        return this.imageResOnLoading != 0?res.getDrawable(this.imageResOnLoading):this.imageOnLoading;
    }

    public Drawable getImageForEmptyUri(Resources res) {
        return this.imageResForEmptyUri != 0?res.getDrawable(this.imageResForEmptyUri):this.imageForEmptyUri;
    }

    public Drawable getImageOnFail(Resources res) {
        return this.imageResOnFail != 0?res.getDrawable(this.imageResOnFail):this.imageOnFail;
    }

    public boolean isResetViewBeforeLoading() {
        return this.resetViewBeforeLoading;
    }

    public boolean isCacheInMemory() {
        return this.cacheInMemory;
    }

    public boolean isCacheOnDisk() {
        return this.cacheOnDisk;
    }

    public ImageScaleType getImageScaleType() {
        return this.imageScaleType;
    }

    public Options getDecodingOptions() {
        return this.decodingOptions;
    }

    public int getDelayBeforeLoading() {
        return this.delayBeforeLoading;
    }

    public boolean isConsiderExifParams() {
        return this.considerExifParams;
    }

    public Object getExtraForDownloader() {
        return this.extraForDownloader;
    }

    public BitmapProcessor getPreProcessor() {
        return this.preProcessor;
    }

    public BitmapProcessor getPostProcessor() {
        return this.postProcessor;
    }

    public BitmapDisplayer getDisplayer() {
        return this.displayer;
    }

    public Handler getHandler() {
        return this.handler;
    }

    boolean isSyncLoading() {
        return this.isSyncLoading;
    }

    public static DisplayImageOptions createSimple() {
        return (new DisplayImageOptions.Builder()).build();
    }

    public static class Builder {
        private int imageResOnLoading = 0;
        private int imageResForEmptyUri = 0;
        private int imageResOnFail = 0;
        private Drawable imageOnLoading = null;
        private Drawable imageForEmptyUri = null;
        private Drawable imageOnFail = null;
        private boolean resetViewBeforeLoading = false;
        private boolean cacheInMemory = false;
        private boolean cacheOnDisk = false;
        private ImageScaleType imageScaleType;
        private Options decodingOptions;
        private int delayBeforeLoading;
        private boolean considerExifParams;
        private Object extraForDownloader;
        private BitmapProcessor preProcessor;
        private BitmapProcessor postProcessor;
        private BitmapDisplayer displayer;
        private Handler handler;
        private boolean isSyncLoading;

        public Builder() {
            this.imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2;
            this.decodingOptions = new Options();
            this.delayBeforeLoading = 0;
            this.considerExifParams = false;
            this.extraForDownloader = null;
            this.preProcessor = null;
            this.postProcessor = null;
            this.displayer = DefaultConfigurationFactory.createBitmapDisplayer();
            this.handler = null;
            this.isSyncLoading = false;
        }

        /** @deprecated */
        @Deprecated
        public DisplayImageOptions.Builder showStubImage(int imageRes) {
            this.imageResOnLoading = imageRes;
            return this;
        }

        public DisplayImageOptions.Builder showImageOnLoading(int imageRes) {
            this.imageResOnLoading = imageRes;
            return this;
        }

        public DisplayImageOptions.Builder showImageOnLoading(Drawable drawable) {
            this.imageOnLoading = drawable;
            return this;
        }

        public DisplayImageOptions.Builder showImageForEmptyUri(int imageRes) {
            this.imageResForEmptyUri = imageRes;
            return this;
        }

        public DisplayImageOptions.Builder showImageForEmptyUri(Drawable drawable) {
            this.imageForEmptyUri = drawable;
            return this;
        }

        public DisplayImageOptions.Builder showImageOnFail(int imageRes) {
            this.imageResOnFail = imageRes;
            return this;
        }

        public DisplayImageOptions.Builder showImageOnFail(Drawable drawable) {
            this.imageOnFail = drawable;
            return this;
        }

        /** @deprecated */
        public DisplayImageOptions.Builder resetViewBeforeLoading() {
            this.resetViewBeforeLoading = true;
            return this;
        }

        public DisplayImageOptions.Builder resetViewBeforeLoading(boolean resetViewBeforeLoading) {
            this.resetViewBeforeLoading = resetViewBeforeLoading;
            return this;
        }

        /** @deprecated */
        @Deprecated
        public DisplayImageOptions.Builder cacheInMemory() {
            this.cacheInMemory = true;
            return this;
        }

        public DisplayImageOptions.Builder cacheInMemory(boolean cacheInMemory) {
            this.cacheInMemory = cacheInMemory;
            return this;
        }

        /** @deprecated */
        @Deprecated
        public DisplayImageOptions.Builder cacheOnDisc() {
            return this.cacheOnDisk(true);
        }

        /** @deprecated */
        @Deprecated
        public DisplayImageOptions.Builder cacheOnDisc(boolean cacheOnDisk) {
            return this.cacheOnDisk(cacheOnDisk);
        }

        public DisplayImageOptions.Builder cacheOnDisk(boolean cacheOnDisk) {
            this.cacheOnDisk = cacheOnDisk;
            return this;
        }

        public DisplayImageOptions.Builder imageScaleType(ImageScaleType imageScaleType) {
            this.imageScaleType = imageScaleType;
            return this;
        }

        public DisplayImageOptions.Builder bitmapConfig(Config bitmapConfig) {
            if(bitmapConfig == null) {
                throw new IllegalArgumentException("bitmapConfig can\'t be null");
            } else {
                this.decodingOptions.inPreferredConfig = bitmapConfig;
                return this;
            }
        }

        public DisplayImageOptions.Builder decodingOptions(Options decodingOptions) {
            if(decodingOptions == null) {
                throw new IllegalArgumentException("decodingOptions can\'t be null");
            } else {
                this.decodingOptions = decodingOptions;
                return this;
            }
        }

        public DisplayImageOptions.Builder delayBeforeLoading(int delayInMillis) {
            this.delayBeforeLoading = delayInMillis;
            return this;
        }

        public DisplayImageOptions.Builder extraForDownloader(Object extra) {
            this.extraForDownloader = extra;
            return this;
        }

        public DisplayImageOptions.Builder considerExifParams(boolean considerExifParams) {
            this.considerExifParams = considerExifParams;
            return this;
        }

        public DisplayImageOptions.Builder preProcessor(BitmapProcessor preProcessor) {
            this.preProcessor = preProcessor;
            return this;
        }

        public DisplayImageOptions.Builder postProcessor(BitmapProcessor postProcessor) {
            this.postProcessor = postProcessor;
            return this;
        }

        public DisplayImageOptions.Builder displayer(BitmapDisplayer displayer) {
            if(displayer == null) {
                throw new IllegalArgumentException("displayer can\'t be null");
            } else {
                this.displayer = displayer;
                return this;
            }
        }

        DisplayImageOptions.Builder syncLoading(boolean isSyncLoading) {
            this.isSyncLoading = isSyncLoading;
            return this;
        }

        public DisplayImageOptions.Builder handler(Handler handler) {
            this.handler = handler;
            return this;
        }

        public DisplayImageOptions.Builder cloneFrom(DisplayImageOptions options) {
            this.imageResOnLoading = options.imageResOnLoading;
            this.imageResForEmptyUri = options.imageResForEmptyUri;
            this.imageResOnFail = options.imageResOnFail;
            this.imageOnLoading = options.imageOnLoading;
            this.imageForEmptyUri = options.imageForEmptyUri;
            this.imageOnFail = options.imageOnFail;
            this.resetViewBeforeLoading = options.resetViewBeforeLoading;
            this.cacheInMemory = options.cacheInMemory;
            this.cacheOnDisk = options.cacheOnDisk;
            this.imageScaleType = options.imageScaleType;
            this.decodingOptions = options.decodingOptions;
            this.delayBeforeLoading = options.delayBeforeLoading;
            this.considerExifParams = options.considerExifParams;
            this.extraForDownloader = options.extraForDownloader;
            this.preProcessor = options.preProcessor;
            this.postProcessor = options.postProcessor;
            this.displayer = options.displayer;
            this.handler = options.handler;
            this.isSyncLoading = options.isSyncLoading;
            return this;
        }

        public DisplayImageOptions build() {
            return new DisplayImageOptions(this);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值