ImageView圆角遮罩
来源:互联网 发布:gson解析json多个数组 编辑:程序博客网 时间:2024/05/19 08:23
重写了ImageView实现了圆角遮罩效果,不需要额外的资源文件,
CircleImageView为了节约内存,没有对图片进行切割,仅仅在onDraw方法中画了一层背景色,所以没法实现透明效果。
截屏:
CircleImageView的代码如下:
代码片段,双击复制
ClipCircleImageView在内存中对ImageView绘画进行了处理,我感觉比较浪费内存,一般情况下有背景色的用前一种够用了。
ClipCircleImageView的代码如下:
代码片段,双击复制
CircleImageView为了节约内存,没有对图片进行切割,仅仅在onDraw方法中画了一层背景色,所以没法实现透明效果。
截屏:
CircleImageView的代码如下:
01
02
03
04
05
06
07
08
09
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
/**
* ImageView圆形遮罩处理
* background设置遮罩颜色
* tag设置圆的边框色
* padding设置圆形线宽
* @author planet
*
*/
public
class
CircleImageView
extends
ImageView {
public
CircleImageView(Context context) {
super
(context);
initColor(
null
);
}
public
CircleImageView(Context context, AttributeSet attrs) {
super
(context, attrs);
initColor(attrs);
}
public
CircleImageView(Context context, AttributeSet attrs,
int
defStyle) {
super
(context, attrs, defStyle);
initColor(attrs);
}
private
Paint paint;
private
Path path;
private
boolean
init =
false
;
private
int
background = Color.WHITE;
private
int
circleLineWidth =
6
;
private
int
circleColor = Color.RED;
private
void
initColor(AttributeSet attrs){
if
(attrs !=
null
){
String v = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android"
,
"background"
);
if
(v !=
null
){
if
(v.startsWith(
"#"
)){
background = Color.parseColor(v);
}
else
{
background = getResources().getColor(Integer.parseInt(v.replaceAll(
"@"
,
""
)));
}
}
}
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
super
.onMeasure(widthMeasureSpec, heightMeasureSpec);
if
(!init){
initPaint();
}
}
private
void
initPaint(){
circleLineWidth = getPaddingLeft();
setPadding(
0
,
0
,
0
,
0
);
paint =
new
Paint();
paint.setStyle(Style.FILL);
paint.setColor(background);
//paint.setColor(Color.TRANSPARENT);
paint.setAntiAlias(
true
);
//paint.setStrokeWidth(2);
//paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
//Log.i(VIEW_LOG_TAG, "22>>>>>>>>>>>>>>>>>>>>3getPaddingBottom()="+getPaddingBottom());
int
width = getMeasuredWidth();
float
radius = width/(
float
)
2
;
path =
new
Path();
Log.i(VIEW_LOG_TAG,
"moveto 0,"
+radius);
path.moveTo(
0
, radius);
Log.i(VIEW_LOG_TAG,
"lineto 0,0"
);
path.lineTo(
0
,
0
);
Log.i(VIEW_LOG_TAG,
"lineto "
+width+
",0"
);
path.lineTo(width,
0
);
Log.i(VIEW_LOG_TAG,
"lineto "
+width+
","
+width);
path.lineTo(width, width);
Log.i(VIEW_LOG_TAG,
"lineto 0,"
+width);
path.lineTo(
0
, width);
Log.i(VIEW_LOG_TAG,
"lineto 0,"
+radius);
path.lineTo(
0
, radius);
//圆弧左边中间起点是180,旋转360度
//Log.i(VIEW_LOG_TAG, "arcto 0,0,"+width+","+width);
path.arcTo(
new
RectF(
0
,
0
, width, width),
180
, -
359
,
true
);
//path.addCircle(radius, radius, radius, Direction.CW);
path.close();
try
{
circleColor = Color.parseColor((String) getTag());
}
catch
(Exception e) {
e.printStackTrace();
}
init =
true
;
}
@Override
protected
void
onDraw(Canvas canvas) {
super
.onDraw(canvas);
paint.setColor(background);
paint.setStyle(Style.FILL);
canvas.drawPath(path, paint);
paint.setColor(circleColor);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(circleLineWidth);
int
width = getMeasuredHeight();
canvas.drawCircle(width/
2
, width/
2
, (
float
) (width/
2
-circleLineWidth*.
5
), paint);
}
}
ClipCircleImageView在内存中对ImageView绘画进行了处理,我感觉比较浪费内存,一般情况下有背景色的用前一种够用了。
ClipCircleImageView的代码如下:
01
02
03
04
05
06
07
08
09
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
/**
* ImageView圆形切割处理(感觉比较浪费内存,一般情况下使用CircleImageView足够了)
* padding代表圆形边框线粗,background代表圆形边框的颜色
* @author planet
*
*/
public
class
ClipCircleImageView
extends
ImageView {
public
ClipCircleImageView(Context context) {
super
(context);
}
public
ClipCircleImageView(Context context, AttributeSet attrs) {
super
(context, attrs);
initColor(attrs);
}
public
ClipCircleImageView(Context context, AttributeSet attrs,
int
defStyle) {
super
(context, attrs, defStyle);
initColor(attrs);
}
private
int
background = Color.WHITE;
private
Paint paint;
private
boolean
set =
false
;
private
int
padding =
0
;
private
void
initColor(
final
AttributeSet attrs){
if
(attrs !=
null
){
String v = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android"
,
"background"
);
if
(v !=
null
){
if
(v.startsWith(
"#"
)){
background = Color.parseColor(v);
}
else
{
background = getResources().getColor(Integer.parseInt(v.replaceAll(
"@"
,
""
)));
}
}
}
setBackgroundResource(android.R.color.transparent);
paint =
new
Paint();
paint.setColor(background);
paint.setAntiAlias(
true
);
paint.setStyle(Style.STROKE);
padding = getPaddingLeft();
setPadding(
0
,
0
,
0
,
0
);
}
@Override
public
void
setImageBitmap(
final
Bitmap bm) {
post(
new
Runnable() {
@Override
public
void
run() {
set =
true
;
ClipCircleImageView.
super
.setImageBitmap(getCroppedBitmap(bm));
}
});
}
@Override
protected
void
onDraw(Canvas canvas) {
if
(!set){
set =
true
;
setImageBitmap(getCroppedBitmap(((BitmapDrawable) getDrawable()).getBitmap()));
}
super
.onDraw(canvas);
paint.setStrokeWidth(padding);
canvas.drawCircle(getMeasuredWidth()/
2
, getMeasuredWidth()/
2
, (
float
) (getMeasuredWidth()*.
5
-padding*.
5
), paint);
}
public
Bitmap getCroppedBitmap(Bitmap bitmap) {
int
width = getMeasuredWidth();
//int height = getMeasuredHeight();
Bitmap output = Bitmap.createBitmap(width,
width, Config.ARGB_8888);
Canvas canvas =
new
Canvas(output);
final
int
color =
0xff424242
;
final
Paint paint =
new
Paint();
final
Rect rect =
new
Rect(
0
,
0
, width, width);
paint.setAntiAlias(
true
);
canvas.drawARGB(
0
,
0
,
0
,
0
);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(width /
2
, width /
2
,
width /
2
, paint);
paint.setXfermode(
new
PorterDuffXfermode(Mode.SRC_IN));
//bitmap = ImageTool.scale(width, bitmap);
canvas.drawBitmap(bitmap, rect, rect, paint);
return
output;
}
}
0 0
- ImageView圆角遮罩
- ImageView
- ImageView
- ImageView
- ImageView
- imageView
- imageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- imageView
- ImageView
- oracle 创建触发器和序列(id自增)
- long long 类型的网络字节顺序转换
- 自定义RadioButton样式并去除默认样式位置
- 在jQuery中利用AJAX加载XML数据并解析
- c++第五周项目一三角形类扩展
- ImageView圆角遮罩
- Ubuntu 使用apt-get install时,出现错误
- 模态窗口错误 Application tried to present modally an active controller
- 游戏中提升效率!
- Jedis连接
- MAC OS 10.9 编译ZEROMQ3
- 【转】树基于点的分治
- IOS后台运行 之 后台播放音乐
- 瑞星安全随身WiFi:为用户WiFi上网安全保驾护航