怎么在Andorid中通過URL獲取用戶頭像?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
1.設置布局屬性:
<ImageView android:scaleType="fitXY"/>
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為基準,創(chuàng)建一個畫布 Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); //在畫布上畫一個圓 canvas.drawCircle(width / 2, width / 2, width / 2, paint); //設置圖片相交情況下的處理方式 //setXfermode:設置當繪制的圖像出現(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類--壓縮圖片
//實現(xiàn)圖片的壓縮處理 //設置寬高必須使用浮點型,否則導致壓縮的比例: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)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
分享文章:怎么在Andorid中通過URL獲取用戶頭像-創(chuàng)新互聯(lián)
文章轉載:http://www.rwnh.cn/article42/cepcec.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信小程序、Google、用戶體驗、網(wǎng)站改版、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容