//最新第三方圆形头像:
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; @Override public void run() { // TODO Auto-generated method stub try { 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() { @Override public 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
额.复制粘贴试试吧.