如何在 android 中创建左上角和右下角的圆角半径?
How to create corner radius in top left and bottom right in android?
如何在android中创建左上角和右下角的圆角半径?
像这样
我的密码是
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/bg_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_margin="@dimen/twenty_sp"
android:text="@string/sign_in"
android:textColor="@color/colorWhite"
android:textSize="@dimen/thirty_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_layout_white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/phone_number"
android:textColor="@color/colorBlack" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/five_sp"
android:orientation="horizontal">
<Spinner
android:id="@+id/main_spinner_coutry"
android:layout_width="wrap_content"
android:layout_height="@dimen/fifty_sp"
android:entries="@array/country_code"
android:prompt="@array/country_code"
android:spinnerMode="dropdown" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:background="@drawable/bg_edt"
android:digits="0123456789"
android:ems="10"
android:hint="@string/_9876543210"
android:imeOptions="actionGo"
android:inputType="phone"
android:maxLength="10" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/password"
android:textColor="@color/colorBlack" />
<EditText
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:layout_margin="@dimen/five_sp"
android:background="@drawable/bg_edt"
android:hint="@string/_9876543210"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<Button
android:id="@+id/main_btn_signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_btn"
android:padding="@dimen/ten_sp"
android:text="@string/sign_in"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:layout_margin="@dimen/ten_sp"/>
<TextView
android:layout_margin="@dimen/ten_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/ten_sp"
android:text="@string/forgot_password"
android:textColor="@color/colorAccent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
可绘制的背景是
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorWhite" />
<corners
android:topRightRadius="30dp"
/>
</shape>
显示为图片我想左上角和右下角上面的代码工作但是它只显示一个带有背景颜色的角,我尝试创建上面的图片但是它不能工作...
您需要创建一个可绘制的形状
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#5089fa" />
<size
android:width="82dp"
android:height="82dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="0dp" />
将该可绘制形状设置为背景,结果如下:
Odd Rounded Corners
可绘制形状是一个很好的解决方案。
您也可以稍微重新构建它并使用顶部小部件的左下角和第二个小部件的右上角。
如果背景在顶部为白色而在其下方为紫色,则仅使用“正常”角半径属性即可获得所需的结果。
使用 Material 组件库,您可以定义自定义 CornerTreatment
。
例如,您可以使用 CardView
并对其应用 ShapeAppearanceModel
.
<LinearLayout
android:background="@color/colorPrimaryLight"
android:clipChildren="false"
android:clipToPadding="false"
..>
<com.google.android.material.card.MaterialCardView
android:id="@+id/card"
app:cardCornerRadius="32dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
../>
</LinearLayout>
然后:
MaterialCardView cardView = findViewById(R.id.card);
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel().toBuilder()
.setTopLeftCorner(new CrazyCornerTreatment())
.setBottomLeftCorner(CornerFamily.ROUNDED,0f)
.setBottomRightCorner(CornerFamily.ROUNDED,0f)
.build());
左上角可以定义为:
class CrazyCornerTreatment : CornerTreatment() {
override fun getCornerPath(
shapePath: ShapePath,
angle: Float,
interpolation: Float,
radius: Float
) {
val interpolatedRadius = radius * interpolation
shapePath.reset(0f, -radius * interpolation, 270f,270 -angle)
shapePath.addArc(
0f,
-2*interpolatedRadius,
2*interpolatedRadius,
0f,
180f,
- angle)
}
}
创建具有两种颜色(即白色和紫色)的可绘制背景
layout_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="300dp">
<shape android:shape="rectangle" >
<size android:height="300dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item android:top="300dp">
<shape android:shape="rectangle" >
<size android:height="50dp" />
<solid android:color="##2d39a1" />
</shape>
</item>
将此可绘制对象设置为布局的背景
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/layout_background"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/purple_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_margin="@dimen/twenty_sp"
android:text="@string/sign_in"
android:textColor="@color/colorWhite"
android:textSize="@dimen/thirty_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/phone_number"
android:textColor="@color/colorBlack" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/five_sp"
android:orientation="horizontal">
<Spinner
android:id="@+id/main_spinner_coutry"
android:layout_width="wrap_content"
android:layout_height="@dimen/fifty_sp"
android:entries="@array/country_code"
android:prompt="@array/country_code"
android:spinnerMode="dropdown" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:background="@drawable/bg_edt"
android:digits="0123456789"
android:ems="10"
android:hint="@string/_9876543210"
android:imeOptions="actionGo"
android:inputType="phone"
android:maxLength="10" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/password"
android:textColor="@color/colorBlack" />
<EditText
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:layout_margin="@dimen/five_sp"
android:background="@drawable/bg_edt"
android:hint="@string/_9876543210"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<Button
android:id="@+id/main_btn_signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_btn"
android:padding="@dimen/ten_sp"
android:text="@string/sign_in"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:layout_margin="@dimen/ten_sp"/>
<TextView
android:layout_margin="@dimen/ten_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/ten_sp"
android:text="@string/forgot_password"
android:textColor="@color/colorAccent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
purple_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorPurple" />
<corners
android:bottomLeftRadius="30dp"
/>
</shape>
white_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorWhite" />
<corners
android:topRightRadius="30dp"
/>
</shape>
如果背景不合适,请在 layout_background.xml 文件中调整高度。我希望你明白了..
如何在android中创建左上角和右下角的圆角半径? 像这样
我的密码是
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/bg_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_margin="@dimen/twenty_sp"
android:text="@string/sign_in"
android:textColor="@color/colorWhite"
android:textSize="@dimen/thirty_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_layout_white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/phone_number"
android:textColor="@color/colorBlack" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/five_sp"
android:orientation="horizontal">
<Spinner
android:id="@+id/main_spinner_coutry"
android:layout_width="wrap_content"
android:layout_height="@dimen/fifty_sp"
android:entries="@array/country_code"
android:prompt="@array/country_code"
android:spinnerMode="dropdown" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:background="@drawable/bg_edt"
android:digits="0123456789"
android:ems="10"
android:hint="@string/_9876543210"
android:imeOptions="actionGo"
android:inputType="phone"
android:maxLength="10" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/password"
android:textColor="@color/colorBlack" />
<EditText
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:layout_margin="@dimen/five_sp"
android:background="@drawable/bg_edt"
android:hint="@string/_9876543210"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<Button
android:id="@+id/main_btn_signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_btn"
android:padding="@dimen/ten_sp"
android:text="@string/sign_in"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:layout_margin="@dimen/ten_sp"/>
<TextView
android:layout_margin="@dimen/ten_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/ten_sp"
android:text="@string/forgot_password"
android:textColor="@color/colorAccent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
可绘制的背景是
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorWhite" />
<corners
android:topRightRadius="30dp"
/>
</shape>
显示为图片我想左上角和右下角上面的代码工作但是它只显示一个带有背景颜色的角,我尝试创建上面的图片但是它不能工作...
您需要创建一个可绘制的形状
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#5089fa" />
<size
android:width="82dp"
android:height="82dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="0dp" />
将该可绘制形状设置为背景,结果如下:
Odd Rounded Corners
可绘制形状是一个很好的解决方案。 您也可以稍微重新构建它并使用顶部小部件的左下角和第二个小部件的右上角。 如果背景在顶部为白色而在其下方为紫色,则仅使用“正常”角半径属性即可获得所需的结果。
使用 Material 组件库,您可以定义自定义 CornerTreatment
。
例如,您可以使用 CardView
并对其应用 ShapeAppearanceModel
.
<LinearLayout
android:background="@color/colorPrimaryLight"
android:clipChildren="false"
android:clipToPadding="false"
..>
<com.google.android.material.card.MaterialCardView
android:id="@+id/card"
app:cardCornerRadius="32dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
../>
</LinearLayout>
然后:
MaterialCardView cardView = findViewById(R.id.card);
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel().toBuilder()
.setTopLeftCorner(new CrazyCornerTreatment())
.setBottomLeftCorner(CornerFamily.ROUNDED,0f)
.setBottomRightCorner(CornerFamily.ROUNDED,0f)
.build());
左上角可以定义为:
class CrazyCornerTreatment : CornerTreatment() {
override fun getCornerPath(
shapePath: ShapePath,
angle: Float,
interpolation: Float,
radius: Float
) {
val interpolatedRadius = radius * interpolation
shapePath.reset(0f, -radius * interpolation, 270f,270 -angle)
shapePath.addArc(
0f,
-2*interpolatedRadius,
2*interpolatedRadius,
0f,
180f,
- angle)
}
}
创建具有两种颜色(即白色和紫色)的可绘制背景
layout_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="300dp">
<shape android:shape="rectangle" >
<size android:height="300dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item android:top="300dp">
<shape android:shape="rectangle" >
<size android:height="50dp" />
<solid android:color="##2d39a1" />
</shape>
</item>
将此可绘制对象设置为布局的背景
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/layout_background"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/purple_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_margin="@dimen/twenty_sp"
android:text="@string/sign_in"
android:textColor="@color/colorWhite"
android:textSize="@dimen/thirty_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/phone_number"
android:textColor="@color/colorBlack" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/five_sp"
android:orientation="horizontal">
<Spinner
android:id="@+id/main_spinner_coutry"
android:layout_width="wrap_content"
android:layout_height="@dimen/fifty_sp"
android:entries="@array/country_code"
android:prompt="@array/country_code"
android:spinnerMode="dropdown" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:background="@drawable/bg_edt"
android:digits="0123456789"
android:ems="10"
android:hint="@string/_9876543210"
android:imeOptions="actionGo"
android:inputType="phone"
android:maxLength="10" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/password"
android:textColor="@color/colorBlack" />
<EditText
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_sp"
android:layout_margin="@dimen/five_sp"
android:background="@drawable/bg_edt"
android:hint="@string/_9876543210"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ten_sp"
android:orientation="vertical"
android:padding="@dimen/ten_sp">
<Button
android:id="@+id/main_btn_signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_btn"
android:padding="@dimen/ten_sp"
android:text="@string/sign_in"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:layout_margin="@dimen/ten_sp"/>
<TextView
android:layout_margin="@dimen/ten_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/ten_sp"
android:text="@string/forgot_password"
android:textColor="@color/colorAccent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
purple_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorPurple" />
<corners
android:bottomLeftRadius="30dp"
/>
</shape>
white_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorWhite" />
<corners
android:topRightRadius="30dp"
/>
</shape>
如果背景不合适,请在 layout_background.xml 文件中调整高度。我希望你明白了..