All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nostra13.universalimageloader.core.DisplayImageOptions Maven / Gradle / Ivy

There is a newer version: 1.9.5
Show newest version
/*******************************************************************************
 * Copyright 2011-2013 Sergey Tarasevich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.nostra13.universalimageloader.core;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.process.BitmapProcessor;

/**
 * Contains options for image display. Defines:
 * 
    *
  • whether stub image will be displayed in {@link android.widget.ImageView ImageView} during image loading
  • *
  • whether stub image will be displayed in {@link android.widget.ImageView ImageView} if empty URI is passed
  • *
  • whether stub image will be displayed in {@link android.widget.ImageView ImageView} if image loading fails
  • *
  • whether {@link android.widget.ImageView ImageView} should be reset before image loading start
  • *
  • whether loaded image will be cached in memory
  • *
  • whether loaded image will be cached on disc
  • *
  • image scale type
  • *
  • decoding options (including bitmap decoding configuration)
  • *
  • delay before loading of image
  • *
  • auxiliary object which will be passed to {@link ImageDownloader#getStream(java.net.URI, Object) ImageDownloader}
  • *
  • pre-processor for image Bitmap (before caching in memory)
  • *
  • post-processor for image Bitmap (after caching in memory, before displaying)
  • *
  • how decoded {@link Bitmap} will be displayed
  • *
* * You can create instance: *
    *
  • with {@link Builder}:
    * i.e. : * new {@link DisplayImageOptions}.{@link Builder#Builder() Builder()}.{@link Builder#cacheInMemory() cacheInMemory()}. * {@link Builder#showStubImage(int) showStubImage()}.{@link Builder#build() build()}
    *
  • *
  • or by static method: {@link #createSimple()}

  • * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @since 1.0.0 */ public final class DisplayImageOptions { private final int stubImage; private final int imageForEmptyUri; private final int imageOnFail; private final boolean resetViewBeforeLoading; private final boolean cacheInMemory; private final boolean cacheOnDisc; private final ImageScaleType imageScaleType; private final Options decodingOptions; private final int delayBeforeLoading; private final Object extraForDownloader; private final BitmapProcessor preProcessor; private final BitmapProcessor postProcessor; private final BitmapDisplayer displayer; private DisplayImageOptions(Builder builder) { stubImage = builder.stubImage; imageForEmptyUri = builder.imageForEmptyUri; imageOnFail = builder.imageOnFail; resetViewBeforeLoading = builder.resetViewBeforeLoading; cacheInMemory = builder.cacheInMemory; cacheOnDisc = builder.cacheOnDisc; imageScaleType = builder.imageScaleType; decodingOptions = builder.decodingOptions; delayBeforeLoading = builder.delayBeforeLoading; extraForDownloader = builder.extraForDownloader; preProcessor = builder.preProcessor; postProcessor = builder.postProcessor; displayer = builder.displayer; } public boolean shouldShowStubImage() { return stubImage != 0; } public boolean shouldShowImageForEmptyUri() { return imageForEmptyUri != 0; } public boolean shouldShowImageOnFail() { return imageOnFail != 0; } public boolean shouldPreProcess() { return preProcessor != null; } public boolean shouldPostProcess() { return postProcessor != null; } public boolean shouldDelayBeforeLoading() { return delayBeforeLoading > 0; } public int getStubImage() { return stubImage; } public int getImageForEmptyUri() { return imageForEmptyUri; } public int getImageOnFail() { return imageOnFail; } public boolean isResetViewBeforeLoading() { return resetViewBeforeLoading; } public boolean isCacheInMemory() { return cacheInMemory; } public boolean isCacheOnDisc() { return cacheOnDisc; } public ImageScaleType getImageScaleType() { return imageScaleType; } public Options getDecodingOptions() { return decodingOptions; } public int getDelayBeforeLoading() { return delayBeforeLoading; } public Object getExtraForDownloader() { return extraForDownloader; } public BitmapProcessor getPreProcessor() { return preProcessor; } public BitmapProcessor getPostProcessor() { return postProcessor; } public BitmapDisplayer getDisplayer() { return displayer; } /** * Builder for {@link DisplayImageOptions} * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) */ public static class Builder { private int stubImage = 0; private int imageForEmptyUri = 0; private int imageOnFail = 0; private boolean resetViewBeforeLoading = false; private boolean cacheInMemory = false; private boolean cacheOnDisc = false; private ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2; private Options decodingOptions = new Options(); private int delayBeforeLoading = 0; private Object extraForDownloader = null; private BitmapProcessor preProcessor = null; private BitmapProcessor postProcessor = null; private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer(); public Builder() { decodingOptions.inPurgeable = true; decodingOptions.inInputShareable = true; } /** * Stub image will be displayed in {@link android.widget.ImageView ImageView} during image loading * * @param stubImageRes Stub image resource */ public Builder showStubImage(int stubImageRes) { stubImage = stubImageRes; return this; } /** * Incoming image will be displayed in {@link android.widget.ImageView ImageView} if empty URI (null or empty * string) will be passed to ImageLoader.displayImage(...) method. * * @param imageRes Image resource */ public Builder showImageForEmptyUri(int imageRes) { imageForEmptyUri = imageRes; return this; } /** * Incoming image will be displayed in {@link android.widget.ImageView ImageView} if some error occurs during * requested image loading/decoding. * * @param imageRes Image resource */ public Builder showImageOnFail(int imageRes) { imageOnFail = imageRes; return this; } /** {@link android.widget.ImageView ImageView} will be reset (set null) before image loading start */ public Builder resetViewBeforeLoading() { resetViewBeforeLoading = true; return this; } /** Loaded image will be cached in memory */ public Builder cacheInMemory() { cacheInMemory = true; return this; } /** Loaded image will be cached on disc */ public Builder cacheOnDisc() { cacheOnDisc = true; return this; } /** * Sets {@linkplain ImageScaleType scale type} for decoding image. This parameter is used while define scale * size for decoding image to Bitmap. Default value - {@link ImageScaleType#IN_SAMPLE_POWER_OF_2} */ public Builder imageScaleType(ImageScaleType imageScaleType) { this.imageScaleType = imageScaleType; return this; } /** Sets {@link Bitmap.Config bitmap config} for image decoding. Default value - {@link Bitmap.Config#ARGB_8888} */ public Builder bitmapConfig(Bitmap.Config bitmapConfig) { decodingOptions.inPreferredConfig = bitmapConfig; return this; } /** * Sets options for image decoding.
    * NOTE: {@link Options#inSampleSize} of incoming options will NOT be considered. Library * calculate the most appropriate sample size itself according yo {@link #imageScaleType(ImageScaleType)} options.
    * NOTE: This option overlaps {@link #bitmapConfig(android.graphics.Bitmap.Config) bitmapConfig()} * option. */ public Builder decodingOptions(Options decodingOptions) { this.decodingOptions = decodingOptions; return this; } /** Sets delay time before starting loading task. Default - no delay. */ public Builder delayBeforeLoading(int delayInMillis) { this.delayBeforeLoading = delayInMillis; return this; } /** Sets auxiliary object which will be passed to {@link ImageDownloader#getStream(java.net.URI, Object)} */ public Builder extraForDownloader(Object extra) { this.extraForDownloader = extra; return this; } /** * Sets bitmap processor which will be process bitmaps before they will be cached in memory. So memory cache * will contain bitmap processed by incoming preProcessor.
    * Image will be pre-processed even if caching in memory is disabled. */ public Builder preProcessor(BitmapProcessor preProcessor) { this.preProcessor = preProcessor; return this; } /** * Sets bitmap processor which will be process bitmaps before they will be displayed in {@link ImageView} but * after they'll have been saved in memory cache. */ public Builder postProcessor(BitmapProcessor postProcessor) { this.postProcessor = postProcessor; return this; } /** * Sets custom {@link BitmapDisplayer displayer} for image loading task. Default value - * {@link DefaultConfigurationFactory#createBitmapDisplayer()} */ public Builder displayer(BitmapDisplayer displayer) { this.displayer = displayer; return this; } /** Sets all options equal to incoming options */ public Builder cloneFrom(DisplayImageOptions options) { stubImage = options.stubImage; imageForEmptyUri = options.imageForEmptyUri; imageOnFail = options.imageOnFail; resetViewBeforeLoading = options.resetViewBeforeLoading; cacheInMemory = options.cacheInMemory; cacheOnDisc = options.cacheOnDisc; imageScaleType = options.imageScaleType; decodingOptions = options.decodingOptions; delayBeforeLoading = options.delayBeforeLoading; extraForDownloader = options.extraForDownloader; preProcessor = options.preProcessor; postProcessor = options.postProcessor; displayer = options.displayer; return this; } /** Builds configured {@link DisplayImageOptions} object */ public DisplayImageOptions build() { return new DisplayImageOptions(this); } } /** * Creates options appropriate for single displaying: *
      *
    • View will not be reset before loading
    • *
    • Loaded image will not be cached in memory
    • *
    • Loaded image will not be cached on disc
    • *
    • {@link ImageScaleType#IN_SAMPLE_POWER_OF_2} decoding type will be used
    • *
    • {@link Bitmap.Config#ARGB_8888} bitmap config will be used for image decoding
    • *
    • {@link SimpleBitmapDisplayer} will be used for image displaying
    • *
    * * These option are appropriate for simple single-use image (from drawables or from Internet) displaying. */ public static DisplayImageOptions createSimple() { return new Builder().build(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy