android开发之 - 圆形头像
生活随笔
收集整理的这篇文章主要介绍了
android开发之 - 圆形头像
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
//最新第三方圆形头像:
https://github.com/hdodenhof/CircleImageView
https://github.com/MostafaGazar/CustomShapeImageView
一段代码,可以设置圆形头像.
返回一个Bitmap对象.
==> 该列子是从网络上获取一张图片。然后展示在ImageView的。
1:首先要确保网络权限
<!-- 访问网络权限 --><uses-permission android:name="android.permission.INTERNET" />
以下是关键代码:
private InputStream inputStream;
Bitmap output;
圆形头像代码:
1
/* 圆形头像 2 * 3 * @param bitmap 4 * @param ratio 5 * 按照截取比例来获取圆形图片 6 * @return 7 */ 8 public Bitmap getRoundedCornerBitmap(Bitmap bitmap) { 9 if (bitmap == null) { 10 bitmap = BitmapFactory.decodeStream(inputStream); 11 } 12 Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), 13 bitmap.getHeight(), Config.ARGB_8888);//Bitmap.Config.ARGB_4444比Bitmap.Config.ARGB_8888更省内存 14 Canvas canvas = new Canvas(outBitmap); 15 final int color = 0xff424242; 16 final Paint paint = new Paint(); 17 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 18 final RectF rectF = new RectF(rect); 19 final float roundPX = bitmap.getWidth() / 2 < bitmap.getHeight() / 2 ? bitmap 20 .getWidth() : bitmap.getHeight(); 21 paint.setAntiAlias(true); 22 canvas.drawARGB(0, 0, 0, 0); 23 paint.setColor(color); 24 canvas.drawRoundRect(rectF, roundPX, roundPX, paint); 25 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 26 canvas.drawBitmap(bitmap, rect, rect, paint); 27 return outBitmap; 28 } 29
额.复制粘贴试试吧.
转载于:https://www.cnblogs.com/SomnusLove/p/3867417.html
总结
以上是生活随笔为你收集整理的android开发之 - 圆形头像的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: CentOs6.5下独立安装Nginx篇
- 下一篇: C1 FlexGrid 行Style设置