Android編程實現自定義ImageView圓圖功能的方法

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

建寧ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

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

今天項目遇到了需要實現圓頭像的編輯顯示,Universal就已經提供了這個顯示RoundedBitmapDisplayer這個類實現了圓圖功能??此拇a可以發(fā)現是實現的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);
    }
  }

其實總結下來,上面圓圖實現步驟就是:

1、通過bitmap初始化位圖著色器BitmapShader類
2、計算bitmap原始圖片的rect
3、計算放置圖片需要的rect
4、使用Matrix類對兩個rect進行壓縮,然后復制給BitmapShader著色器里去。最后是畫布畫圖。
(剛開始一直以為shader是陰影的意思,原來有道一下是著色器的意思,這個翻譯其實對我理解代碼還是很重要的,所以不要想當然,要勤奮點,這個是優(yōu)秀程序員必備要素。)

最后我要實現的是繼承ImageView實現圓圖

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會不斷調用,如果一直new對象的話會吃內存。所以為了避免重復new對象,根據自己的需求進行判空操作。具體根據自己需求來優(yōu)化代碼,有時候為了達到需求也沒辦法做到在ondraw方法里不出現重復new對象的現象。

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

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

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

分享題目:Android編程實現自定義ImageView圓圖功能的方法
轉載源于:http://muchs.cn/article30/jpehso.html

成都網站建設公司_創(chuàng)新互聯,為您提供定制開發(fā)網站內鏈、微信公眾號、App設計外貿網站建設、企業(yè)建站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

h5響應式網站建設