記錄glide加載圖片,設置圓角

支持所有角的圓角,自動計算合適的半徑,不用擔心圖片比預定值小導致的圓角過大的問題

修改自:https://blog.csdn.net/qq_15059163/article/details/97613790
增加了指定圖片尺寸、解決了圖片某些情況下圓角過大的問題

public class GlideRoundCornersTransUtils implements Transformation<Bitmap> {private BitmapPool mBitmapPool;private int mRadius;//半徑private int mDiameter;//直徑private CornerType mCornerType = CornerType.ALL;private DisplayMetrics metrics;int width, height;public GlideRoundCornersTransUtils(Context context, int radius, CornerType type, int width, int height) {mBitmapPool = Glide.get(context).getBitmapPool();metrics = context.getResources().getDisplayMetrics();mRadius = (int) (radius * (metrics.densityDpi / 160f));mCornerType = type;mDiameter = 2 * mRadius;this.width = width;this.height = height;}public enum CornerType {/*** 所有角*/ALL,/*** 左上*/LEFT_TOP,/*** 左下*/LEFT_BOTTOM,/*** 右上*/RIGHT_TOP,/*** 右下*/RIGHT_BOTTOM,/*** 左側*/LEFT,/*** 右側*/RIGHT,/*** 下側*/BOTTOM,/*** 上側*/TOP,}@Overridepublic Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {Bitmap source = resource.get();// 剪裁圖片到指定的尺寸Bitmap croppedBitmap = cropBitmap(source, width, height);int width = croppedBitmap.getWidth();int height = croppedBitmap.getHeight();Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);if (bitmap == null) {bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);}Canvas canvas = new Canvas(bitmap);Paint paint = new Paint();paint.setAntiAlias(true);paint.setShader(new BitmapShader(croppedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));drawRoundRect(canvas, paint, width, height);return BitmapResource.obtain(bitmap, mBitmapPool);}private Bitmap cropBitmap(Bitmap source, int targetWidth, int targetHeight) {int sourceWidth = source.getWidth();int sourceHeight = source.getHeight();float srcRatio = (float) sourceWidth / sourceHeight;float targetRatio = (float) targetWidth / targetHeight;int width, height;if (srcRatio > targetRatio) {// 源圖片更寬,需要剪裁寬度width = (int) (sourceHeight * targetRatio);height = sourceHeight;} else {// 源圖片更高,需要剪裁高度width = sourceWidth;height = (int) (sourceWidth / targetRatio);}// 計算剪裁的起始點int x = (sourceWidth - width) / 2;int y = (sourceHeight - height) / 2;//計算當前適合的半徑、直徑,因為當前圖片的寬高有可能比預定值小,因此,半徑、直徑也要跟著縮小mRadius=mRadius*width/targetWidth;mDiameter=mDiameter*width/targetWidth;// 剪裁圖片return Bitmap.createBitmap(source, x, y, width, height);}private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {Log.e("測試", "全局: width:" + this.width + ",height:" + this.height + ",局部:width:" + width + ",height:" + height+",角度:"+mRadius);switch (mCornerType) {case LEFT_TOP:drawLeftTopCorner(canvas, paint, width, height);break;case LEFT_BOTTOM:drawLeftBottomCorner(canvas, paint, width, height);break;case RIGHT_TOP:drawRightTopCorner(canvas, paint, width, height);break;case RIGHT_BOTTOM:drawRightBottomCorner(canvas, paint, width, height);break;case LEFT:drawLeftCorner(canvas, paint, width, height);break;case RIGHT:drawRightCorner(canvas, paint, width, height);break;case BOTTOM:drawBottomCorner(canvas, paint, width, height);break;case TOP:drawTopCorner(canvas, paint, width, height);break;case ALL:default:canvas.drawRoundRect(new RectF(0, 0, width, height), mRadius, mRadius, paint);break;}}/*** 畫左上角*/private void drawLeftTopCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(mRadius, 0, width, height), paint);canvas.drawRect(new RectF(0, mRadius, mRadius, height), paint);canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);}/*** 畫左下角*/private void drawLeftBottomCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);canvas.drawRect(new RectF(mRadius, height - mRadius, width, height), paint);canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);}/*** 畫右上角*/private void drawRightTopCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(0, 0, width - mRadius, height), paint);canvas.drawRect(new RectF(width - mRadius, mRadius, width, height), paint);canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);}/*** 畫右下角*/private void drawRightBottomCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);canvas.drawRect(new RectF(0, height - mRadius, width - mRadius, height), paint);canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);}/*** 畫左 角*/private void drawLeftCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(mRadius, 0, width, height), paint);canvas.drawRect(new RectF(0, mRadius, mRadius, height - mRadius), paint);canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);}/*** 畫右角*/private void drawRightCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(0, 0, width - mRadius, height), paint);canvas.drawRect(new RectF(width - mRadius, mRadius, width, height - mRadius), paint);canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);}/*** 畫上 角*/private void drawTopCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(0, mRadius, width, height), paint);canvas.drawRect(new RectF(mRadius, 0, width - mRadius, mRadius), paint);canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);}/*** 畫下 角*/private void drawBottomCorner(Canvas canvas, Paint paint, float width, float height) {canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);canvas.drawRect(new RectF(mRadius, height - mRadius, width - mRadius, height), paint);canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);}@Overridepublic String getId() {return "RoundedTransformation(radius=" + mRadius + ", diameter=" + mDiameter + ")";}
}

調用

 view.post(()->{Glide.with(context).load(glideUrl).override(view.getWidth(),view.getHeight()).bitmapTransform( new GlideRoundCornersTransUtils(context,radius,CornerType.ALL,view.getWidth(),view.getHeight())).into(view);});

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/12928.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/12928.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/12928.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

先有JVM還是先有垃圾回收器?很多人弄混淆了

是先有垃圾回收器再有JVM呢&#xff0c;還是先有JVM再有垃圾回收器呢&#xff1f;或者是先有垃圾回收再有JVM呢&#xff1f;歷史上還真是垃圾回收更早面世&#xff0c;垃圾回收最早起源于1960年誕生的LISP語言&#xff0c;Java只是支持垃圾回收的其中一種。下面我們就來刨析刨析…

外賣系統的JWT實現登錄

1、什么是JWT jwt可以生成一個加密的token&#xff0c;作為用戶登錄的令牌&#xff0c;當用戶登陸成功之后&#xff0c;發放給客戶端。請求需要登錄的資源或者接口的時候&#xff0c;將token攜帶&#xff0c;后端驗證token是否合法。jwt有三部分組成&#xff1a; A&#xff1a;…

【特大喜訊】國內前33位持有PMI-RMP風險管理專業認證的學員分享~!

【學員背景】 沈陽某信息科技有限公司&#xff0c;從事企業采購供應鏈數字化轉型方向&#xff1b; 為企業提供有效的降本增效解決方案。 【學員順利拿證后期訪問】 問&#xff1a;學員您好&#xff0c;首先恭喜您順利拿到RMP證書&#xff0c;請問您在此次備考過程中&#xf…

抖店商品詳情API接口(產品參數|詳情圖)

抖店商品詳情API接口(產品參數|詳情圖) 參數僅供參考&#xff1a; {"code": 0,"msg": "調用成功","time": "1715763239","data": {"properties": [{"format": [{"message": [{&q…

C語言簡要(一)

總得讓她開心吧 helloworld #include <stdio.h>int main() {printf("hello world!\n");return 0; } 程序框架 #include <stdio.h> int main {return 0; }輸出 printf("hello world!\n"); "里面的內容叫做“字符串”&#xff0c;prin…

BUUCTF靶場[MISC]wireshark、被嗅探的流量、神秘龍卷風、另一個世界

[misc]wireshark 考點&#xff1a;流量、追蹤流 工具&#xff1a;wireshark 先看題目&#xff0c;管理員密碼 將下載的文件用wireshark打開&#xff0c;查找flag 點擊追蹤tcp流&#xff0c;開始挨個查看flag [misc]被嗅探的流量 考點&#xff1a;流量、追蹤流 工具&#xf…

武漢星起航:亞馬遜構建綜合性商業生態,賣家買家共享全球化紅利

在當今全球化日益加速的時代&#xff0c;亞馬遜不僅以其卓越的電商平臺服務全球消費者&#xff0c;更通過一系列前沿服務打造了一個綜合性的商業生態系統。在這個生態系統中&#xff0c;賣家能夠輕松拓展全球業務&#xff0c;買家則享受到了前所未有的購物體驗。亞馬遜以其獨特…

FreeRTOS【6】線程優先級

1.開發背景 基于上一篇指引&#xff0c;已經了解了線程的阻塞&#xff0c;這個篇章主要介紹線程優先級的影響 2.開發需求 設計實驗驗證高優先級會搶占低優先級線程 CPU 3.開發環境 window10 MDK STM32F429 FreeRTOS10.3.1 4.實現步驟 1&#xff09;創建測試線程&#xff…

測試之路 - 精準而優雅

引子 這幾年業內一直在做精準測試&#xff0c;大都使用工具 diff 代碼改動、分析代碼覆蓋率這些平臺集成的能力。 業務測試中&#xff0c;我們在技術設計和代碼實現的基礎上也做了一些精減和精準的測試實踐&#xff0c;通過深入測試有針對的設計 case&#xff0c;發現隱藏問題…

抖音小程序使用Vant

安裝 Vant 有針對小程序的版本&#xff0c;通過npm安裝&#xff1a; npm i vant/weapp -S --production構建 npm 安裝 Vant Weapp 后需要構建 NPM&#xff0c;在菜單的【工具】選項中選擇【構建 NPM】&#xff1a; 使用組件 抖音小程序和微信小程序還是有一些差別的&#x…

怎么把3d模型導出cad立面---模大獅模型網

在設計工作中&#xff0c;將3D模型導出到CAD軟件并生成立面圖是一項常見但關鍵的任務。這不僅有助于更好地展示設計方案&#xff0c;還能方便后續的工程制圖和施工。本文將介紹如何通過3ds Max軟件將3D模型導出到CAD軟件&#xff0c;并生成高質量的立面圖&#xff0c;為您提供實…

現貨正泰漏電小型斷路器NXB-32LE-C16 30MA1P+N原裝正品NXB-40L

品牌&#xff1a;CHNT/正泰 型號&#xff1a;NXBLE 額定電流&#xff1a;25A,16A,20A,40A,32A 漏電保護器類型&#xff1a;2P 產地&#xff1a;中國大陸 電壓&#xff1a;1000V及以下 極數&#xff1a;3P,4p,2P,1PN 電源方式&#xff1a;交流電 3C證書編號&#xff1a;…

大模型時代下的先行者:景聯文科技引領數據標注新時代

在大模型時代&#xff0c;數據標注不再是簡單的分類標注&#xff0c;而是一項融合了技術革新、專業技能、法律合規和精細化管理的綜合性任務&#xff0c;對推動AI技術的發展和落地應用具有重要意義。 景聯文科技作為AI基礎行業的數據供應商&#xff0c;可協助人工智能企業解決整…

easyx快速入門1

1.基本說明 EasyX 是針對 C 的圖形庫&#xff0c;可以幫助 C/C 初學者快速上手圖形和游戲編程。 比如&#xff0c;可以基于 EasyX 圖形庫很快的用幾何圖形畫一個房子&#xff0c;或者一輛移動的小車&#xff0c;可以編寫俄羅斯方塊、貪吃蛇、黑白棋等小游戲&#xff0c;可以練…

fl studio試用版文件保存無法打開??一個方法教你免費打開!

前言 當下&#xff0c;各款編曲軟件五花八門&#xff0c;而這其中最有聲譽的必為FL Studio莫屬 這個軟件呢國人習慣叫他水果&#xff0c;擁有強大的錄音、編曲、混音等功能&#xff0c;所以廣受音樂圈歡迎。如今&#xff0c;大部分水果一旦有編曲所需&#xff0c;一般都要使用…

【Python快速上手(二十三)】

目錄 Python快速上手&#xff08;二十三&#xff09;Python3 多線程1. 線程的創建2. 線程同步2.1 鎖&#xff08;Lock&#xff09;2.2 信號量&#xff08;Semaphore&#xff09;2.3 事件&#xff08;Event&#xff09;2.4 條件&#xff08;Condition&#xff09; 3. 線程優先級…

【Linux】Centos9設置ActiveMq開機自啟功能

配置流程&#xff1a; 1. 創建 Systemd 服務文件。這個文件通常存放在/usr/lib/systemd/system/目錄下&#xff0c;命名為 activemq.service。 #先創建文件&#xff0c;然后編輯&#xff1a; sudo touch /usr/lib/systemd/system/activemq.service sudo vim /usr/lib/systemd…

CSS 根據子元素選擇父元素,并設置父元素的樣式

場景舉例&#xff1a;當子元素有增加了一個class時&#xff0c;需要影響其父元素的樣式 可以使用":has"偽類來實現選擇父元素的效果 <style>.parent:has(.child){background-color: #eee;}p{width:100px;border:1px solid #000;} </style> <body>…

Python3 筆記:for語句和while語句的區別

一般來說&#xff0c;循環次數確定的問題使用for循環或者while循環都可以解決&#xff0c;而循環次數不確定的問題只能使用while循環解決。 for語句的格式&#xff1a; for 循環變量 in 遍歷對象: 語句 while語句的格式&#xff1a; while 條件表達式: 循環體 for…

人機協同中的比較、調整與反轉

人機協同是指人與機器之間的合作關系&#xff0c;通過共同努力實現特定任務的目標。在人機協同中&#xff0c;存在著比較與調整的過程&#xff0c;這是為了實現更好的合作效果和任務完成質量。 比較是指人與機器在任務執行過程中對彼此的表現進行評估和比較。這可以通過對機器的…