国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中使用Bitmap類將矩形圖片轉為圓形的方法

2020-01-02 06:59:47
字體:
來源:轉載
供稿:網友

一般要做正圓形圖片,只能是正方形的基礎上才能實現,否則就變成橢圓了,下面說說如何使長方形的圖片生成正圓形圖片
廢話不多說,沒圖沒真相,先上圖吧:
原圖:

2016319163739626.jpg (480×800)

變成正圓后:

2016319163810889.jpg (480×800)

下面上代碼:

public static Bitmap makeRoundCorner(Bitmap bitmap) {   int width = bitmap.getWidth();   int height = bitmap.getHeight();   int left = 0, top = 0, right = width, bottom = height;   float roundPx = height/2;   if (width > height) {     left = (width - height)/2;     top = 0;     right = left + height;     bottom = height;   } else if (height > width) {     left = 0;     top = (height - width)/2;     right = width;     bottom = top + width;     roundPx = width/2;   }   ZLog.i(TAG, "ps:"+ left +", "+ top +", "+ right +", "+ bottom);    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);   Canvas canvas = new Canvas(output);   int color = 0xff424242;   Paint paint = new Paint();   Rect rect = new Rect(left, top, right, bottom);   RectF rectF = new RectF(rect);    paint.setAntiAlias(true);   canvas.drawARGB(0, 0, 0, 0);   paint.setColor(color);   canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));   canvas.drawBitmap(bitmap, rect, rect, paint);   return output; } 


下面再解釋下:
由于圖片是長方形,所以圖片的 寬、高 肯定會有一邊要小于另一邊,要生成正方形就已最小的一邊為基準,再來裁切定位另一邊的顯示范圍
至于圓角的半徑則是正方形寬的一半,用過 CSS 的就知道,畫圓很方便 border-radius設為 50% 就可以了,都是一個道理
android 的 UI 真是太繁瑣了

矩形畫個圓角的代碼:

public static Bitmap makeRoundCorner(Bitmap bitmap, int px) {   int width = bitmap.getWidth();   int height = bitmap.getHeight();   Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);   Canvas canvas = new Canvas(output);   int color = 0xff424242;   Paint paint = new Paint();   Rect rect = new Rect(0, 0, width, height);   RectF rectF = new RectF(rect);   paint.setAntiAlias(true);   canvas.drawARGB(0, 0, 0, 0);   paint.setColor(color);   canvas.drawRoundRect(rectF, px, px, paint);   paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));   canvas.drawBitmap(bitmap, rect, rect, paint);   return output; } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肥乡县| 河曲县| 青河县| 靖西县| 荆门市| 台北县| 乐至县| 珲春市| 揭西县| 宝兴县| 宁津县| 漠河县| 特克斯县| 敦化市| 台南市| 衡阳县| 湖口县| 永年县| 阿坝县| 新平| 株洲县| 石河子市| 长海县| 宣恩县| 收藏| 昌邑市| 隆昌县| 论坛| 瓮安县| 基隆市| 潮安县| 郯城县| 太仓市| 拉萨市| 福海县| 洛宁县| 遵义市| 扶沟县| 北流市| 新干县| 莲花县|