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)修改的,使用的話(huà)要把mContext替換為你自己的activty,mImage為圖片載入的位置
使用之前的準(zhǔn)備
1.添加項(xiàng)目依賴(lài)
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)用戶(hù)圓形頭像和模糊背景
相關(guān)文章
Android 完全退出當(dāng)前應(yīng)用程序的四種方法
Android程序有很多Activity,比如說(shuō)主窗口A,調(diào)用了子窗口B,如果在B中直接finish(), 接下里顯示的是A。在B中如何關(guān)閉整個(gè)Android應(yīng)用程序呢?本人總結(jié)了幾種比較簡(jiǎn)單的實(shí)現(xiàn)方法2016-02-02
flutter實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了flutter發(fā)送驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android?AMS啟動(dòng)App進(jìn)程原理分析
這篇文章主要介紹了Android?AMS啟動(dòng)App進(jìn)程原理,系統(tǒng)fork函數(shù)是如何創(chuàng)建進(jìn)程,文中有詳細(xì)的代碼示例,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-05-05
Android?ASM插樁探索實(shí)戰(zhàn)詳情
這篇文章主要介紹了Android?ASM插樁探索實(shí)戰(zhàn)詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
如何在Android中實(shí)現(xiàn)左右滑動(dòng)的指引效果
本篇文章是對(duì)在Android中實(shí)現(xiàn)左右滑動(dòng)指引效果的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
詳解Android開(kāi)發(fā)技巧之PagerAdapter實(shí)現(xiàn)類(lèi)的封裝
這篇文章主要介紹了詳解Android開(kāi)發(fā)技巧之PagerAdapter實(shí)現(xiàn)類(lèi)的封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11

