/* * Copyright (c) 2020 WildFireChat. All rights reserved. */ package cn.wildfire.chat.kit.widget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Path; import android.os.Build; import android.util.AttributeSet; import android.view.View; import androidx.appcompat.widget.AppCompatImageView; import cn.wildfire.chat.kit.R; public class RoundImageView extends AppCompatImageView { float width,height; float roundSize; public RoundImageView(Context context) { this(context, null); } public RoundImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView); roundSize = a.getDimension(R.styleable.RoundImageView_roundSize, 10); if (Build.VERSION.SDK_INT < 18) { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } } @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) { if (width > roundSize && height > roundSize) { Path path = new Path(); path.moveTo(roundSize, 0); path.lineTo(width - roundSize, 0); path.quadTo(width, 0, width, roundSize); path.lineTo(width, height - roundSize); path.quadTo(width, height, width - roundSize, height); path.lineTo(roundSize, height); path.quadTo(0, height, 0, height - roundSize); path.lineTo(0, roundSize); path.quadTo(0, 0, roundSize, 0); canvas.clipPath(path); } super.onDraw(canvas); } }