android中圆角图片(ImageView)

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

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);
	}
}

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

     

实现的效果如下:



你可能感兴趣的:(android)