Android开发中经常会用到圆形图片,比如在用户头像设置,现在提供三种主要实现方式:
1、添加依赖
dependencies {
compile 'com.facebook.fresco:fresco:0.14.1'
}
2、初始化
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
3、修改 manifest
android:name=".MyApplication"
4、添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
5、文件布局
需要添加命名空间
xmlns:fresco="http://schemas.android.com/apk/res-auto"
引入
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/drawee_img"
fresco:roundAsCircle="true"
fresco:roundedCornerRadius="20dp"
android:layout_width="80dp"
android:layout_height="80dp" />
6、使用
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.drawee_img);
Uri uri = Uri.parse("http://p7.qhimg.com/dr/200_200_/t01b2e3a907f6ecc29d.jpg");
draweeView.setImageURI(uri);
public class CircleImageView extends ImageView {
private Paint mPaint; //画笔
private int mRadius; //圆形图片的半径
private float mScale; //图片的缩放比例
public CircleImageView(Context context) {
super(context);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//因为是圆形图片,所以应该让宽高保持一致
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
mRadius = size / 2;
setMeasuredDimension(size, size);
}
@Override
protected void onDraw(Canvas canvas) {
mPaint = new Paint();
Bitmap bitmap = drawableToBitmap(getDrawable());
//初始化BitmapShader,传入bitmap对象
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//计算缩放比例
mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());
Matrix matrix = new Matrix();
matrix.setScale(mScale, mScale);
bitmapShader.setLocalMatrix(matrix);
mPaint.setShader(bitmapShader);
//画圆形,指定好中心点坐标、半径、画笔
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
}
//写一个drawble转BitMap的方法
private Bitmap drawableToBitmap(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;
}
}
使用:
.cn.module2.CircleImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@drawable/gangtiexia"
/>
方式一
public static Bitmap createCircleImage(Bitmap source) {
int length = source.getWidth() < source.getHeight() ? source.getWidth() : source.getHeight();
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(target);
canvas.drawCircle(length / 2, length / 2, length / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
方式二:
public static Bitmap createCircleImage(Bitmap source) {
int width = source.getWidth();
int height = source.getHeight();
float raduis = Math.min(width, height) * 0.5f;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//paint.setColor(Color.RED);
//画布设置遮罩效果
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
//处理图像数据
Bitmap bitmap = Bitmap.createBitmap(width, height, source.getConfig());
Canvas canvas = new Canvas(bitmap);
//bitmap的显示由画笔paint来决定
canvas.drawCircle(width * 0.5f, height *0.5f, raduis, paint);
return bitmap;
}
与Picasso结合使用:
Picasso.with(mContext).load(Constant.format(headUrl)).transform(new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
source=createCircleImage(source);
source.recycle();
return bitmap;
}
//Picasso缓存bitmap,下次获取bitmap, 通过key
//第一次调用用key 存入
//第二次调用用key去取
@Override
public String key() {
Log.d(TAG, "key() called");
return "key";
}
}).into(meHeader);