您的位置:首页 > 移动开发 > Android开发

android中圆角图片(ImageView)

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

[java] view
plaincopy

public class RoundImageView extends ImageView {  

  

    public RoundImageView(Context context) {  

        super(context);  

        // TODO Auto-generated constructor stub  

    }  

  

    public RoundImageView(Context context, AttributeSet attrs) {  

        super(context, attrs);  

    }  

  

    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {  

        super(context, attrs, defStyle);  

    }  

  

    @Override  

    protected void onDraw(Canvas canvas) {  

        Path clipPath = new Path();  

        int w = this.getWidth();  

        int h = this.getHeight();  

        /** 

         * RectF  圆角矩形 

         * **/  

        clipPath.addRoundRect(new RectF(0, 0, w, h), 4.0f, 4.0f,  

                Path.Direction.CW);  

        canvas.clipPath(clipPath);  

        super.onDraw(canvas);  

    }  

}  

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

[html] view
plaincopy

<com.test.RoundImageView  

     

   android:layout_width="80dp"  

   android:layout_height="80dp"  

   android:scaleType="centerCrop"  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: