http://www.eoeandroid.com/thread-327424-1-1.html

 

重写了ImageView实现了圆形遮罩效果,不需要额外的资源文件,
CircleImageView为了节约内存,没有对图片进行切割,仅仅在onDraw方法中画了一层背景色,所以没法实现透明效果。
截屏:
<ignore_js_op style="word-wrap: break-word; color: rgb(51, 51, 51); font-family: 'Microsoft YaHei', Tahoma, Helvetica, SimSun, sans-serif; font-size: 14px; line-height: 21px;"> 
CircleImageView的代码如下:

001 /**
002  * ImageView圆形遮罩处理
003  * background设置遮罩颜色
004  * tag设置圆的边框色
005  * padding设置圆形线宽
006  * @author planet
007  *
008  */
009 public class CircleImageView extends ImageView {
010         public CircleImageView(Context context) {
011                 super(context);
012                 initColor(null);
013         }
014         public CircleImageView(Context context, AttributeSet attrs) {
015                 super(context, attrs);
016                 initColor(attrs);
017         }
018         public CircleImageView(Context context, AttributeSet attrs,
019                         int defStyle) {
020                 super(context, attrs, defStyle);
021                 initColor(attrs);
022         }
023  
024         private Paint paint;
025         private Path path;
026         private boolean init = false;
027         private int background = Color.WHITE;
028         private int circleLineWidth = 6;
029         private int circleColor = Color.RED;
030  
031         private void initColor(AttributeSet attrs){
032                 if(attrs != null){
033                         String v = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "background");
034                         if(v != null){
035                                 if(v.startsWith("#")){
036                                         background = Color.parseColor(v);
037                                 }else{
038                                         background = getResources().getColor(Integer.parseInt(v.replaceAll("@", "")));
039                                 }
040                         }
041                 }
042         }
043  
044         @Override
045         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
046                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
047                 if(!init){
048                         initPaint();
049                 }
050         }
051  
052         private void initPaint(){
053                 circleLineWidth = getPaddingLeft();
054                 setPadding(0, 0, 0, 0);
055                 paint = new Paint();
056                 paint.setStyle(Style.FILL);
057                 paint.setColor(background);
058                 //paint.setColor(Color.TRANSPARENT);
059                 paint.setAntiAlias(true);
060                 //paint.setStrokeWidth(2);
061                 //paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
062                 //Log.i(VIEW_LOG_TAG, "22>>>>>>>>>>>>>>>>>>>>3getPaddingBottom()="+getPaddingBottom());
063                 int width = getMeasuredWidth();
064                 float radius = width/(float)2;
065  
066                 path = new Path();
067                 Log.i(VIEW_LOG_TAG, "moveto 0,"+radius);
068                 path.moveTo(0, radius);
069                 Log.i(VIEW_LOG_TAG, "lineto 0,0");
070                 path.lineTo(0, 0);
071                 Log.i(VIEW_LOG_TAG, "lineto "+width+",0");
072                 path.lineTo(width, 0);
073                 Log.i(VIEW_LOG_TAG, "lineto "+width+","+width);
074                 path.lineTo(width, width);
075                 Log.i(VIEW_LOG_TAG, "lineto 0,"+width);
076                 path.lineTo(0, width);
077                 Log.i(VIEW_LOG_TAG, "lineto 0,"+radius);
078                 path.lineTo(0, radius);
079                 //圆弧左边中间起点是180,旋转360度
080                 //Log.i(VIEW_LOG_TAG, "arcto 0,0,"+width+","+width);
081                 path.arcTo(new RectF(0, 0, width, width), 180, -359, true);
082                 //path.addCircle(radius, radius, radius, Direction.CW);
083                 path.close();
084                 try {
085                         circleColor = Color.parseColor((String) getTag());
086                 } catch (Exception e) {
087                         e.printStackTrace();
088                 }
089                 init = true;
090         }
091  
092         @Override
093         protected void onDraw(Canvas canvas) {
094                 super.onDraw(canvas);
095                 paint.setColor(background);
096                 paint.setStyle(Style.FILL);
097                 canvas.drawPath(path, paint);
098                 paint.setColor(circleColor);
099                 paint.setStyle(Style.STROKE);
100                 paint.setStrokeWidth(circleLineWidth);
101                 int width = getMeasuredHeight();
102                 canvas.drawCircle(width/2, width/2, (float) (width/2-circleLineWidth*.5), paint);
103         }
104 }



ClipCircleImageView在内存中对ImageView绘画进行了处理,我感觉比较浪费内存,一般情况下有背景色的用前一种够用了。
ClipCircleImageView的代码如下:

01 /**
02  * ImageView圆形切割处理(感觉比较浪费内存,一般情况下使用CircleImageView足够了)
03  * padding代表圆形边框线粗,background代表圆形边框的颜色
04  * @author planet
05  *
06  */
07 public class ClipCircleImageView extends ImageView {
08         public ClipCircleImageView(Context context) {
09                 super(context);
10         }
11         public ClipCircleImageView(Context context, AttributeSet attrs) {
12                 super(context, attrs);
13                 initColor(attrs);
14         }
15         public ClipCircleImageView(Context context, AttributeSet attrs,
16                         int defStyle) {
17                 super(context, attrs, defStyle);
18                 initColor(attrs);
19         }
20  
21         private int background = Color.WHITE;
22         private Paint paint;
23         private boolean set = false;
24         private int padding = 0;
25  
26         private void initColor(final AttributeSet attrs){
27                 if(attrs != null){
28                         String v = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "background");
29                         if(v != null){
30                                 if(v.startsWith("#")){
31                                         background = Color.parseColor(v);
32                                 }else{
33                                         background = getResources().getColor(Integer.parseInt(v.replaceAll("@", "")));
34                                 }
35                         }
36                 }
37                 setBackgroundResource(android.R.color.transparent);
38                 paint = new Paint();
39                 paint.setColor(background);
40                 paint.setAntiAlias(true);
41                 paint.setStyle(Style.STROKE);
42                 padding = getPaddingLeft();
43                 setPadding(0, 0, 0, 0);
44         }
45  
46         @Override
47         public void setImageBitmap(final Bitmap bm) {
48                 post(new Runnable() {
49                         @Override
50                         public void run() {
51                                 set = true;
52                                 ClipCircleImageView.super.setImageBitmap(getCroppedBitmap(bm));
53                         }
54                 });
55         }
56  
57         @Override
58         protected void onDraw(Canvas canvas) {
59                 if(!set){
60                         set = true;
61                         setImageBitmap(getCroppedBitmap(((BitmapDrawable) getDrawable()).getBitmap()));
62                 }
63                 super.onDraw(canvas);
64                 paint.setStrokeWidth(padding);
65                 canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, (float) (getMeasuredWidth()*.5-padding*.5), paint);
66         }
67  
68         public Bitmap getCroppedBitmap(Bitmap bitmap) {
69                 int width = getMeasuredWidth();
70                 //int height = getMeasuredHeight();
71                 Bitmap output = Bitmap.createBitmap(width,
72                                 width, Config.ARGB_8888);
73                 Canvas canvas = new Canvas(output);
74  
75                 final int color = 0xff424242;
76                 final Paint paint = new Paint();
77                 final Rect rect = new Rect(0, 0, width, width);
78  
79                 paint.setAntiAlias(true);
80                 canvas.drawARGB(0, 0, 0, 0);
81                 paint.setColor(color);
82                 // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
83                 canvas.drawCircle(width / 2, width / 2,
84                                 width / 2, paint);
85                 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
86                 //bitmap = ImageTool.scale(width, bitmap);
87                 canvas.drawBitmap(bitmap, rect, rect, paint);
88                 return output;
89         }
90 }



arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()