前言
提示:可支持四角自定义圆角大小的自定义ImageView。
提示:以下是本篇文章正文内容,下面案例可供参考
一、使用步骤
1.代码中使用方法
在Java中使用代码如下(示例):
CustomRoundAngleImageView view = holder.get(R.id.iv_device_item);
view.setLeftTopRadius(100); //左上
view.setRightTopRadius(100); //右上
view.setLeftBottomRadius(100); //左下
view.setRightBottomRadius(100); //右下
view.setRadius(0); //默认值
注意: 首先要在value下创建attrs.xml文件,并在里面加入如写代码:
<declare-styleable name="Custom_Round_Image_View">
<attr name="initial_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>
在XML中使用 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
//一定要加 xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ll_root"
android:background="@color/com_color_white"
android:gravity="center">
<com.tenbay.home.nvr.View.CustomRoundAngleImageView
android:id="@+id/iv_device_item"
android:layout_width="@dimen/com_155dp"
android:layout_height="@dimen/com_73dp"
app:left_top_radius="10dp"/>
</LinearLayout>
2.自定义ImageView
代码如下(示例):
public class CustomRoundAngleImageView extends AppCompatImageView {
float width, height;
private int radius;
private int leftTopRadius;
private int rightTopRadius;
private int rightBottomRadius;
private int leftBottomRadius;
public CustomRoundAngleImageView(Context context) {
this(context, null);
initView(context, null);
}
public CustomRoundAngleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
initView(context, attrs);
}
public CustomRoundAngleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
@SuppressLint("CustomViewStyleable") TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Custom_Round_Image_View);
int defaultRadius = 0;
radius = array.getDimensionPixelOffset(R.styleable.Custom_Round_Image_View_initial_radius, defaultRadius);
leftTopRadius = array.getDimensionPixelOffset(R.styleable.Custom_Round_Image_View_left_top_radius, defaultRadius);
rightTopRadius = array.getDimensionPixelOffset(R.styleable.Custom_Round_Image_View_right_top_radius, defaultRadius);
rightBottomRadius = array.getDimensionPixelOffset(R.styleable.Custom_Round_Image_View_right_bottom_radius, defaultRadius);
leftBottomRadius = array.getDimensionPixelOffset(R.styleable.Custom_Round_Image_View_left_bottom_radius, defaultRadius);
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();
}
public void setRadius(int radius) {
this.radius = radius;
}
public void setLeftTopRadius(int leftTopRadius) {
this.leftTopRadius = leftTopRadius;
}
public void setRightTopRadius(int rightTopRadius) {
this.rightTopRadius = rightTopRadius;
}
public void setRightBottomRadius(int rightBottomRadius) {
this.rightBottomRadius = rightBottomRadius;
}
public void setLeftBottomRadius(int leftBottomRadius) {
this.leftBottomRadius = leftBottomRadius;
}
@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) {
@SuppressLint("DrawAllocation") 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);
}
}
总结
只是为了记录一下,方便日后需要时,不用再重写,复制即可使用。
注: 此篇文章只是为了日后自己不用再重复造轮子。