public class CustomView extends View {
private float cornerRadius;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
cornerRadius = typedArray.getDimension(R.styleable.CustomView_cornerRadius, 0);
typedArray.recycle();
init();
}
private void init() {
// 设置outline provider
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) { // create a rounded rectangle outline outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), cornerRadius); } }); // open The view clips to the outline setClipToOutline(true); } }
2. The second method, using ShapeableImageView to customize rounded corners