自定义圆形图片CircleImageView

来源:互联网 发布:南风知我意2 txt下载 编辑:程序博客网 时间:2024/05/24 06:27

转载请注明出处:http://blog.csdn.net/u012975705/article/details/49584533

效果图

这里写图片描述

具体代码实现

自定义的ImageView:CircleImageView.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);    }}

CanvasUtil.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.createScaledBitmap方法介绍:

/**     * 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>
1 0
原创粉丝点击