赞
踩
- <dependency>
- <groupId>com.nostra13.universalimageloader</groupId>
- <artifactId>universal-image-loader</artifactId>
- <version>1.9.3</version>
- </dependency>
Gradle:
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
- <uses-permission android:name="android.permission.INTERNET" />
- <!-- Include following permission if you want to cache images on SD card -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- // Create global configuration and initialize ImageLoader with this config
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
- ...
- .build();
- ImageLoader.getInstance().init(config);
"http://site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables - ImageView.setImageResource(...) instead of using of ImageLoader.
- imageLoader.displayImage(imageUri, imageView);
- imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
- @Override
- public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
- // Do whatever you want with Bitmap
- }
- });
- // Load image, decode it to Bitmap and return Bitmap synchronously
- Bitmap bmp = imageLoader.loadImageSync(imageUri);
- // Load image, decode it to Bitmap and return Bitmap to callback
- ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size
- imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {
- @Override
- public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
- // Do whatever you want with Bitmap
- }
- });
- // Load image, decode it to Bitmap and return Bitmap synchronously
- ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size
- Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);
还可以通过
ImageLoadingProgressListener监听进度。
- // DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
- // See the sample project how to use ImageLoader correctly.
- File cacheDir = StorageUtils.getCacheDirectory(context);
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
- .memoryCacheExtraOptions(480, 800) // default = device screen dimensions 推荐
- .diskCacheExtraOptions(480, 800, null) //.推荐diskCacheExtraOptions(480, 800, null)
- .taskExecutor(...)
- .taskExecutorForCachedImages(...)
- .threadPoolSize(3) // default 推荐1-5
- .threadPriority(Thread.NORM_PRIORITY - 2) // default
- .tasksProcessingOrder(QueueProcessingType.FIFO) // default
- .denyCacheImageMultipleSizesInMemory() //设置内存缓存不允许缓存一张图片的多个尺寸,默认允许。
- .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //使用强引用的缓存使用它,不过推荐使用weak与strong引用结合的UsingFreqLimitedMemoryCache或者使用全弱引用的WeakMemoryCache
- .memoryCacheSize(2 * 1024 * 1024)
- .memoryCacheSizePercentage(13) // default
- .diskCache(new UnlimitedDiscCache(cacheDir)) // default
- .diskCacheSize(50 * 1024 * 1024)
- .diskCacheFileCount(100)
- .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
- .imageDownloader(new BaseImageDownloader(context)) // default
- .imageDecoder(new BaseImageDecoder()) // default
- .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
- .writeDebugLogs()
- .build();
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache");
.diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
- // DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
- // See the sample project how to use ImageLoader correctly.
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- .showImageOnLoading(R.drawable.ic_stub) // resource or drawable
- .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
- .showImageOnFail(R.drawable.ic_error) // resource or drawable
- .resetViewBeforeLoading(false) // default
- .delayBeforeLoading(1000)
- .cacheInMemory(false) // default
- .cacheOnDisk(false) // default
- .preProcessor(...)
- .postProcessor(...)
- .extraForDownloader(...)
- .considerExifParams(false) // default
- .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 推荐.imageScaleType(ImageScaleType.EXACTLY) 节省内存
- .bitmapConfig(Bitmap.Config.ARGB_8888) // default 推荐.bitmapConfig(Bitmap.Config.RGB_565)节省内存
- .decodingOptions(...)
- .displayer(new SimpleBitmapDisplayer()) // default
- <span style="white-space:pre"> </span>/*推荐使用RoundedBitmapDisplayer (Displays bitmap with rounded corners)和
- <span style="white-space:pre"> </span>*FadeInBitmapDisplayer (Displays image with "fade in" animation
- <span style="white-space:pre"> </span>*但是使用RoundedBitmapDisplayer有些必要的注意事项,看后面提示。
- <span style="white-space:pre"> </span>*/
- .handler(new Handler()) // default
- .build();
以上配置中的:
- // Create default options which will be used for every
- // displayImage(...) call if no options will be passed to this method
- DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
- ...
- .cacheInMemory(true)
- .cacheOnDisk(true)<pre name="code" class="java">boolean pauseOnScroll = false; // or true
- boolean pauseOnFling = true; // or false
- PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
- listView.setOnScrollListener(listener);
- // Then later, when you want to display image
- ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
或者通过如下方式:
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- ...
- .cacheInMemory(true)
- .cacheOnDisk(true)
- ...
- .build();
- ImageLoader.getInstance().displayImage(imageUrl, imageView, options); // Incoming options will be used
2、为了防止list(listview,grid等)滚动迟钝,可以使用PauseOnScrollListener
- boolean pauseOnScroll = false; // or true
- boolean pauseOnFling = true; // or false
- PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
- listView.setOnScrollListener(listener);
- To generate the rounded images I simply wrote a custom Drawable that draws a rounded rectangle using Canvas.drawRoundRect(). The trick is to use a Paint with a BitmapShader to fill the rounded rectangle with a texture instead of a simple color. Here is what the code looks like:
-
- BitmapShader shader;
- shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
-
- Paint paint = new Paint();
- paint.setAntiAlias(true);
- paint.setShader(shader);
-
- RectF rect = new RectF(0.0f, 0.0f, width, height);
-
- // rect contains the bounds of the shape
- // radius is the radius in pixels of the rounded corners
- // paint contains the shader that will texture the shape
- canvas.drawRoundRect(rect, radius, radius, paint);
2、使用在线图片圆角绘制工具:http://www.asqql.com/gifyjtp/ 和http://www.roundpic.com/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。