自定义ImageView实现圆角

package com.electric.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;

import com.electric.R;

public class RoundImageView extends ImageView{

/** 图片的类型,圆形或者圆角 **/
private int type;
private static final int TYPE_CIRCLE = 0;
private static final int TYPE_ROUND = 1;
/** 圆角的默认值 **/
private static final int BODER_RADIUS_DEFAULT = 10;
private int mBorderRadius;
private Paint mBitmapPaint;
/** 圆角的半径 **/
private int mRadius;
private Matrix mMatrix;
private BitmapShader mBitmapShader;
private int mWidth;
private RectF mRoundRect;

public RoundImageView(Context context) {
    super(context);
    init(context, null);
}

public RoundImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}
private void init(Context context, AttributeSet attrs){
    mMatrix = new Matrix();
    mBitmapPaint = new Paint();
    mBitmapPaint.setAntiAlias(true);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.RoundImageView);
    mBorderRadius = a.getDimensionPixelSize(
            R.styleable.RoundImageView_borderRadius, (int) TypedValue
                    .applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                            BODER_RADIUS_DEFAULT, getResources()
                                    .getDisplayMetrics()));
    type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle
    a.recycle();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // 圆角图片的范围
    if (type == TYPE_ROUND)
        mRoundRect = new RectF(0, 0, getWidth(), getHeight());
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (type == TYPE_CIRCLE) {
        mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = mWidth / 2;
        setMeasuredDimension(mWidth, mWidth);
    }

}

@Override
protected void onDraw(Canvas canvas) {
    if (getDrawable() == null) {
        return;
    }
    setUpShader();
    if (type == TYPE_ROUND) {
        canvas.drawRoundRect(mRoundRect, mBorderRadius, mBorderRadius,
                mBitmapPaint);
    } else {
        canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint);
    }
}
private void setUpShader() {
    Drawable drawable = getDrawable();
    if (drawable == null) {
        return;
    }

    Bitmap bmp = drawableToBitamp(drawable);
    // 将bmp作为着色器,就是在指定区域内绘制bmp
    mBitmapShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    float scale = 1.0f;
    if (type == TYPE_CIRCLE) {
        // 拿到bitmap宽或高的小值
        int bSize = Math.min(bmp.getWidth(), bmp.getHeight());
        scale = mWidth * 1.0f / bSize;
    } else if (type == TYPE_ROUND) {
        // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,
        一定要大于我们view的宽高;所以我们这里取大值;
        scale = Math.max(getWidth() * 1.0f / bmp.getWidth(), getHeight()
                * 1.0f / bmp.getHeight());
    }
    // shader的变换矩阵,我们这里主要用于放大或者缩小
    mMatrix.setScale(scale, scale);
    // 设置变换矩阵
    mBitmapShader.setLocalMatrix(mMatrix);
    // 设置shader
    mBitmapPaint.setShader(mBitmapShader);
}


/**
 * drawable转bitmap
 *
 * @param drawable
 * @return
 */
private Bitmap drawableToBitamp(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        return bd.getBitmap();
    }
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值