码迷,mamicode.com
首页 > 移动开发 > 详细

Android圆角图片封装类

时间:2015-01-12 19:19:07      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

你还在为处理圆角而在项目里到处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
package com.phodev.andtools.widget;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
 
/**
 * 圆角ImageView
 *
 * @author skg
 *
 */
public class RoundImageView extends ImageView {
 
    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    public RoundImageView(Context context) {
        super(context);
        init();
    }
 
    private final RectF roundRect = new RectF();
    private float rect_adius = 6;
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();
 
    private void init() {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
        //
        float density = getResources().getDisplayMetrics().density;
        rect_adius = rect_adius * density;
    }
 
    public void setRectAdius(float adius) {
        rect_adius = adius;
        invalidate();
    }
 
    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
        roundRect.set(0, 0, w, h);
    }
 
    @Override
    public void 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圆角图片封装类

标签:

原文地址:http://blog.csdn.net/u014311077/article/details/42645587

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!