1. 程式人生 > >自定義圓角帶邊框ImageView

自定義圓角帶邊框ImageView

最近專案改版需要帶陰影的圓角矩形,最後只實現了帶邊框的圓角矩形,陰影需要漸變,由於寶寶經驗有限,只能參考被人的實現成了這樣的。有路過的大神可以留下腳印,嘿嘿

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import
android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; import com.trthi.mall.R; /** * 圓角帶邊框矩形ImageView */ public class RectangleView extends
ImageView {
private Paint paint; private Bitmap sbmp; private float left; private float top; private float right; private float bottom; public RectangleView(Context context) { this(context, null); } public RectangleView(Context context, AttributeSet attrs) { this
(context, attrs,0); } public RectangleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); } protected void onDraw(Canvas canvas) { int roundPx = 10;//圓角的大小,這個單位是畫素 paint.setAntiAlias(true); paint.setColor(getResources().getColor(R.color.text_color_0c66666)); //這裡的顏色決定了邊緣的顏色 Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } if(drawable instanceof BitmapDrawable){ Bitmap b = ((BitmapDrawable) drawable).getBitmap(); Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(); int h = getHeight(); RectF rectF = new RectF(0, 0, w, h); Bitmap roundBitmap = getCroppedBitmap(bitmap, w, h,roundPx); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); canvas.drawBitmap(roundBitmap, 0, 0, null); } } public Bitmap getCroppedBitmap(Bitmap bmp, int lengthx, int lengthy,int roundPx) { if (bmp.getWidth() != lengthx || bmp.getHeight() != lengthy) sbmp = Bitmap.createScaledBitmap(bmp, lengthx, lengthy, false); else sbmp = bmp; Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); //left、right -:向右 +:向左 top、bottom -:向下 +:向上 final RectF rectF = new RectF(left,top,sbmp.getWidth()+right,sbmp.getHeight()+bottom); // final RectF rectF = new RectF(-3, 0, sbmp.getWidth()+3, sbmp.getHeight() - 2); final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(sbmp, rect, rect, paint); return output; } //這個方法是通過外部來控制邊框的大小 public void setRectFSize(float left,float top,float right,float bottom){ this.left = left; this.top = top; this.right = right; this.bottom = bottom; } }

這裡需要說明一點,給這個自定義的ImageView設定background是不起作用的,這個不也不知道為啥,所以就想到給他加一個父佈局(帶陰影的)來解決帶陰影的圓角矩形。
我的建議,直接讓設計給圓角帶陰影的矩形最好,原因:一本身不用重寫任何控制元件,減少了APP檔案的大小;二節省了開發時間。但是最好還是要一起溝通和看具體需求了嘿嘿