自定义ImageView实现圆角图片,满足以下需求:
1、四个角的度数均要支持自定义
2、可以在布局当中添加自定义的度数
实现步骤:
1、自定义一个ImageView,继承自AppCompatImageView。
(关于AppCompatImageView,继承自ImageView,是一个在平台的旧版本上支持兼容功能的imageView。)
实现它的构造方法,创建一个init()方法,保证构造方法里面都会调用。
2、在onLayout方法里面获取到ImageView的width和height。
3、在onDraw方法里面对Canvas进行裁剪。
4、让四个角的度数支持自定义,并且支持在xml中设置。
实现代码如下:
public class CircleImageView extends AppCompatImageView {
float width, height;
private int defaultRadius = 0;
private int radius;
private int leftTopRadius;
private int rightTopRadius;
private int rightBottomRadius;
private int leftBottomRadius;
public CircleImageView(Context context) {
super(context);
init(context, null);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.circle_image_view);
radius = array.getDimensionPixelOffset(R.styleable.circle_image_view_radius, defaultRadius);
leftTopRadius = array.getDimensionPixelOffset(R.styleable.circle_image_view_left_top_radius, leftTopRadius);
rightTopRadius = array.getDimensionPixelOffset(R.styleable.circle_image_view_right_top_radius, rightTopRadius);
rightBottomRadius = array.getDimensionPixelOffset(R.styleable.circle_image_view_right_bottom_radius, rightBottomRadius);
leftBottomRadius = array.getDimensionPixelOffset(R.styleable.circle_image_view_left_bottom_radius, leftBottomRadius);
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);
}
}
attrs.xml,位于values文件夹下,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="circle_image_view">
<attr name="radius" format="dimension"/>
<attr name="left_top_radius" format="dimension"/>
<attr name="right_top_radius" format="dimension"/>
<attr name="right_bottom_radius" format="dimension"/>
<attr name="left_bottom_radius" format="dimension"/>
</declare-styleable>
</resources>
使用:
<com.example.custom_view.CircleImageView
android:id="@+id/civ"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="centerCrop"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:radius="20dp"/>