本文實例講述了Android編程實現(xiàn)自定義ImageView圓圖功能的方法。分享給大家供大家參考,具體如下:
建寧ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
首先很感謝開源項目Universal Image Loader圖片加載框架。之前也看過一段時間框架源碼,但是卻沒有時間進(jìn)行知識點的總結(jié)。
今天項目遇到了需要實現(xiàn)圓頭像的編輯顯示,Universal就已經(jīng)提供了這個顯示RoundedBitmapDisplayer這個類實現(xiàn)了圓圖功能??此拇a可以發(fā)現(xiàn)是實現(xiàn)的Drawable
public static class RoundedDrawable extends Drawable { protected final float cornerRadius; protected final int margin; protected final RectF mRect = new RectF(), mBitmapRect; protected final BitmapShader bitmapShader; protected final Paint paint; public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) { this.cornerRadius = cornerRadius; this.margin = margin; bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin); paint = new Paint(); paint.setAntiAlias(true); paint.setShader(bitmapShader); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin); // Resize the original bitmap to fit the new bound 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); } @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); } }
其實總結(jié)下來,上面圓圖實現(xiàn)步驟就是:
1、通過bitmap初始化位圖著色器BitmapShader類
2、計算bitmap原始圖片的rect
3、計算放置圖片需要的rect
4、使用Matrix類對兩個rect進(jìn)行壓縮,然后復(fù)制給BitmapShader著色器里去。最后是畫布畫圖。
(剛開始一直以為shader是陰影的意思,原來有道一下是著色器的意思,這個翻譯其實對我理解代碼還是很重要的,所以不要想當(dāng)然,要勤奮點,這個是優(yōu)秀程序員必備要素。)
最后我要實現(xiàn)的是繼承ImageView實現(xiàn)圓圖
public class URoundedImageView extends ImageView { private Paint mBitmapPaint,mBackgroundPaint; private BitmapShader mBitmapShader; private RectF mBitmapRect , mRect; private int borderWidth; private Bitmap mBitmap; private Matrix shaderMatrix; public URoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public URoundedImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public URoundedImageView(Context context) { super(context); init(); } private void init(){ mBitmapPaint = new Paint(); mBitmapPaint.setAntiAlias(true); mBackgroundPaint = new Paint(); mBackgroundPaint.setAntiAlias(true); mBackgroundPaint.setColor(Color.WHITE); borderWidth = 5; mRect = new RectF(); shaderMatrix = new Matrix(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // TODO Auto-generated method stub super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { mBitmap = ((BitmapDrawable) getDrawable()).getBitmap(); if (getWidth() == 0 || getHeight() == 0 || mBitmap == null) { return; } int w = getWidth(); int h = getHeight(); int radius = Math.min(w, h) / 2; canvas.drawCircle(w / 2, h / 2, radius, mBackgroundPaint); //傳入bitmap初始化位圖著色器 if (mBitmapShader == null) { mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); } if (mBitmapRect == null) { mBitmapRect = new RectF(borderWidth, borderWidth, mBitmap.getWidth() - borderWidth, mBitmap.getHeight() - borderWidth); } mBitmapPaint.setShader(mBitmapShader); mRect.set(borderWidth, borderWidth, w - borderWidth, h - borderWidth); //對bitmap原始圖進(jìn)行縮放 shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL); mBitmapShader.setLocalMatrix(shaderMatrix); canvas.drawRoundRect(mRect, radius, radius, mBitmapPaint); } }
剛開始寫的不夠規(guī)范,直接在ondraw方法里面new一些需要的對象,lint提醒我們Avoid object allocations during draw/layout operations (preallocate and reuse instead)這個warning。因為ondraw會不斷調(diào)用,如果一直new對象的話會吃內(nèi)存。所以為了避免重復(fù)new對象,根據(jù)自己的需求進(jìn)行判空操作。具體根據(jù)自己需求來優(yōu)化代碼,有時候為了達(dá)到需求也沒辦法做到在ondraw方法里不出現(xiàn)重復(fù)new對象的現(xiàn)象。
總結(jié):多參考優(yōu)秀的開源項目,用正確的方法做正確的事情!
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。