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

首頁 > 系統(tǒng) > Android > 正文

Android編程實現(xiàn)自定義ImageView圓圖功能的方法

2019-12-12 02:16:22
字體:
供稿:網(wǎng)友

本文實例講述了Android編程實現(xiàn)自定義ImageView圓圖功能的方法。分享給大家供大家參考,具體如下:

首先很感謝開源項目Universal Image Loader圖片加載框架。之前也看過一段時間框架源碼,但是卻沒有時間進行知識點的總結。

今天項目遇到了需要實現(xiàn)圓頭像的編輯顯示,Universal就已經(jīng)提供了這個顯示RoundedBitmapDisplayer這個類實現(xiàn)了圓圖功能。看它的代碼可以發(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);    }  }

其實總結下來,上面圓圖實現(xiàn)步驟就是:

1、通過bitmap初始化位圖著色器BitmapShader類
2、計算bitmap原始圖片的rect
3、計算放置圖片需要的rect
4、使用Matrix類對兩個rect進行壓縮,然后復制給BitmapShader著色器里去。最后是畫布畫圖。
(剛開始一直以為shader是陰影的意思,原來有道一下是著色器的意思,這個翻譯其實對我理解代碼還是很重要的,所以不要想當然,要勤奮點,這個是優(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原始圖進行縮放    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)存。所以為了避免重復new對象,根據(jù)自己的需求進行判空操作。具體根據(jù)自己需求來優(yōu)化代碼,有時候為了達到需求也沒辦法做到在ondraw方法里不出現(xiàn)重復new對象的現(xiàn)象。

總結:多參考優(yōu)秀的開源項目,用正確的方法做正確的事情!

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 六安市| 万宁市| 醴陵市| 灵川县| 临泽县| 涿州市| 富锦市| 太保市| 井冈山市| 凌源市| 许昌市| 年辖:市辖区| 阜城县| 靖边县| 荔波县| 和平县| 赤壁市| 隆回县| 朝阳县| 南部县| 扶沟县| 保山市| 海晏县| 南乐县| SHOW| 芦溪县| 嵩明县| 诸暨市| 东台市| 吉木萨尔县| 宁晋县| 乐东| 密云县| 青冈县| 嘉定区| 保定市| 凤阳县| 武宁县| 瑞昌市| 巢湖市| 昌乐县|