Giter Site home page Giter Site logo

webimageloader's Introduction

WebImageLoader

WebImageLoader is a library designed to take to hassle out of handling images on the web. It has the following features:

  • Images are downloaded on a background thread pool and saved to disk and memory.
  • Disk and memory cache size is configurable and can even be reconfigured on the fly.
  • Separate thread do load images back from disk after being cached, reducing I/O bottlenecks on most phones.
  • Reusing requests when the same image is requested multiple times.
  • Respects cache-control and expires headers and will refetch images when they expire (using conditional get).
  • Support image transformations which are also cached to disk and memory.
  • Support to do synchronous fetches while still taking advantage of the cache.
  • Easy setup without singletons.
  • Compatible with API level 7 and up.
  • Only depends on DiskLruCache.

Usage

Use the builder to build an ImageLoader that suits your needs.

// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
int memClass = am.getMemoryClass();

// Use part of the available memory for memory cache.
final int memoryCacheSize = 1024 * 1024 * memClass / 8;

File cacheDir = new File(getExternalCacheDir(), "images");
ImageLoader imageLoader = new ImageLoader.Builder(context)
        .enableDiskCache(cacheDir, 10 * 1024 * 1024)
        .enableMemoryCache(memoryCacheSize).build();

Or use the provided Applications class for convenience and reasonable defaults (which you can override!)

<application android:name="com.webimageloader.ext.ImageLoaderApplication">
    <!-- Your app -->
</application>

Retrive your loader like this.

ImageLoader imageLoader = ImageLoaderApplication.getLoader(context);

Loading images

Loading images is simple if you want it to be.

// This will show a nice fade in when the image has loaded
new ImageHelper(context, imageLoader)
        .setFadeIn(true)
        .load(imageView, "http://example.com/image.png");

You can also use specify a loading and failure image

new ImageHelper(context, imageLoader)
        .setLoadingResource(R.drawable.loading)
        .setErrorResource(R.drawable.error)
        .load(imageView, "http://example.com/image.png");

Loading images can also be done more explicit if needed.

Bitmap b = loader.load(imageView, "http://example.com/image.png", new Listener<ImageView>() {
    @Override
    public void onSuccess(ImageView v, Bitmap b) {
        // Everything went well
        v.setImageBitmap(b);
    }

    @Override
    public void onError(ImageView v, Throwable t) {
        // Something went wrong
        Log.d("MyApp", "Failed to load image", t);
    }
});

// Did we get an image immediately?
if (b != null) {
    imageView.setImageBitmap(b);
}

Transformations

You can transform (and cache!) the images you get.

final int width = 100;
final int height = 100;
        
Transformation t = new SimpleTransformation() {
    @Override
    public String getIdentifier() {
        // Pass a unique identifier for caching
        return "scale-" + width + "x" + height;
    }
    
    @Override
    public Bitmap transform(Bitmap b) {
        return Bitmap.createScaledBitmap(b, width, height, true);
    }
};

new ImageHelper(this, imageLoader)
        .load(imageView, "http://example.com/image.png", t);

Obtaining

You can include the library by downloading the .jar and also adding DiskLruCache to your project.

If you are a Maven user, simply add the following to your pom.xml:

<dependency>
    <groupId>com.webimageloader</groupId>
    <artifactId>webimageloader</artifactId>
    <version>1.1.4</version>
</dependency>

Developed By

License

WebImageLoader is licensed under Apache 2.0. If you find this library useful feel free to send me a tweet (or not).

Copyright 2012 Alexander Blom

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.

webimageloader's People

Contributors

lexs avatar vbabiy avatar atomfrede avatar

Watchers

Jianshuai avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.