網站首頁 編程語言 正文
Fresco是FaceBook開源的Android平臺圖片加載庫,可以從網絡,從本地文件系統,本地資源加載圖片
Fresco本身已經實現了圓角以及圓形圖片的功能。
工作中,遇到圓形頭像的時候,UI通常會給我們這樣一張圖作為默認圖片
理論上來講,只需要加入下列這行代碼,就可以完成這部分工作了
app:placeholderImage="@drawable/ic_avatar_default"
然而圓形圖片本身已經是圓形的了,在有些機型上就出現了這個樣式。
搜索了一波,自帶的屬性都不能解決這個問題,干脆自己來定義這個圓形的實現吧,同時Fresco自帶的圓角效果只能保證使用統一的半徑,想要讓四個圓角的半徑不同,只能在java文件中設置,不夠靈活,定義圓角半徑的屬性也需要做些變更。
思路:自定義RoundImageView繼承自 SimpleDraweeVie,具備其所有的功能。
Canvas的clipPath(Path path)可以根據Path,將Canvas剪裁成我們想要的圖形。
public class RoundImageView extends SimpleDraweeView { ? ?? ? ? private final static int DEFAULT_VALUE = 0; ? ? private float mWidth; ? ? private float mHeight; ? ? private Path mPath; ? ? // 圓角角度 ? ? private float mCornerRadius; ? ? // 左上角圓角角度 ? ? private float mLeftTopRadius; ? ? // 右上角圓角角度 ? ? private float mRightTopRadius; ? ? // 右下角圓角角度 ? ? private float mRightBottomRadius; ? ? // 左下角圓角角度 ? ? private float mLeftBottomRadius; ? ? // 是否使用圓形圖片 ? ? private boolean mAsCircle; ? ? // 圓形圖片半徑 ? ? private float mRadius; ? ?? ? ? public RoundImageView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public RoundImageView(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? initData(); ? ? ? ? initAttrs(context, attrs); ? ? } ? ?? ? ? private void initData() { ? ? ? ? mPath = new Path(); ? ? } ? ? private void initAttrs(Context context, AttributeSet attrs) { ? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView); ? ? ? ? mCornerRadius = typedArray.getDimension(R.styleable.RoundImageView_cornerRadius, DEFAULT_VALUE); ? ? ? ? mAsCircle = typedArray.getBoolean(R.styleable.RoundImageView_asCircle, false); ? ? ? ? if (mCornerRadius <= 0) { ? ? ? ? ? ? // 說明用戶沒有設置四個圓角的有效值,此時四個圓角各自使用自己的值 ? ? ? ? ? ? mLeftTopRadius = typedArray.getDimension(R.styleable.RoundImageView_leftTopRadius, DEFAULT_VALUE); ? ? ? ? ? ? mRightTopRadius = typedArray.getDimension(R.styleable.RoundImageView_rightTopRadius, DEFAULT_VALUE); ? ? ? ? ? ? mRightBottomRadius = typedArray.getDimension(R.styleable.RoundImageView_rightBottomRadius, DEFAULT_VALUE); ? ? ? ? ? ? mLeftBottomRadius = typedArray.getDimension(R.styleable.RoundImageView_leftBottomRadius, DEFAULT_VALUE); ? ? ? ? } else { ? ? ? ? ? ? // 使用了統一的圓角,因此使用mCornerRadius統一的值 ? ? ? ? ? ? mLeftTopRadius = mCornerRadius; ? ? ? ? ? ? mRightTopRadius = mCornerRadius; ? ? ? ? ? ? mRightBottomRadius = mCornerRadius; ? ? ? ? ? ? mLeftBottomRadius = mCornerRadius; ? ? ? ? } ? ? ? ?? ? ? ? ? typedArray.recycle(); ? ? } ? ? @Override ? ? protected void onLayout(boolean changed, int left, int top, int right, int bottom) { ? ? ? ? super.onLayout(changed, left, top, right, bottom); ? ? ? ? mWidth = getWidth(); ? ? ? ? mHeight = getHeight(); ? ? ? ? // 如果開啟了圓形標記 ? ? ? ? if (mAsCircle) { ? ? ? ? ? ? mRadius = Math.min(mWidth / 2, mHeight / 2); ? ? ? ? } ? ? } ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? // 如果開啟了圓形標記,圓形圖片的優先級高于圓角圖片 ? ? ? ? if(mAsCircle) { ? ? ? ? ? ? drawCircleImage(canvas); ? ? ? ? } else { ? ? ? ? ? ? drawCornerImage(canvas); ? ? ? ? } ? ? ? ? super.onDraw(canvas); ? ? } ? ? /** ? ? ?* 畫中間圓形 ? ? ?* @param canvas ? ? ?*/ ? ? private void drawCircleImage(Canvas canvas) { ? ? ? ? mPath.addCircle(mWidth / 2, mHeight / 2, mRadius, Path.Direction.CW); ? ? ? ? canvas.clipPath(mPath); ? ? } ? ? /** ? ? ?* 畫圓角 ? ? ?* @param canvas ? ? ?*/ ? ? private void drawCornerImage(Canvas canvas) { ? ? ? ? if (mWidth > mCornerRadius && mHeight > mCornerRadius) { ? ? ? ? ? ? // 設置四個角的x,y半徑值 ? ? ? ? ? ? float[] radius = {mLeftTopRadius, mLeftTopRadius, mRightTopRadius, mRightTopRadius, mRightBottomRadius, mRightBottomRadius, mLeftBottomRadius, mLeftBottomRadius}; ? ? ? ? ? ? mPath.addRoundRect(new RectF(0,0, mWidth, mHeight), radius, Path.Direction.CW); ? ? ? ? ? ? canvas.clipPath(mPath); ? ? ? ? } ? ? } }
attr屬性如下
? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
原文鏈接:https://blog.csdn.net/YX_BB/article/details/104561359
相關推薦
- 2022-03-29 深入了解Python并發編程_python
- 2022-07-18 iptables防火墻
- 2023-01-26 使用Jedis面臨的非線程安全問題詳解_Redis
- 2022-03-15 跨域報錯:Response to preflight request doesn‘t pass ac
- 2023-03-01 多語言切換在Androidx失效的踩坑解決記錄_Android
- 2022-10-10 C++?STL標準庫std::vector擴容時進行深復制原因詳解_C 語言
- 2022-07-08 一文詳解Python中生成器的原理與使用_python
- 2022-07-04 教你在容器中使用nginx搭建上傳下載的文件服務器_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支