目前想到的能實(shí)現(xiàn)圓角的方式有這么幾種:
1劲赠、使用shape元素(比較常用)
2、使用背景圖片(有點(diǎn)蠢峡竣,還得讓UI切圖靠抑,不建議這種方式)
3、自定義控件實(shí)現(xiàn)
4适掰、使用ViewOutlineProvider裁剪view
5颂碧、使用cardView
6、其他第三方庫(kù)
下面來(lái)詳細(xì)說(shuō)明一下类浪,如何用上面的方式實(shí)現(xiàn)圓角Button:
1载城、使用shape元素
在drawable文件夾中新建一個(gè)shape.xml文件,之后布局文件中設(shè)置背景顏色為shape.xml即可费就。利用shape元素實(shí)現(xiàn)圓角:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"/> <!-- 設(shè)置圓角弧度 -->
<solid android:color="@color/bg_color"/> <!-- 設(shè)置背景顏色 -->
<stroke android:color="@color/divider_color" android:width="@dimen/y2"/> <!-- 設(shè)置邊框顏色以及寬度 -->
</shape>
使用shape元素的優(yōu)點(diǎn)是:方便簡(jiǎn)潔且直觀诉瓦,可以在項(xiàng)目中先定義好通用的控件樣式,其他需要同樣樣式的按鈕直接設(shè)置背景顏色就可以了受楼,缺點(diǎn)是一個(gè)xml文件只能寫(xiě)定一種樣式垦搬,如果項(xiàng)目中需要多種樣式則每種不同的樣式都需要一個(gè)專(zhuān)門(mén)的xml文件呼寸。
2艳汽、使用切圖就不多說(shuō)了,使用起來(lái)不方便对雪。
3河狐、自定義Button
第一步:在values文件中新建attrs文件,然后在resources中寫(xiě)入要自定義的屬性
<resources>
<declare-styleable name="CustomButton">
<!-- 圓角半徑 -->
<attr name="radius" format="dimension" />
<!-- 背景顏色 -->
<attr name="normal_color" format="reference|color" />
</declare-styleable>
</resources>
第二步瑟捣,自定義class文件馋艺,繼承自Button或者AppCompatButton
public class CustomButton extends AppCompatButton {
// 圓角半徑
private float radius = getResources().getDimension(R.dimen.x6);
// 按鈕正常時(shí)的背景顏色
private int normalColor;
private GradientDrawable gradientDrawable;
public CustomButton(Context context) {
this(context, null, 0);
}
public CustomButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttris(attrs);
init();
}
private void init() {
setGravity(Gravity.CENTER);
gradientDrawable = new GradientDrawable();
gradientDrawable.setCornerRadius(radius);
gradientDrawable.setColor(normalColor);
// 這句話(huà)一定不要忘記,不然沒(méi)效果
setBackground(gradientDrawable);
}
/**
* 屬行初始化
* @param attrs
*/
private void initAttris(AttributeSet attrs) {
if (attrs != null) { // 獲取自定義屬性
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomButton);
radius = (int) typedArray .getDimension(R.styleable.CustomButton_radius, getResources().getDimension(R.dimen.x6));
int defaultColor = getResources().getColor(R.color.base_blue);
normalColor = typedArray.getColor(R.styleable.CustomButton_normal_color, defaultColor);
typedArray.recycle();
}
}
}
第三步迈套,在xml文件中使用自定義按鈕捐祠,其中app:radius,app:normal_color即是使用自定義的屬性設(shè)置桑李。
<com.application.demo.view.CustomButton
android:id="@+id/btn_test"
android:layout_width="128dp"
android:layout_height="44dp"
app:radius="@dimen/x40"
app:normal_color="@color/base_orange"
android:textColor="@color/white"
android:text="測(cè)試"/>
使用自定義Button彌補(bǔ)了使用shape元素的缺點(diǎn)踱蛀,當(dāng)項(xiàng)目中需要很多不同的樣式時(shí)窿给,只需要修改對(duì)應(yīng)的屬性值即可。
4率拒、使用ViewOutlineProvider
ViewOutlineProvider是Android 5.x引入的新特性崩泡,用于實(shí)現(xiàn)View的陰影和輪廓,用ViewOutlineProvider實(shí)現(xiàn)圓角實(shí)現(xiàn)圓角:
roundImage.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
// 設(shè)置按鈕圓角率為30
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
// 設(shè)置按鈕為圓形
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
roundImage.setClipToOutline(true);
5猬膨、使用CardView
CardView是Android 5.0引入的卡片顯示控件角撞,可以實(shí)現(xiàn)陰影和圓角,通過(guò)設(shè)置cardCornerRadius來(lái)設(shè)置圓角半徑
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:cardCornerRadius="50dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/girl" />
</android.support.v7.widget.CardView>
6勃痴、其他一些第三方的庫(kù)有的也有圓角的功能谒所,例如fresco,如果你項(xiàng)目中引入了fresco沛申,那么可以通過(guò)設(shè)置下面的屬行來(lái)設(shè)置圓角半徑:
fresco:roundedCornerRadius="25dp"
相關(guān)文章:你不知道的圓形圓角處理方式