android圆形图片

参考    1      2      3       4   

 

一、自定义view  继承ImageView    文件

value文件夹下 新建attr.xml  复制代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <declare-styleable name="ShapedImageView">
 4         <attr name="shape_mode" format="enum">
 5             <enum name="round_rect" value="1" />
 6             <enum name="circle" value="2" />
 7         </attr>
 8         <attr name="round_radius" format="dimension" />
 9         <attr name="stroke_width" format="dimension" />
10         <attr name="stroke_color" format="color|reference" />
11     </declare-styleable>
12 </resources>
attr

ShapedImageView.java文件  拷贝到view文件夹下 

在布局 文件下添加  xmlns:app="http://schemas.android.com/apk/res-auto"    就能创建了

 

圆形     app:shape_mode="circle"       app:stroke_color  边框颜色   app:stroke_width  边框大小    

1     <com.xxxx.view.ShapedImageView
2         android:id="@+id/iv_circle5"
3         android:layout_width="100dp"
4         android:layout_height="100dp"
5         android:scaleType="centerCrop"
6         app:shape_mode="circle"
7         />

 

角矩形   app:shape_mode="round_rect"      app:round_radius  矩形弧度    app:stroke_color  边框颜色   app:stroke_width  边框大小    

 1     <com.xxxx.view.ShapedImageView
 2         android:id="@+id/iv_circle6"
 3         android:layout_width="100dp"
 4         android:layout_height="100dp"
 5         android:scaleType="centerCrop"
 6         app:shape_mode="round_rect"
 7         app:round_radius="20dp"
 8         app:stroke_width="5dp"
 9         app:stroke_color="#1E90FF"
10          />

 

值得一提的是  android:scaleType="centerCrop"  如果不设置的话  获取网络图片会有显示问题  本地图片没问题  但是最好还是设置

universalimageloader   Glide  都能正常加载    

 

 

二、Glide  BitmapTransformation 

圆形 GlideCircleTransform

角矩形 GlideRoundTransform

把这2个文件添加到 项目文件夹中

然后在Glide 中使用   

Glide.with(this).load(imageUrl1).transform(new GlideCircleTransform(context)).into(imageView1);

Glide.with(this).load(imageUrl2).transform(new GlideRoundTransform(context)).into(imageView2);

Glide.with(this).load(imageUrl2).transform(new GlideRoundTransform(context,10)).into(imageView3);

 

不过没有边框效果   而且也只能Glide用

 

posted @ 2016-07-14 14:06  demon9  阅读(1010)  评论(0编辑  收藏  举报