Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圓角圖片實作:自訂任意幾個角顯示為圓角

編輯:關於 Android 程式設計

為什麼需要自訂圓角?

在 Android 裡替圖片加上圓角,方法其實不少,常見的有這幾種:

  • shape 背景
  • .9 圖(NinePatch)
  • XferMode
  • BitmapShader
  • ClipPath

前兩種做法比較直接粗暴,而 XferMode 與 BitmapShader 則是坊間最普遍採用的方案。

主流圖片庫的痛點

翻看目前熱門的圖片載入框架,無論是 Picasso、Glide、Fresco 還是 Android-Universal-Image-Loader,都能顯示圓角圖片。但除了 Fresco 能指定「哪幾個角圓、哪幾個角不圓」之外,其餘三者都做不到。而 Fresco 的問題在於侵入性偏強:版面配置裡得改用 com.facebook.drawee.view.SimpleDraweeView 而非單純的 ImageView,還要逐一為每個角設定 true/false,寫起來頗為繁瑣。

因此本文以 Universal-Image-Loader 的 RoundedBitmapDisplayer 為基礎進行擴充,自訂出一個 FlexibleRoundedBitmapDisplayer,讓你能用簡潔的程式碼自由指定哪些角要圓、哪些角不要圓。

先看原始碼:RoundedBitmapDisplayer

 public class RoundedBitmapDisplayer implements BitmapDisplayer { protected final int cornerRadius; protected final int margin; public RoundedBitmapDisplayer(int cornerRadiusPixels) { this(cornerRadiusPixels, 0); } public RoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) { this.cornerRadius = cornerRadiusPixels; this.margin = marginPixels; } public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) { if(!(imageAware instanceof ImageViewAware)) { throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected."); } else { imageAware.setImageDrawable(new RoundedBitmapDisplayer.RoundedDrawable(bitmap, this.cornerRadius, this.margin)); } } public static class RoundedDrawable extends Drawable { protected final float cornerRadius; protected final int margin; protected final RectF mRect = new RectF(); protected final RectF mBitmapRect; protected final BitmapShader bitmapShader; protected final Paint paint; public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) { this.cornerRadius = (float)cornerRadius; this.margin = margin; this.bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP); this.mBitmapRect = new RectF((float)margin, (float)margin, (float)(bitmap.getWidth() - margin), (float)(bitmap.getHeight() - margin)); this.paint = new Paint(); this.paint.setAntiAlias(true); this.paint.setShader(this.bitmapShader); this.paint.setFilterBitmap(true); this.paint.setDither(true); } protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); this.mRect.set((float)this.margin, (float)this.margin, (float)(bounds.width() - this.margin), (float)(bounds.height() - this.margin)); Matrix shaderMatrix = new Matrix(); shaderMatrix.setRectToRect(this.mBitmapRect, this.mRect, ScaleToFit.FILL); this.bitmapShader.setLocalMatrix(shaderMatrix); } public void draw(Canvas canvas) { canvas.drawRoundRect(this.mRect, this.cornerRadius, this.cornerRadius, this.paint); } public int getOpacity() { return -3; } public void setAlpha(int alpha) { this.paint.setAlpha(alpha); } public void setColorFilter(ColorFilter cf) { this.paint.setColorFilter(cf); } } }

原理解析:BitmapShader 是怎麼運作的

Universal-Image-Loader 採用的正是第四種做法——BitmapShader。Shader 又被稱為著色器或渲染器,BitmapShader 產生的是一個影像,概念上有點像 Photoshop 裡的漸層填色:它讓 Paint 得以用指定的 Bitmap 去填滿畫布。這裡的填滿模式是 TileMode.CLAMP(拉伸模式,會複製圖片最後一個像素點,通常在圖片尺寸合適時不會真的被拉伸)。

講白一點,就是用一張圖片打造出一支「帶有影像填充能力」的畫筆,再用這支畫筆去畫出一個形狀,圖片就會呈現在畫過的地方。Universal-Image-Loader 這裡用 drawRoundRect() 畫出圓角矩形,於是圖片就顯示成圓角矩形了。

用過 Photoshop 的人應該很好理解:這就像替圖層加了一塊白色遮罩,再用不透明度 100% 的黑色畫筆在遮罩上塗抹,畫筆掃過之處原本的圖層便會顯現出來,正是同樣的道理。

實作思路:如何只讓某幾個角圓

搞懂 RoundedBitmapDisplayer 的原理後,自訂圓角的思路就很清楚了:先用圓角矩形把整張圖變成圓角,接著針對「你不想圓的那個角」,把那個角落位置的原圖用矩形補畫回來即可,使用的正是 drawRect() 方法。

FlexibleRoundedBitmapDisplayer 完整程式碼

 /** * Universal-Image-Loader中RoundedBitmapDisplayer的增強版,可以自定義圖片4個角中的指定角為圓角 */ public class FlexibleRoundedBitmapDisplayer implements BitmapDisplayer { protected int cornerRadius; protected int corners; public static final int CORNER_TOP_LEFT = 1; public static final int CORNER_TOP_RIGHT = 1 << 1; public static final int CORNER_BOTTOM_LEFT = 1 << 2; public static final int CORNER_BOTTOM_RIGHT = 1 << 3; public static final int CORNER_ALL = CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT; /** * 構造方法說明:設定圓角像素大小,所有角都為圓角 * @param cornerRadiusPixels 圓角像素大小 */ public FlexibleRoundedBitmapDisplayer(int cornerRadiusPixels){ this.cornerRadius = cornerRadiusPixels; this.corners = CORNER_ALL; } /** * 構造方法說明:設定圓角像素大小,指定角為圓角 * @param cornerRadiusPixels 圓角像素大小 * @param corners 自定義圓角 * CORNER_NONE 無圓角 * CORNER_ALL 全為圓角 * CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT 指定圓角(選其中若干組合 ) */ public FlexibleRoundedBitmapDisplayer(int cornerRadiusPixels, int corners){ this.cornerRadius = cornerRadiusPixels; this.corners = corners; } @Override public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) { if (!(imageAware instanceof ImageViewAware)) { throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected."); } imageAware.setImageDrawable(new FlexibleRoundedDrawable(bitmap,cornerRadius,corners)); } public static class FlexibleRoundedDrawable extends Drawable { protected final float cornerRadius; protected final RectF mRect = new RectF(), mBitmapRect; protected final BitmapShader bitmapShader; protected final Paint paint; private int corners; public FlexibleRoundedDrawable(Bitmap bitmap, int cornerRadius, int corners) { this.cornerRadius = cornerRadius; this.corners = corners; bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mBitmapRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()); paint = new Paint(); paint.setAntiAlias(true); paint.setShader(bitmapShader); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRect.set(0, 0, bounds.width(), bounds.height()); Matrix shaderMatrix = new Matrix(); shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL); bitmapShader.setLocalMatrix(shaderMatrix); } @Override public void draw(Canvas canvas) { //先畫一個圓角矩形將圖片顯示為圓角 canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint); int notRoundedCorners = corners ^ CORNER_ALL; //哪個角不是圓角我再把你用矩形畫出來 if ((notRoundedCorners & CORNER_TOP_LEFT) != 0) { canvas.drawRect(0, 0, cornerRadius, cornerRadius, paint); } if ((notRoundedCorners & CORNER_TOP_RIGHT) != 0) { canvas.drawRect(mRect.right - cornerRadius, 0, mRect.right, cornerRadius, paint); } if ((notRoundedCorners & CORNER_BOTTOM_LEFT) != 0) { canvas.drawRect(0, mRect.bottom - cornerRadius, cornerRadius, mRect.bottom, paint); } if ((notRoundedCorners & CORNER_BOTTOM_RIGHT) != 0) { canvas.drawRect(mRect.right - cornerRadius, mRect.bottom - cornerRadius, mRect.right, mRect.bottom, paint); } } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } @Override public void setAlpha(int alpha) { paint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter cf) { paint.setColorFilter(cf); } } }

怎麼使用

寫好之後,就能這樣搭配 Universal-Image-Loader 使用了:

 /* * 設置圖片---自定義圖片4個角中的指定角為圓角 * @param url 圖片的url * @param cornerRadius 圓角像素大小 * @param corners 自定義圓角: * 以下參數為FlexibleRoundedBitmapDisplayer中靜態變量: * CORNER_NONE 無圓角 * CORNER_ALL 全為圓角 * CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT 指定圓角(選其中若干組合 ) * @param image url為空時加載該圖片 * @param imageView 要設置圖片的ImageView */ public void setRoundedImage(String url, int cornerRadius, int corners, int image, ImageView imageView) { ImageLoader imageLoader = ImageLoader.getInstance(); DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(image).showStubImage(image) .showImageForEmptyUri(image)//url為空時顯示的圖片 .showImageOnFail(image)//加載失敗顯示的圖片 .cacheInMemory()//內存緩存 .cacheOnDisc()//磁盤緩存 .displayer(new FlexibleRoundedBitmapDisplayer(cornerRadius,corners)) // 自定義增強型BitmapDisplayer .build(); imageLoader.displayImage(url, imageView, options); }

搭配方式如下:

實際運行效果:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved