欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

android开发之 - 圆形头像

发布时间:2025/7/14 编程问答 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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;

new Thread(new Runnable() {URL url;@Overridepublic void run() {// TODO Auto-generated method stubtry {url = new URL("http://99touxiang.com/public/upload/rihan/15/13-042315_940.jpg");inputStream = url.openStream();// 将字节流转换为Bitmap,使用BitmapFactory工厂类进行转换Bitmap bitmap = BitmapFactory.decodeStream(inputStream);output = getRoundedCornerBitmap(bitmap);header_image.post(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub// 更新界面 header_image.setImageBitmap(output);}});} catch (MalformedURLException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}).start();

 

 

圆形头像代码:


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开发之 - 圆形头像的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。