Android 两种方式实现圆形头像
【摘要】
方案一:继承自ImageView
package com.chy.widget;
import android.content.Context;
import android.graphics.Can...
方案一:继承自ImageView
package com.chy.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.ImageView;
//有一个问题,当我把它放在列表项中, 然后列表上面有一个edittext用来filter, 结果发现该自定义imageview本该被edittext挡住, 结果却绘制在edittext之上
//初步判断是由canvas.clipPath(path, Region.Op.REPLACE);引起的,它把第一次不存在的部分也画了
//解决方案: REPLACE(覆盖) 改为 INTERSECT(交集)
public class CircleImageView extends ImageView {
private Paint paint = null;
// 设置画布抗锯齿(毛边过滤)
private PaintFlagsDrawFilter pfdf = null;
private Path path = null;
public CircleImageView(Context context) {
super(context);
init(context, null);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
// public CircleImageView(Context context, AttributeSet attrs, int
// defStyleAttr, int defStyleRes) {
// super(context, attrs, defStyleAttr, defStyleRes);
// init(context, attrs);
// }
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
// 透明度: 00%=FF(不透明) 100%=00(透明)
paint.setColor(Color.WHITE);
// paint.setColor(Color.parseColor("ffffffff"));
paint.setStyle(Paint.Style.STROKE);
// 解决图片拉伸后出现锯齿的两种办法: 1.画笔上设置抗锯齿 2.画布上设置抗锯齿
// http://labs.easymobi.cn/?p=3819
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
int clearBits = 0;
int setBits = Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG;
pfdf = new PaintFlagsDrawFilter(clearBits, setBits);
//由于imageview有默认底色,如黑色,设置背景为透明是为了第一次setImageBitmap时不显示圆以外方型的默认背景色
//但是这样在中兴nubia手机上还会首先显示正方形黑色背景,然后才变圆(解决办法,先裁成圆再setImageBitmap)
setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
}
@Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
// CCW: CounterClockwise(逆时针)
// CW: Clockwise(顺时针)
if (path == null) {
path = new Path();
path.addCircle(width / 2f, height / 2f, Math.min(width / 2f, height / 2f), Path.Direction.CCW);
path.close();
}
// canvas.drawCircle(width / 2f, height / 2f, Math.min(width / 2f, height / 2f), paint);
// super.onDraw里面也可能有多个canvas.save
int saveCount = canvas.save();
canvas.setDrawFilter(pfdf);
// Region.Op.REPLACE 是显示第二次的
// canvas.clipPath(path, Region.Op.REPLACE);
canvas.clipPath(path, Region.Op.INTERSECT);
super.onDraw(canvas);
canvas.restoreToCount(saveCount);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
方案二:继承自View
package com.chy.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView.ScaleType;
public class CircleView extends View {
private Paint paint = null;
private PaintFlagsDrawFilter pfdf = null;
// 在已有的图像上绘图将会在其上面添加一层新的形状, 设置这两个形状的显示方式
private PorterDuffXfermode xfermode = null;
private RectF rectf = null;
private Bitmap bitmap = null;
private Bitmap destBmp = null;
private Matrix matrix = null;
private ScaleType scaleType = ScaleType.CENTER_CROP;
public CircleView(Context context) {
super(context);
init(context, null);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
// public CircleView(Context context, AttributeSet attrs, int defStyleAttr,
// int defStyleRes) {
// super(context, attrs, defStyleAttr, defStyleRes);
// init(context, attrs);
// }
private void init(Context context, AttributeSet attrs) {
try {
if (android.os.Build.VERSION.SDK_INT >= 11) {
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
} catch (Exception e) {
e.printStackTrace();
}
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
// 透明度: 00%=FF(不透明) 100%=00(透明)
// paint.setColor(Color.parseColor("#ffffffff"));
// 解决图片拉伸后出现锯齿的两种办法: 1.画笔上设置抗锯齿 2.画布上设置抗锯齿
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
int clearBits = 0;
int setBits = Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG;
pfdf = new PaintFlagsDrawFilter(clearBits, setBits);
// PorterDuff.Mode.MULTIPLY只显示重叠部分
xfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
matrix = new Matrix();
}
public void setImageResource(int resId) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId);
setBitmap(bmp);
}
public void setBitmap(Bitmap bmp) {
this.bitmap = bmp;
}
public void setScaleType(ScaleType scaleType) {
this.scaleType = scaleType;
}
private void makeDestBmp(int width, int height) {
destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
rectf = new RectF(0, 0, width, height);
Canvas canvas = new Canvas(destBmp);
canvas.drawOval(rectf, paint);
}
@Override
protected void onDraw(Canvas canvas) {
if (bitmap == null) {
return;
}
int width = getWidth();
int height = getHeight();
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
if (destBmp == null) {
makeDestBmp(width, height);
}
// canvas.save();
// 设置画布抗锯齿
canvas.setDrawFilter(pfdf);
canvas.drawBitmap(destBmp, 0, 0, paint);
paint.setXfermode(xfermode);
switch(scaleType) {
case FIT_XY:
canvas.drawBitmap(bitmap, null, rectf, paint);
break;
case CENTER_CROP:
default:
matrix.reset();
float scale = Math.max((float) width / (float) bmpWidth, (float) height / (float) bmpHeight);
// 默认绕原点进行缩放 matrix.postScale(scale, scale, 0, 0);
matrix.postScale(scale, scale, 0, 0);
matrix.postTranslate((width - bmpWidth * scale) / 2f, (height - bmpHeight * scale) / 2f);
canvas.drawBitmap(bitmap, matrix, paint);
break;
}
paint.setXfermode(null);
// canvas.restore();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/54800086
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)