Android一行代碼實(shí)現(xiàn)圓形頭像
效果圖

在開(kāi)發(fā)APP中,經(jīng)常要實(shí)現(xiàn)圓形頭像,那么該如何實(shí)現(xiàn)呢?
要裁剪嗎,要重寫(xiě)draw函數(shù)嗎?不用,只用一行代碼就可以實(shí)現(xiàn)
Glide實(shí)現(xiàn)圓形圖像
Glide.with(mContext) .load(R.drawable.iv_image_header) .error(R.drawable.ic_error_default) .transform(new GlideCircleTransform(mContext)) .into(mImage);
其中l(wèi)oad后為載入的圖像,error后為出錯(cuò)時(shí)載入的圖像,transform是對(duì)其修改,我們也是通過(guò)這個(gè)GlideCirTransForm來(lái)修改的,使用的話要把mContext替換為你自己的activty,mImage為圖片載入的位置
使用之前的準(zhǔn)備
1.添加項(xiàng)目依賴
compile 'org.greenrobot:eventbus:3.0.0' compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'org.jetbrains:annotations-java5:15.0' compile 'in.srain.cube:ultra-ptr:1.0.11' compile 'com.wang.avi:library:1.0.5'
2.導(dǎo)入GlideCircleTransform.java文件
GlideCircleTransform.java代碼如下:
package com.sina.weibo.sdk.demo.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}
完成這兩步,你就可以使用那行代碼完成你自己的圓形頭像啦!
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
- Android圓形頭像拍照后“無(wú)法加載此圖片”的問(wèn)題解決方法(適配Android7.0)
- Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
- android dialog背景模糊化效果實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)個(gè)人資料頁(yè)面頭像背景模糊顯示包(狀態(tài)欄)
- Android中實(shí)現(xiàn)布局背景模糊化處理的方法
- Android實(shí)現(xiàn)用戶圓形頭像和模糊背景
相關(guān)文章
Android實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)的圖片頭像控件
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有動(dòng)態(tài)圓環(huán)的圓形圖片控件DynamicAvatarView的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android Flutter實(shí)現(xiàn)視頻上滑翻頁(yè)效果的示例代碼
我們?cè)诙桃曨l應(yīng)用中經(jīng)常會(huì)看到不停上滑瀏覽下一條視頻的沉浸式交互效果,這種交互能夠讓用戶不停地翻頁(yè),直到找到喜歡的視頻內(nèi)容。本文將通過(guò)Flutter中的PageView組件實(shí)現(xiàn),感興趣的可以了解一下2022-10-10
Android實(shí)現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變
這篇文章主要介紹了Android實(shí)現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
這篇文章主要介紹了Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了改變ExpandableListView的indicator圖標(biāo)相關(guān)步驟與實(shí)現(xiàn)技巧,涉及Android配置文件的修改,需要的朋友可以參考下2016-03-03
Android Studio里如何使用lambda表達(dá)式
這篇文章主要介紹了Android Studio里如何使用lambda表達(dá)式,需要的朋友可以參考下2017-05-05
Android仿新浪微博/QQ空間滑動(dòng)自動(dòng)播放視頻功能
相信用過(guò)新浪微博或者QQ空間的朋友都看到過(guò)滑動(dòng)自動(dòng)播放視頻的效果,那么這篇文章跟大家分享下如何利用Android實(shí)現(xiàn)這一個(gè)功能,有需要的朋友們可以參考借鑒。2016-09-09
Android實(shí)現(xiàn)無(wú)標(biāo)題欄全屏的方法
這篇文章主要介紹了Android實(shí)現(xiàn)無(wú)標(biāo)題欄全屏的三種方法,感興趣的小伙伴們可以參考一下2016-07-07
Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法
這篇文章主要介紹了Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法,實(shí)例分析了Android中使用libgdx框架實(shí)現(xiàn)響應(yīng)方向鍵的技巧,適用于Android游戲開(kāi)發(fā)領(lǐng)域,需要的朋友可以參考下2015-12-12

