首页 > 代码库 > Android圆角图片封装类

Android圆角图片封装类

你还在为处理圆角而在项目里到处copy 处理代码吗?你还在不停动态生成圆角bitmap后再使用吗?no,代码里不需要知道这些,

如果一个view需要圆角,xml直接使用下面的RoundImageView即可,

代码逻辑根本不需要任何干涉,代码中你依然当做这个是一个普通的ImageView完全没有问题,这就是关键。


而且省去了bitmap的动态生成,避免了内存开销,跟界面卡顿(原始处理圆角的方法,再处理图片比较多的时候,如果不用单独thread性能并不好)

注:如果你想让一个布局you圆角效果也是完全做到的,用这个思路即可,或者@我qq群:37493362,呼呼
标签: Android

[1].[文件] RoundImageView.java ~ 2KB    下载(107) 跳至 [1]

?
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
packagecom.phodev.andtools.widget;
 
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.RectF;
importandroid.util.AttributeSet;
importandroid.widget.ImageView;
 
/**
 * 圆角ImageView
 *
 * @author skg
 *
 */
publicclass RoundImageView extendsImageView {
 
    publicRoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    publicRoundImageView(Context context) {
        super(context);
        init();
    }
 
    privatefinal RectF roundRect = newRectF();
    privatefloat rect_adius = 6;
    privatefinal Paint maskPaint = newPaint();
    privatefinal Paint zonePaint = newPaint();
 
    privatevoid init() {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
        //
        floatdensity = getResources().getDisplayMetrics().density;
        rect_adius = rect_adius * density;
    }
 
    publicvoid setRectAdius(floatadius) {
        rect_adius = adius;
        invalidate();
    }
 
    @Override
    protectedvoid onLayout(booleanchanged, intleft, inttop, intright,
            intbottom) {
        super.onLayout(changed, left, top, right, bottom);
        intw = getWidth();
        inth = getHeight();
        roundRect.set(0,0, w, h);
    }
 
    @Override
    publicvoid draw(Canvas canvas) {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
        //
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }
 
}

Android圆角图片封装类