【android】 圆角头像

I:画圆角:

 1   private void roundImg(ImageView iv){
 2 
 3   /**本地资源*/
 4 
 5   InputStream is = getResources().openRawResource(R.drawable.icon);
 6 
 7   Bitmap bitmap = BitmapFactory.decodeStream(is);
 8 
 9   /**网络资源*/
10 
11   //FileInputStream fis = new FileInputStream(url);
12 
13   //Bitmap bitmap = BitmapFactory.decodeStream(fis);
14 
15     if(bitmap != null){
16 
17     Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap,100,100);
18 
19     Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap,50.0f);
20 
21     iv.setImageBitmap(roundBitmap);
22 
23     }
24 
25   }

 II:ImageUtil工具类

 1 public class ImageUtil {
 2 
 3     public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
 4 
 5         int width = bitmap.getWidth();
 6         int height = bitmap.getHeight();
 7         /**
 8          * 
 9          */
10         Matrix matrix = new Matrix();
11         float scaleWidth = (float) w / width;
12         float scaleHeight = (float) h / height;
13         matrix.postScale(scaleWidth, scaleHeight);
14         Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
15         return newbmp;
16     }
17 
18     
19     public static Bitmap drawableToBitmap(Drawable drawable) {
20         int width = drawable.getIntrinsicWidth();
21         int height = drawable.getIntrinsicHeight();
22         Bitmap bitmap = Bitmap.createBitmap(width, height,
23                 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
24                         : Bitmap.Config.RGB_565);
25         Canvas canvas = new Canvas(bitmap);
26         drawable.setBounds(0, 0, width, height);
27         drawable.draw(canvas);
28         return bitmap;
29     }
30 
31     
32     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
33 
34         Bitmap output = Bitmap
35                 .createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
36         Canvas canvas = new Canvas(output);
37         final int color = 0xff424242;
38         final Paint paint = new Paint();
39         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
40         final RectF rectf = new RectF(rect);
41 
42         paint.setAntiAlias(true);
43         canvas.drawARGB(0, 0, 0, 0);
44         paint.setColor(color);
45         canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
46 
47         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
48         canvas.drawBitmap(bitmap, rect, rect, paint);
49         return output;
50     }
51 
52     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
53         final int reflectionGap = 4;
54         int width = bitmap.getWidth();
55         int height = bitmap.getHeight();
56 
57         Matrix matrix = new Matrix();
58         matrix.preScale(1, -1);
59 
60         Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
61                 0, height / 2, width, height / 2, matrix, false);
62 
63         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),
64                 Config.ARGB_8888);
65 
66         Canvas canvas = new Canvas(bitmapWithReflection);
67         canvas.drawBitmap(bitmap, 0, 0, null);
68         Paint deafalutPaint = new Paint();
69         canvas.drawRect(0, height, width, height + reflectionGap,
70                 deafalutPaint);
71 
72         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
73 
74         Paint paint = new Paint();
75         LinearGradient shader = new LinearGradient(0,
76                 bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
77                         + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
78         paint.setShader(shader);
79         // Set the Transfer mode to be porter duff and destination in
80         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
81         // Draw a rectangle using the paint with our linear gradient
82         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
83                 + reflectionGap, paint);
84 
85         return bitmapWithReflection;
86     }
87     
88 }

 

【android】 圆角头像,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。