android中圆角图片(ImageView)

来源:互联网 发布:网络语言暴力调研论文 编辑:程序博客网 时间:2024/06/18 13:14

圆角图片没有生硬的感觉,带来很好的交互感觉,其为自定义代码实现方法,继承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);}@Overrideprotected 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);}}

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

     <com.test.RoundImageView                android:layout_width="80dp"        android:layout_height="80dp"        android:scaleType="centerCrop"        android:src="@drawable/liushishi" />

实现的效果如下:



原创粉丝点击