怎么在Andorid中通過URL獲取用戶頭像?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

1.設(shè)置布局屬性:
<ImageView android:scaleType="fitXY"/>
2.BitmapUtils類-- 得到指定圓形的Bitmap對(duì)象
public static Bitmap circleBitmap(Bitmap source) {
//獲取Bitmap的寬度
int width = source.getWidth();
//以Bitmap的寬度值作為新的bitmap的寬高值。
Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
//以此bitmap為基準(zhǔn),創(chuàng)建一個(gè)畫布
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
//在畫布上畫一個(gè)圓
canvas.drawCircle(width / 2, width / 2, width / 2, paint);
//設(shè)置圖片相交情況下的處理方式
//setXfermode:設(shè)置當(dāng)繪制的圖像出現(xiàn)相交情況時(shí)候的處理方式的,它包含的常用模式有:
//PorterDuff.Mode.SRC_IN 取兩層圖像交集部分,只顯示上層圖像
//PorterDuff.Mode.DST_IN 取兩層圖像交集部分,只顯示下層圖像
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//在畫布上繪制bitmap
canvas.drawBitmap(source, 0, 0, paint);
return bitmap;
}3.BitmapUtils類--壓縮圖片
//實(shí)現(xiàn)圖片的壓縮處理
//設(shè)置寬高必須使用浮點(diǎn)型,否則導(dǎo)致壓縮的比例:0
public static Bitmap zoom(Bitmap source,float width ,float height){
Matrix matrix = new Matrix();
//圖片的壓縮處理
matrix.postScale(width / source.getWidth(),height / source.getHeight());
Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);
return bitmap;
}4.根據(jù)user.getImageurl()顯示圓形圖像
//使用Picasso聯(lián)網(wǎng)獲取圖片
Picasso.with(this.getActivity()).load(user.getImageurl()).transform(new Transformation() {
@Override
public Bitmap transform(Bitmap source) {//下載以后的內(nèi)存中的bitmap對(duì)象
//壓縮處理
Bitmap bitmap = BitmapUtils.zoom(source, UIUtils.dp2px(62),UIUtils.dp2px(62));
//圓形處理
bitmap = BitmapUtils.circleBitmap(bitmap);
//回收bitmap資源
source.recycle();
return bitmap;
}
@Override
public String key() {
return "";//需要保證返回值不能為null。否則報(bào)錯(cuò)
}
}).into(ivMeIcon);看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享文章:怎么在Andorid中通過URL獲取用戶頭像-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://chinadenli.net/article42/cepcec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信小程序、Google、用戶體驗(yàn)、網(wǎng)站改版、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)