上代码
final ImageView v1 = (ImageView) findViewById(R.id.imageView1);
final ImageView v2 = (ImageView) findViewById(R.id.imageView2);
// 圆形
Glide.with(this).load(R.drawable.info05_1_content_img01).asBitmap().override(180, 180).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_launcher).error(R.drawable.ic_launcher).centerCrop().into(new BitmapImageViewTarget(v1) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
circularBitmapDrawable.setCircular(true);
v1.setImageDrawable(circularBitmapDrawable);
}
});
// 圆角矩形
Glide.with(this).load(R.drawable.info05_1_content_img01).asBitmap().override(180, 180).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_launcher).error(R.drawable.ic_launcher).centerCrop().into(new BitmapImageViewTarget(v2) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
circularBitmapDrawable.setCornerRadius(15);
v2.setImageDrawable(circularBitmapDrawable);
}
});
android-support-v4和glide-3.6.1下载地址:
http://download.csdn.net/detail/mhtqq809201/9383499
另外附上大神写的自定义ImageView,不会OOM
/*
* AUTHOR:YOLANDA
*
* DESCRIPTION:create the File, and add the content.
*
* Copyright © ZhiMore. All Rights Reserved
*
*/
package com.zhimore.mama.view.imageview;
import java.lang.ref.WeakReference;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* <br/>
* Created in Dec 9, 2015 11:18:01 AM
*
* @author YOLANDA; QQ: 757699476
*/
public class XCRoundImageViewByXfermode extends ImageView {
// 数据定义
private Paint mPaint;
private Xfermode mXfermode = new PorterDuffXfermode(Mode.DST_IN);
private Bitmap mMaskBitmap; // 用来做
private int mRoundBorderRadius;// 圆角大小
private int mType;// 类型:圆形、圆角或椭圆
private WeakReference<Bitmap> mBufferBitmap;// 使用缓存技术,避免每次都执行onDraw
/**
* 圆
*/
public static final int TYPE_CIRCLE = 1;
/**
* 四角圆,矩形
*/
public static final int TYPE_ROUND = 2;
/**
* 椭圆形
*/
public static final int TYPE_OVAL = 3;
private static final int DEFAULT_ROUND_BORDER_RADIUS = 10;// 默认圆角大小
// 构造方法
public XCRoundImageViewByXfermode(Context context) {
this(context, null);
}
public XCRoundImageViewByXfermode(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);// 设置消除锯齿
mType = TYPE_CIRCLE;
mRoundBorderRadius = DEFAULT_ROUND_BORDER_RADIUS;
}
/**
* 测量view的大小
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 如果类型是圆形,则强制设置view的宽高一致,取宽高的较小值
if (mType == TYPE_CIRCLE) {
int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(width, width);
}
}
/**
* 绘制view的内容
*/
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// 从缓存中取出bitmap
Bitmap bmp = (mBufferBitmap == null ? null : mBufferBitmap.get());
if (bmp == null || bmp.isRecycled()) {
// 如果没有缓存存在的情况
// 获取drawable
Drawable drawable = getDrawable();
if (null != drawable) {
// 获取drawable的宽高
int dwidth = drawable.getIntrinsicWidth();
int dheight = drawable.getIntrinsicHeight();
bmp = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
float scale = 1.0f;
// 创建画布
Canvas drawCanvas = new Canvas(bmp);
// 按照bitmap的宽高,以及view的宽高,计算缩放比例;因为设置的src宽高
// 比例可能和imageview的宽高比例不同,这里我们不希望图片失真;
if (mType == TYPE_CIRCLE) {// 如果是圆形
scale = getWidth() * 1.0F / Math.min(dwidth, dheight);
} else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {// 如果是圆角矩形或椭圆
// 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;
// 缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
scale = Math.max(getWidth() * 1.0f / dwidth, getHeight() * 1.0f / dheight);
}
// 根据缩放比例,设置bounds,即相当于做缩放图片
drawable.setBounds(0, 0, (int) (scale * dwidth), (int) (scale * dheight));
drawable.draw(drawCanvas);
// 获取bitmap,即圆形、圆角或椭圆的bitmap
if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
mMaskBitmap = getDrawBitmap();
}
// 为paint设置Xfermode 渲染模式
mPaint.reset();
mPaint.setFilterBitmap(false);
mPaint.setXfermode(mXfermode);
// 绘制不同形状
if (mMaskBitmap != null) {
drawCanvas.drawBitmap(mMaskBitmap, 0, 0, mPaint);
}
mPaint.setXfermode(null);
// 将准备好的bitmap绘制出来
canvas.drawBitmap(bmp, 0, 0, null);
// bitmap缓存起来,避免每次调用onDraw,分配内存
mBufferBitmap = new WeakReference<Bitmap>(bmp);
}
} else {
// 如果缓存还存在的情况
mPaint.setXfermode(null);
canvas.drawBitmap(bmp, 0.0f, 0.0f, mPaint);
return;
}
}
/**
* 绘制不同的图形Bitmap
*/
private Bitmap getDrawBitmap() {
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
if (mType == TYPE_CIRCLE) {// 绘制圆形
canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, paint);
} else if (mType == TYPE_ROUND) {// 绘制圆角矩形
canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), mRoundBorderRadius, mRoundBorderRadius, paint);
} else if (mType == TYPE_OVAL) {
// 绘制椭圆
canvas.drawOval(new RectF(0, 0, getWidth(), getHeight()), mPaint);
}
} catch (Throwable e) {
}
return bitmap;
}
/**
* 因为使用了缓存技术,所以需要在invalidate中做些回收释放资源的处理
*/
@Override
public void invalidate() {
mBufferBitmap = null;
if (mMaskBitmap != null) {
mMaskBitmap.recycle();
mMaskBitmap = null;
}
super.invalidate();
}
public int getRoundBorderRadius() {
return mRoundBorderRadius;
}
public void setRoundBorderRadius(int mRoundBorderRadius) {
if (this.mRoundBorderRadius != mRoundBorderRadius) {
this.mRoundBorderRadius = mRoundBorderRadius;
invalidate();
}
}
public int getType() {
return this.mType;
}
public void setType(int mType) {
if (this.mType != mType) {
this.mType = mType;
invalidate();
}
}
}