ImageLoader加载圆形、圆角、原图照片

开源项目Universal-Image-Loader功能很强大,是很好的个开源项目,下面就针对Imageloader加载圆形、圆角、和原图照片做详细讲解。

最新版本Github地址:https://github.com/nostra13/Android-Universal-Image-Loader

显示圆形图片方案有两种:

1、通过自定义一个圆形的ImageView,然后调用imageloader  displayImage()方法,这种方法缺点是没有缓存

2、使用ImageLoader的DisplayImageOptions.Builder().displayer(mDoctorRoundDisplayer) 方法,这种方式会缓存,今天就主要来针对第二种方法做详细讲解

第一步:加入库

下载universal-image-loader:1.9.3.jar 然后在gradle文件中引入或者通过maven 直接在gradle文件中声明:compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3',imageloader最小支 持 版本android4.1,否则会报错 ,在gradle文件中声明 minSdkVersion16

第二步:配置清单文件权限

第三步:在Application中配置ImageLoaderConfiguration参数并初始化

mDefaultImageOpions=newDisplayImageOptions.Builder()

.imageScaleType(ImageScaleType.EXACTLY)

.bitmapConfig(Bitmap.Config.RGB_565)

.cacheInMemory(true)

.cacheOnDisk(true)

.build();

//设置为0会启用默认的memory大小,为1/8的可用内存控件

MemoryCache normalMemoryCache = DefaultConfigurationFactory.createMemoryCache(0);

ImageLoaderConfiguration config =newImageLoaderConfiguration.Builder(context)

.threadPriority(Thread.NORM_PRIORITY-2)

.denyCacheImageMultipleSizesInMemory()

.diskCacheFileNameGenerator(newMd5FileNameGenerator())

.diskCacheSize(DEFAULT_DISC_CACHE_SIZE)

.tasksProcessingOrder(QueueProcessingType.LIFO)

.defaultDisplayImageOptions(mDefaultImageOpions)

.memoryCache(normalMemoryCache)

.writeDebugLogs()

.build();

// Initialize ImageLoader with configuration.

mInnerImageLoader= ImageLoader.getInstance();

mInnerImageLoader.init(config);

第四部:定义原型图片player

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapShader;

importandroid.graphics.Canvas;

importandroid.graphics.ColorFilter;

importandroid.graphics.Matrix;

importandroid.graphics.Paint;

importandroid.graphics.Rect;

importandroid.graphics.RectF;

importandroid.graphics.Matrix.ScaleToFit;

importandroid.graphics.Shader.TileMode;

importandroid.graphics.drawable.Drawable;

importcom.nostra13.universalimageloader.core.assist.LoadedFrom;

importcom.nostra13.universalimageloader.core.display.BitmapDisplayer;

importcom.nostra13.universalimageloader.core.imageaware.ImageAware;

importcom.nostra13.universalimageloader.core.imageaware.ImageViewAware;

public classRoundedBitmapWithBorderDisplayerimplementsBitmapDisplayer {

protected final intborderThickness;

protected final intborderColor;

publicRoundedBitmapWithBorderDisplayer() {

this(0,-16777216);

}

publicRoundedBitmapWithBorderDisplayer(intborderThickness) {

this(borderThickness,-16777216);

}

publicRoundedBitmapWithBorderDisplayer(intborderThickness, intborderColor) {

this.borderThickness= borderThickness;

this.borderColor= borderColor;

}

public voiddisplay(Bitmap bitmap,ImageAware imageAware,LoadedFrom loadedFrom) {

if(!(imageAwareinstanceofImageViewAware)) {

throw newIllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");

}else{

imageAware.setImageDrawable(newRoundedBitmapWithBorderDisplayer.RoundedDrawable(bitmap, this.borderThickness, this.borderColor));

}

}

public static classRoundedDrawableextendsDrawable {

protected final intmargin;

protected finalRectFmRect=newRectF();

protected finalRectFmBitmapRect;

protected finalRectFmBackgroundRect;

protected finalBitmapShaderbitmapShader;

protected finalPaintpaint;

protected finalPaintbackPaint;

publicRoundedDrawable(Bitmap bitmap, intmargin, intcolor) {

this.margin= margin;

this.bitmapShader=newBitmapShader(bitmap,TileMode.CLAMP,TileMode.CLAMP);

this.mBitmapRect=newRectF((float)margin,(float)margin,(float)(bitmap.getWidth() - margin),(float)(bitmap.getHeight() - margin));

this.mBackgroundRect=newRectF(0.0F,0.0F,(float)bitmap.getWidth(),(float)bitmap.getHeight());

this.paint=newPaint();

this.paint.setAntiAlias(true);

this.paint.setShader(this.bitmapShader);

this.backPaint=newPaint();

this.backPaint.setColor(color);

}

protected voidonBoundsChange(Rect bounds) {

super.onBoundsChange(bounds);

this.mRect.set((float)this.margin,(float)this.margin,(float)(bounds.width() -this.margin),(float)(bounds.height() -this.margin));

MatrixshaderMatrix=newMatrix();

shaderMatrix.setRectToRect(this.mBitmapRect, this.mRect,ScaleToFit.FILL);

this.bitmapShader.setLocalMatrix(shaderMatrix);

}

public voiddraw(Canvas canvas) {

canvas.drawCircle(this.mRect.centerX(), this.mRect.centerY(), this.mRect.width() /2.0F+ (float)this.margin, this.backPaint);

canvas.drawCircle(this.mRect.centerX(), this.mRect.centerY(), this.mRect.width() /2.0F, this.paint);

}

public intgetOpacity() {

return-3;

}

public voidsetAlpha(intalpha) {

this.paint.setAlpha(alpha);

}

public voidsetColorFilter(ColorFilter cf) {

this.paint.setColorFilter(cf);

}

}

}

第五步:配置displayoption,并设置imageview显示

RoundedBitmapWithBorderDisplayer mDoctorRoundDisplayer =newRoundedBitmapWithBorderDisplayer();

mDefaultRoundOption=newDisplayImageOptions.Builder()

.imageScaleType(ImageScaleType.EXACTLY)

.bitmapConfig(Bitmap.Config.RGB_565)

.cacheInMemory(true)

.cacheOnDisk(true)

.displayer(mDoctorRoundDisplayer)

.showImageOnLoading(R.drawable.touxiang)

.showImageOnFail(R.drawable.touxiang)

.build();

设置view显示

imageloader.displayImage(url,view,options)

你可能感兴趣的:(ImageLoader加载圆形、圆角、原图照片)