【Android】自訂圓形ImageView(圓形頭像 可指定大小)

來源:互聯網
上載者:User

標籤:android ui   imageview   圓形頭像   

       最近在仿手Q的UI,這裡面經常要用到的就是圓形頭像,看到 在android中畫圓形圖片的幾種辦法 這篇文章,瞭解了製作這種頭像的原理.不過裡面提供的方法還有一個不足的地方就是不能根據實際需求改變圖片的大小,也就是說提供的原圖是大尺寸的,轉換之後的圖片也是大尺寸的,這顯然不符合我們實際項目中的需求.於是我對裡面介紹的第一種方法做了一番改進,使其能直接在XML中指定圖片的大小.大體步驟
  1. 將原圖置中裁剪成正方形
  2. 根據指定的寬度對正方形進行縮放
  3. 裁剪成圓形
效果

代碼實現
package com.demos.tencent_qq_ui.CustomerView;import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.util.AttributeSet; import android.widget.ImageView; /** * Created by mummyding on 15-8-7. */public class AvatarImageView extends ImageView {    private Paint paint = new Paint();    public AvatarImageView(Context context) {        super(context);    }    public AvatarImageView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public AvatarImageView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    //將頭像按比例縮放    private Bitmap scaleBitmap(Bitmap bitmap){        int width = getWidth();        //一定要強轉成float 不然有可能因為精度不夠 出現 scale為0 的錯誤        float scale = (float)width/(float)bitmap.getWidth();        Matrix matrix = new Matrix();        matrix.postScale(scale, scale);        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);    }    //將原始映像裁剪成正方形    private Bitmap dealRawBitmap(Bitmap bitmap){        int width = bitmap.getWidth();        int height = bitmap.getHeight();        //擷取寬度        int minWidth = width > height ?  height:width ;        //計算正方形的範圍        int leftTopX = (width - minWidth)/2;        int leftTopY = (height - minWidth)/2;        //裁剪成正方形        Bitmap newBitmap = Bitmap.createBitmap(bitmap,leftTopX,leftTopY,minWidth,minWidth,null,false);        return  scaleBitmap(newBitmap);    }    @Override    protected void onDraw(Canvas canvas) {        Drawable drawable = getDrawable();        if (null != drawable) {            Bitmap rawBitmap =((BitmapDrawable)drawable).getBitmap();            //處理Bitmap 轉成正方形            Bitmap newBitmap = dealRawBitmap(rawBitmap);            //將newBitmap 轉換成圓形            Bitmap circleBitmap = toRoundCorner(newBitmap, 14);            final Rect rect = new Rect(0, 0, circleBitmap.getWidth(), circleBitmap.getHeight());            paint.reset();            //繪製到畫布上            canvas.drawBitmap(circleBitmap, rect, rect, paint);        } else {            super.onDraw(canvas);        }    }    private Bitmap toRoundCorner(Bitmap bitmap, int pixels) {        //指定為 ARGB_4444 可以減小圖片大小        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);        Canvas canvas = new Canvas(output);        final int color = 0xff424242;        final Rect rect = new Rect(0, 0,bitmap.getWidth(), bitmap.getHeight());        paint.setAntiAlias(true);        canvas.drawARGB(0, 0, 0, 0);        paint.setColor(color);        int x = bitmap.getWidth();        canvas.drawCircle(x / 2, x / 2, x / 2, paint);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        canvas.drawBitmap(bitmap, rect, rect, paint);        return output;    }}


然後就可以直接像其他View一樣直接在XML中使用了.例如
 <com.demos.tencent_qq_ui.CustomerView.AvatarImageView       android:layout_width="96dip"  <!--可自行指定大小-->       android:layout_height="96dip"       android:layout_centerHorizontal="true"       android:src="@drawable/avatar"  <!--這裡放圖片-->       />

這樣,在介面上顯示的映像就是圓形的了.       不過我這裡還有一個不足的地方是沒有檢查原圖片的大小,如果你放一張高清的圖片,那就很可能crash 掉.對於這點,我目前的解決方案就是 Google 官方教程裡的 Loading Large Bitmaps Efficiently   但它的原理就是先不不把圖片載入進來,而是先根據原圖的大小計算縮放值,然後在根據縮放值讀取圖片(算是在讀取的時候進行壓縮吧....).但是這個方法需要提供視圖ID,也就是說要給AvatarImageView 指定一個ID,然後一個AvatarImageView 要對應一個視圖(我是這麼理解的) 這樣的話AvatarImageView就無法重用了.當項目中多處要用到這個的話就會很麻煩. 為此我在stackoverflow提了一個問,但是目前還沒有人回答T_T  How to make bitmap(set in src) compress?

【轉載請註明出處】

Author: MummyDing

出處:http://blog.csdn.net/u012560612/article/category/5651761




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

【Android】自訂圓形ImageView(圓形頭像 可指定大小)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.