使用自定义ImageView,实现圆角功能

1.自定义属性attrs.xml

<?xml version="1.0" encoding="utf-8"?>                                                

2.自定义RoundCornerImageView,继承AppCompatImageView

public class RoundCornerImageView extends AppCompatImageView {    private float width, height;    private int defaultRadius = 0;    private int radius;    private int leftTopRadius;    private int rightTopRadius;    private int rightBottomRadius;    private int leftBottomRadius;    public RoundCornerImageView(Context context) {        this(context, null);        init(context, null);    }    public RoundCornerImageView(Context context, AttributeSet attrs) {        this(context, attrs, 0);        init(context, attrs);    }    public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context, attrs);    }    private void init(Context context, AttributeSet attrs) {        if (Build.VERSION.SDK_INT < 18) {            setLayerType(View.LAYER_TYPE_SOFTWARE, null);        }        // 读取配置        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);        radius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_radius, defaultRadius);        leftTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_top_radius, defaultRadius);        rightTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_top_radius, defaultRadius);        rightBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_bottom_radius, defaultRadius);        leftBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_bottom_radius, defaultRadius);        //如果四个角的值没有设置,那么就使用通用的radius的值。        if (defaultRadius == leftTopRadius) {            leftTopRadius = radius;        }        if (defaultRadius == rightTopRadius) {            rightTopRadius = radius;        }        if (defaultRadius == rightBottomRadius) {            rightBottomRadius = radius;        }        if (defaultRadius == leftBottomRadius) {            leftBottomRadius = radius;        }        array.recycle();    }    @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        super.onLayout(changed, left, top, right, bottom);        width = getWidth();        height = getHeight();    }    @Override    protected void onDraw(Canvas canvas) {        //这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪        int maxLeft = Math.max(leftTopRadius, leftBottomRadius);        int maxRight = Math.max(rightTopRadius, rightBottomRadius);        int minWidth = maxLeft + maxRight;        int maxTop = Math.max(leftTopRadius, rightTopRadius);        int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);        int minHeight = maxTop + maxBottom;        if (width >= minWidth && height > minHeight) {            Path path = new Path();            //四个角:右上,右下,左下,左上            path.moveTo(leftTopRadius, 0);            path.lineTo(width - rightTopRadius, 0);            path.quadTo(width, 0, width, rightTopRadius);            path.lineTo(width, height - rightBottomRadius);            path.quadTo(width, height, width - rightBottomRadius, height);            path.lineTo(leftBottomRadius, height);            path.quadTo(0, height, 0, height - leftBottomRadius);            path.lineTo(0, leftTopRadius);            path.quadTo(0, 0, leftTopRadius, 0);            canvas.clipPath(path);        }        super.onDraw(canvas);    }}

3.布局文件中使用

<?xml version="1.0" encoding="utf-8"?>    

4.调用

public class MainActivity extends AppCompatActivity {    String avatarUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1516644385815&di=c0552674db9f07a5f889d7c0980e33db&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170529%2F83d3ce719e9d4c0a8f1cd033ecac3692_th.jpg";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ImageView ivAvatar = findViewById(R.id.iv_avatar);        Glide.with(this).load(avatarUrl).into(ivAvatar);    }}

更多相关文章

  1. ImageView的android:scaleType属性
  2. android 动态设置布局
  3. 新浪微博布局学习——活用Android的RelativeLayout
  4. android 系统属性 之 自定义属性
  5. android分开布局
  6. android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明
  7. Android属性动画-简单实例
  8. Android获取屏幕相关属性ScreenUtil
  9. android中属性动画 ObjectionAnimation

随机推荐

  1. android中的按钮以图片的方式显示_基础篇
  2. Android(安卓)Softap启动分析
  3. android学习网站
  4. android ksoap2 访问https javax.net.ssl
  5. 【Android】播放提示音
  6. 自定义RatingBar
  7. Android--Intent常用(拨号,浏览器,联系人,Wi-
  8. android 设置textview边框以及点击效果
  9. android:Spinner 设置收起以及展开的字体
  10. Android(安卓)对话框