十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
怎么在Andorid中通過URL獲取用戶頭像?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1.設(shè)置布局屬性:
2.BitmapUtils類-- 得到指定圓形的Bitmap對象
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)建一個畫布 Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); //在畫布上畫一個圓 canvas.drawCircle(width / 2, width / 2, width / 2, paint); //設(shè)置圖片相交情況下的處理方式 //setXfermode:設(shè)置當(dāng)繪制的圖像出現(xiàn)相交情況時候的處理方式的,它包含的常用模式有: //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對象 //壓縮處理 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。否則報錯 } }).into(ivMeIcon);
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。