ImageLoader加載圓形巷帝、圓角、原圖照片

開(kāi)源項(xiàng)目Universal-Image-Loader功能很強(qiáng)大扫夜,是很好的個(gè)開(kāi)源項(xiàng)目楞泼,下面就針對(duì)Imageloader加載圓形驰徊、圓角、和原圖照片做詳細(xì)講解堕阔。

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

顯示圓形圖片方案有兩種:

1棍厂、通過(guò)自定義一個(gè)圓形的ImageView,然后調(diào)用imageloader? displayImage()方法超陆,這種方法缺點(diǎn)是沒(méi)有緩存

2牺弹、使用ImageLoader的DisplayImageOptions.Builder().displayer(mDoctorRoundDisplayer) 方法,這種方式會(huì)緩存时呀,今天就主要來(lái)針對(duì)第二種方法做詳細(xì)講解

第一步:加入庫(kù)

下載universal-image-loader:1.9.3.jar 然后在gradle文件中引入或者通過(guò)maven 直接在gradle文件中聲明:compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'张漂,imageloader最小支 持 版本android4.1,否則會(huì)報(bào)錯(cuò) 谨娜,在gradle文件中聲明?minSdkVersion16

第二步:配置清單文件權(quán)限

<uses-permission android:name="android.permission.WRITE_EXTERAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTRAL_STORAGE"/>

<uses-permission android:name="android.permission.INTERNET"/>

第三步:在Application中配置ImageLoaderConfiguration參數(shù)并初始化

mDefaultImageOpions=newDisplayImageOptions.Builder()

.imageScaleType(ImageScaleType.EXACTLY)

.bitmapConfig(Bitmap.Config.RGB_565)

.cacheInMemory(true)

.cacheOnDisk(true)

.build();

//設(shè)置為0會(huì)啟用默認(rèn)的memory大小航攒,為1/8的可用內(nèi)存控件

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,并設(shè)置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();

設(shè)置view顯示

imageloader.displayImage(url,view,options)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末趴梢,一起剝皮案震驚了整個(gè)濱河市漠畜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌坞靶,老刑警劉巖憔狞,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異彰阴,居然都是意外死亡瘾敢,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門硝枉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)廉丽,“玉大人,你說(shuō)我怎么就攤上這事妻味≌梗” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵责球,是天一觀的道長(zhǎng)焦履。 經(jīng)常有香客問(wèn)我,道長(zhǎng)雏逾,這世上最難降的妖魔是什么嘉裤? 我笑而不...
    開(kāi)封第一講書人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮栖博,結(jié)果婚禮上屑宠,老公的妹妹穿的比我還像新娘。我一直安慰自己仇让,他們只是感情好典奉,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布躺翻。 她就那樣靜靜地躺著,像睡著了一般卫玖。 火紅的嫁衣襯著肌膚如雪公你。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 50,050評(píng)論 1 291
  • 那天假瞬,我揣著相機(jī)與錄音陕靠,去河邊找鬼。 笑死脱茉,一個(gè)胖子當(dāng)著我的面吹牛剪芥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播琴许,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼粗俱,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了虚吟?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤签财,失蹤者是張志新(化名)和其女友劉穎串慰,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體唱蒸,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡邦鲫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了神汹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片庆捺。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖屁魏,靈堂內(nèi)的尸體忽然破棺而出滔以,到底是詐尸還是另有隱情,我是刑警寧澤氓拼,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布你画,位于F島的核電站,受9級(jí)特大地震影響桃漾,放射性物質(zhì)發(fā)生泄漏坏匪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一撬统、第九天 我趴在偏房一處隱蔽的房頂上張望适滓。 院中可真熱鬧,春花似錦恋追、人聲如沸凭迹。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蕊苗。三九已至沿后,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間朽砰,已是汗流浹背尖滚。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留瞧柔,地道東北人漆弄。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像造锅,于是被迫代替她去往敵國(guó)和親撼唾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • 關(guān)于網(wǎng)絡(luò)加載已經(jīng)寫完了哥蔚,今天來(lái)給大家分享一下關(guān)于圖像加載的知識(shí)倒谷,在開(kāi)發(fā)中除了請(qǐng)求數(shù)據(jù)怎么顯示之外,剩下的 最...
    deyson閱讀 893評(píng)論 0 3
  • Android中使用圖形處理引擎糙箍,2D部分是android SDK內(nèi)部自己提供渤愁,3D部分是用Open GL ES ...
    溫暖的外星閱讀 3,163評(píng)論 2 10
  • 1. 圖像_UIL 主頁(yè): https://github.com/nostra13/Android-Univers...
    麋鹿原閱讀 1,585評(píng)論 0 5
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,376評(píng)論 0 17
  • 很多時(shí)候他們都在找對(duì)的人,可是卻很少有人想要努力把自己變成那個(gè)對(duì)的人质蕉,然后遇見(jiàn)幸福呢撞。 卑微到塵埃里花不一定能結(jié)出果...
    洛織閱讀 344評(píng)論 0 3