实现了CornerConstraintLayout和CornerFragmeLayout两个自定义的ViewGroup。不管其中的子view如何布局,都可以自由设定带有圆角的整体布局。 如果设置宽高相等,圆角半径为宽高的一半,那整个布局就是圆形
不会影响原来的布局,只需要替换对应的ViewGroup,就可以使用带有圆角的布局。。
目前针对了ConstraintLayout和FragmentLayout实现了CornerContraintLayout CornerFrameLay头,如果想实现基于其他的VeiwGroup如LinearLayout,RelativeLayout的控件,完全复用CornerContrainerLayout的代码,只需要把对应的继承类ConstaintLayout替换成LinearLayout或者RelativeLayout。
如果项目是androidx
implementation'com.leaf:corner:1.1.0'implementation'androidx.constraintlayout:constraintlayout:1.1.3'如果不是androidx项目
implementation'com.leaf:corner:1.0.0'implementation'com.android.support.constraint:constraint-layout:1.1.3'<declare-styleablename="CustomCorner">
<attrname="topRightRadius"format="dimension" />
<attrname="topLeftRadius"format="dimension" />
<attrname="bottomRightRadius"format="dimension" />
<attrname="bottomLeftRadius"format="dimension" />
<attrname="strokeWidth"format="dimension" />
<attrname="strokeColor"format="color" />
<attrname="radius"format="dimension" />
</declare-styleable>1.直接定义四个角的半径
2.设置为圆形布局
3.分别定义每个圆角的半径
4.设置边的颜色和宽度。比如可以设置整体布局有10dp的半透明的边
privateCornerConstraintLayoutcornerConstraintLayout;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cornerConstraintLayout = findViewById(R.id.corner_layout);
findViewById(R.id.modify_bnt).setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(Viewv) {
cornerConstraintLayout.setCornderRadius(30, 0, 40, 0);
}
});
}<com.leaf.corner.CornerConstraintLayoutandroid:layout_width="wrap_content" android:layout_height="wrap_content" app:radius="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"> <ImageView android:id="@+id/img" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:scaleType="centerInside" android:src="@mipmap/beautiful_img" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />
<ImageViewandroid:layout_width="300dp"android:layout_height="200dp"android:layout_gravity="bottom"android:scaleType="centerCrop"android:src="@mipmap/test1_image"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/img"/>
</com.leaf.corner.CornerConstraintLayout><com.leaf.corner.CornerConstraintLayoutandroid:layout_width="300dp" android:layout_height="300dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:radius="150dp"> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerInside" android:src="@mipmap/beautiful_img" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />
</com.leaf.corner.CornerConstraintLayout><com.leaf.corner.CornerFrameLayoutandroid:layout_width="wrap_content" android:layout_height="wrap_content" app:bottomRightRadius="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:topLeftRadius="20dp"> <ImageView android:id="@+id/img" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:scaleType="centerInside" android:src="@mipmap/beautiful_img" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"/>
<ImageViewandroid:layout_width="300dp"android:layout_height="200dp"android:layout_gravity="bottom"android:scaleType="centerCrop"android:src="@mipmap/test1_image"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/img"/>
</com.leaf.corner.CornerFrameLayout> <com.leaf.corner.CornerConstraintLayoutandroid:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:strokeColor="#80ffffff" app:strokeWidth="20dp"> <ImageView android:id="@+id/img" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:scaleType="centerInside" android:src="@mipmap/beautiful_img" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />
<ImageViewandroid:layout_width="300dp"android:layout_height="200dp"android:layout_gravity="bottom"android:scaleType="centerCrop"android:src="@mipmap/test1_image"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/img" />
</com.leaf.corner.CornerConstraintLayout>


