カスタム円形画像Circure ImageView


転載は出典を明記してください.http://blog.csdn.net/u012975705/article/details/49584533
効果図
自定义圆形图片CircleImageView_第1张图片
具体的なコードの実装
カスタムImageView:Circure ImageView.java
package com.plusub.rentlandapp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.plusub.rentlandapp.util.CanvasUtil;

/** * Created by noyet on 2015/11/2. */
public class CircleImageView extends ImageView {

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        //    ,   Bitmap
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        if (null == b) {
            return;
        }
        //     32 ARGB  ,      
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        //       
        int width = getWidth();
        Bitmap roundBitmap = CanvasUtil.getCirCleBitmap(bitmap, width);
        //    
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }
}
Canvas Util.java
package com.plusub.rentlandapp.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

/** * Created by noyet on 2015/11/2. */
public class CanvasUtil {

    /** *        * @param bmp        * @param radius               * @return Bitmap */
    public static Bitmap getCirCleBitmap(Bitmap bmp, int radius) {
        Bitmap p;
        //          radius    ,     ,             radius   
        if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
            p = Bitmap.createScaledBitmap(bmp, radius, radius, false);
        } else {
            p = bmp;
        }
        //         
        Bitmap bitmap = Bitmap.createBitmap(p.getWidth(),
                p.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, p.getWidth(), p.getHeight());
        //          ,      
        paint.setAntiAlias(true);
        //       true,             Bitmap       ,    
        paint.setFilterBitmap(true);
        //   
        paint.setDither(true);
        //   
        canvas.drawARGB(0, 0, 0, 0);
        //     
        paint.setColor(Color.parseColor("#BAB399"));
        //      
        canvas.drawCircle(p.getWidth() / 2 + 0.7f, p.getHeight() / 2 + 0.7f,
                p.getWidth() / 2 + 0.1f, paint);
        //             ,               
        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(p, rect, rect, paint);
        return bitmap;
    }
}
Bitmap.creat ScareedBitmap方法紹介:
/**
     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
     * specified width and height are the same as the current width and height of 
     * the source bitmap, the source bitmap is returned and no new bitmap is
     * created.
     *
     * @param src       The source bitmap.//bitmap  
     * @param dstWidth  The new bitmap's desired width.//         * @param dstHeight The new bitmap's desired height.//        
     * @param filter    true if the source should be filtered.//         (    )
     * @return The new scaled bitmap or the source bitmap if no scaling is required.
     * @throws IllegalArgumentException if width is <= 0, or height is <= 0
     */
xmlファイル:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:paddingTop="10dp" android:orientation="vertical">

    <com.plusub.rentlandapp.view.CircleImageView  android:scaleType="fitCenter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_test" />
</LinearLayout>