package com.example.roundedcornerlayout;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到自定义 ViewGroup 并设置圆角
RoundedCornerLayout roundedCornerLayout = findViewById(R.id.rounded_corner_layout);
roundedCornerLayout.setCornerRadius(50.0f); // 设置圆角半径
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<com.example.roundedcornerlayout.RoundedCornerLayout
android:id="@+id/rounded_corner_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFC107">
<!-- 子 View -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello, Rounded Corner!"
android:textSize="18sp"
android:textColor="#FFFFFF" />
</com.example.roundedcornerlayout.RoundedCornerLayout>
</RelativeLayout>
package com.example.roundedcornerlayout;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class RoundedCornerLayout extends ViewGroup {
private Paint paint;
private Path path;
private float cornerRadius = 50.0f; // 圆角半径
public RoundedCornerLayout(Context context) {
super(context);
init();
}
public RoundedCornerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// 初始化绘制圆角所需的画笔和路径
paint = new Paint();
paint.setAntiAlias(true);
path = new Path();
}
@Override
protected void dispatchDraw(Canvas canvas) {
// 在绘制子View之前对画布进行裁剪,以实现圆角效果
path.reset();
RectF rect = new RectF(0, 0, getWidth(), getHeight());
path.addRoundRect(rect, cornerRadius, cornerRadius, Path.Direction.CW);
canvas.save();
canvas.clipPath(path);
super.dispatchDraw(canvas);
canvas.restore();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 布局子View的位置
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final android.view.View child = getChildAt(i);
if (child.getVisibility() != GONE) {
// 假设这里所有子View都充满父View
child.layout(0, 0, getWidth(), getHeight());
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 测量子View,假设这里所有子View都使用父View的大小
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
public void setCornerRadius(float radius) {
// 设置圆角半径
cornerRadius = radius;
invalidate(); // 重新绘制View
}
}