android中圆角图片(ImageView)

来源:互联网 发布:网络爬虫工具 编辑:程序博客网 时间:2024/06/01 10:08

圆角图片没有生硬的感觉,带来很好的交互感觉,其为自定义代码实现方法,继承ImageView,实现过程如下:

[java] view plaincopy
  1. public class RoundImageView extends ImageView {  
  2.   
  3.     public RoundImageView(Context context) {  
  4.         super(context);  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.   
  8.     public RoundImageView(Context context, AttributeSet attrs) {  
  9.         super(context, attrs);  
  10.     }  
  11.   
  12.     public RoundImageView(Context context, AttributeSet attrs, int defStyle) {  
  13.         super(context, attrs, defStyle);  
  14.     }  
  15.   
  16.     @Override  
  17.     protected void onDraw(Canvas canvas) {  
  18.         Path clipPath = new Path();  
  19.         int w = this.getWidth();  
  20.         int h = this.getHeight();  
  21.         /** 
  22.          * RectF  圆角矩形 
  23.          * **/  
  24.         clipPath.addRoundRect(new RectF(00, w, h), 4.0f, 4.0f,  
  25.                 Path.Direction.CW);  
  26.         canvas.clipPath(clipPath);  
  27.         super.onDraw(canvas);  
  28.     }  
  29. }  

引用实现代码的布局如下:

[html] view plaincopy
  1. <com.test.RoundImageView  
  2.      
  3.    android:layout_width="80dp"  
  4.    android:layout_height="80dp"  
  5.    android:scaleType="centerCrop"  
0 0