Android圆角设置

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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值