每次写圆角都要去网上找,在这里做个记录: 圆角的imageview
/** * 圆角的ImageView */ public class MyCircleImageView extends AppCompatImageView { private float topLeftRadius; private float topRightRadius; private float bottomLeftRadius; private float bottomRightRadius; private boolean isCircle = true; private float defRadius = 0; private Paint roundPaint; private Paint imagePaint; private int borderColor; private float borderWidth; private Paint borderPaint; public MyCircleImageView(Context context) { this(context, null); } public MyCircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyCircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); super.setScaleType(ScaleType.CENTER_CROP); if (attrs != null) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCircleImageView); float radius = ta.getDimension(R.styleable.MyCircleImageView_radius, 0); topLeftRadius = ta.getDimension(R.styleable.MyCircleImageView_topLeftRadius, radius); topRightRadius = ta.getDimension(R.styleable.MyCircleImageView_topRightRadius, radius); bottomLeftRadius = ta.getDimension(R.styleable.MyCircleImageView_bottomLeftRadius, radius); bottomRightRadius = ta.getDimension(R.styleable.MyCircleImageView_bottomRightRadius, radius); isCircle = ta.getBoolean(R.styleable.MyCircleImageView_isCircle, true); borderColor = ta.getColor(R.styleable.MyCircleImageView_circleBorderColor, Color.WHITE); borderWidth = ta.getDimension(R.styleable.MyCircleImageView_circleBorderWidth, 0); ta.recycle(); } roundPaint = new Paint(); roundPaint.setColor(Color.WHITE); roundPaint.setAntiAlias(true); roundPaint.setDither(true); roundPaint.setStyle(Paint.Style.FILL); roundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); imagePaint = new Paint(); imagePaint.setXfermode(null); if (borderWidth > 0) { setBorderPaint(); } } private void setBorderPaint() { if (borderPaint == null){ borderPaint = new Paint(); borderPaint.setColor(borderColor); borderPaint.setAntiAlias(true); borderPaint.setDither(true); borderPaint.setStyle(Paint.Style.FILL); borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

本文记录了在Android中创建自定义圆角图片ImageView的方法,包括自定义类的实现和在styles文件中添加样式,使得在布局文件中就能轻松设置图片的圆角大小。
最低0.47元/天 解锁文章
1051

被折叠的 条评论
为什么被折叠?



