android 使用shape定义圆角矩形

通过在drawble下定义xml实现圆角边

没事随便写写!需要用到圆角矩形边框,通过在drawble文件定义shape.xml实现;
不多说,直接上代码:
shape_round_corner.xml

//定义四个圆角
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ff00" />
    <corners android:topLeftRadius="12dp"
        android:topRightRadius="12dp"
        android:bottomRightRadius="12dp"
        android:bottomLeftRadius="12dp"/>
    <stroke android:width="1dp" android:color="#ff0" />
</shape>

<!--定义上半圆角,如果定义下面,方式一样,去掉topLeftRadius/topRightRadius,加上bottomRightRadius/bottomLeftRadius即可;-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ff00" />
    <corners android:topLeftRadius="12dp"
        android:topRightRadius="12dp"
        />
    <stroke android:width="1dp" android:color="#ff0" />
</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:alpha="0.8"
        android:background="@drawable/shape_round_corner">
    </LinearLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

附属上一张图片:
这里写图片描述

Android中创建圆角矩形可以通过多种方式实现,以下是两种常见的方法: 1. 使用XML定义圆角矩形: 可以在资源文件中使用`<shape>`标签定义一个圆角矩形的样式。以下是一个简单的例子: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FFFFFF"/> <!-- 设置填充颜色 --> <corners android:radius="8dp" /> <!-- 设置圆角半径 --> </shape> ``` 这个例子中定义了一个白色的圆角矩形圆角半径为8dp。将这个XML文件放在res/drawable目录下,然后在布局文件中通过`android:background="@drawable/your_rounded_rectangle"`引用。 2. 使用代码创建圆角矩形: 可以通过`Paint`和`Canvas`类在代码中绘制一个圆角矩形。以下是一个示例代码片段: ```java // 创建一个Paint对象并设置抗锯齿 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 设置画笔颜色 paint.setColor(Color.WHITE); // 设置画笔样式为填充 paint.setStyle(Paint.Style.FILL); // 设置圆角半径 paint.setShadowLayer(10f, 0f, 0f, Color.BLACK); // 可以增加阴影效果 // 创建一个 RectF 对象表示矩形的位置和大小 RectF rect = new RectF(0, 0, 100, 100); // 这里的尺寸可以根据需要调整 // 使用Canvas绘制圆角矩形 Canvas canvas = new Canvas(bitmap); // bitmap是预先创建好的Bitmap对象 canvas.drawRoundRect(rect, 10, 10, paint); // 第二个和第三个参数是圆角的水平和垂直半径 ``` 在这个例子中,首先创建了一个`Paint`对象并配置了抗锯齿、颜色、样式和阴影层。接着定义了一个`RectF`对象来指定矩形的位置和尺寸。最后在`Canvas`对象上绘制圆角矩形
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值