布局中寫法
<ZTButton
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="20dp"
android:text="圓角按鈕"
android:textColor="#ffffff"
android:textSize="30sp"
zt_btn:bgColor="@color/colorPrimaryDark"
zt_btn:borderColor="#ff0000"
zt_btn:borderWidth="5dp"
zt_btn:bottom_left_radius="0"
zt_btn:bottom_right_radius="100"
zt_btn:radius="20"
zt_btn:shape="rectangle"
zt_btn:top_left_radius="100"
zt_btn:top_right_radius="0" />
attrs.xml 文件中添加如下代碼
<declare-styleable name="ZTButton">
<attr name="bgColor" format="color" /><!--背景顏色-->
<attr name="borderWidth" format="dimension" /><!--邊框?qū)挾?->
<attr name="borderColor" format="color" /><!--邊框顏色-->
<attr name="radius" format="float" /><!--圓角-->
<attr name="top_left_radius" format="float" /><!--圓角左上-->
<attr name="top_right_radius" format="float" /><!--圓角右上-->
<attr name="bottom_left_radius" format="float" /><!--圓角左下-->
<attr name="bottom_right_radius" format="float" /><!--圓角右下-->
<attr name="shape">
<enum name="rectangle" value="0" /><!--矩形(圓角矩形)-->
<enum name="oval" value="1" /><!--橢圓逐样,圓-->
<enum name="line" value="2" /><!--線-->
<enum name="ring" value="3" /><!--圓環(huán)-->
</attr>
</declare-styleable>
控件代碼
/**
* 作者:zt
* 時(shí)間:on 18/12/26
* 說明: 自定義 按鈕, 實(shí)現(xiàn)不同角的圓角 ,添加邊框
*/
public class ZTButton extends android.support.v7.widget.AppCompatButton {
//形狀 圖
private GradientDrawable shapeDrawable;
public ZTButton(Context context) {
super(context);
}
public ZTButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ZTButton);
if (a != null) {
//背景顏色
int bgColor = a.getColor(R.styleable.ZTButton_bgColor, 0);
setBgColor(bgColor);
//獲取形狀
int shape = a.getInteger(R.styleable.ZTButton_shape, 0);
setShape(shape);
//獲取圓角
float radius = a.getFloat(R.styleable.ZTButton_radius, 0);
setRadius(radius);
//獲取左上圓角
float topLeftRadius = a.getFloat(R.styleable.ZTButton_top_left_radius, 0);
//獲取右上圓角
float topRightRadius = a.getFloat(R.styleable.ZTButton_top_right_radius, 0);
//獲取左下圓角
float bottomLeftRadius = a.getFloat(R.styleable.ZTButton_bottom_left_radius, 0);
//獲取右下圓角
float bottomRightRadius = a.getFloat(R.styleable.ZTButton_bottom_right_radius, 0);
if (topLeftRadius != 0 || bottomLeftRadius != 0 || topRightRadius != 0 || bottomRightRadius != 0) {
setRadius(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
}
//邊框?qū)挾? int borderWidth = (int) a.getDimension(R.styleable.ZTButton_borderWidth, 0);
//邊框顏色
int borderColor = a.getColor(R.styleable.ZTButton_borderColor, bgColor);
setBorder(borderWidth, borderColor);
}
}
/**
* 設(shè)置邊框
*
* @param borderColor 邊框顏色
* @param borderWidth 邊框?qū)挾? */
private void setBorder(int borderWidth, @ColorInt int borderColor) {
getGradientDrawable();
shapeDrawable.setStroke(borderWidth, borderColor);
setDrawable(shapeDrawable);
}
/**
* 設(shè)置背景顏色
*
* @param bgColor
*/
private void setBgColor(@ColorInt int bgColor) {
getGradientDrawable();
shapeDrawable.setColor(bgColor);
setDrawable(shapeDrawable);
}
/**
* 設(shè)置圓角
*
* @param radius 角度
*/
private void setRadius(float radius) {
getGradientDrawable();
shapeDrawable.setCornerRadius(radius);
setDrawable(shapeDrawable);
}
/**
* 設(shè)置圓角
*
* @param topLeftRadius 左上
* @param topRightRadius 右上
* @param bottomLeftRadius 左下
* @param bottomRightRadius 右下
*/
private void setRadius(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius) {
getGradientDrawable();
shapeDrawable.setCornerRadii(new float[]{
topLeftRadius, topLeftRadius,
topRightRadius, topRightRadius,
bottomRightRadius, bottomRightRadius,
bottomLeftRadius, bottomLeftRadius
});
setDrawable(shapeDrawable);
}
/**
* 設(shè)置圖形類型
*
* @param shape 形狀
*/
private void setShape(int shape) {
getGradientDrawable();
shapeDrawable.setShape(shape);
setDrawable(shapeDrawable);
}
/**
* 設(shè)置背景
*
* @param drawable 背景
*/
private void setDrawable(Drawable drawable) {
setBackgroundDrawable(drawable);
}
/**
* 獲取需要設(shè)置到背景的圖片
*/
private void getGradientDrawable() {
if (shapeDrawable == null) {
shapeDrawable = new GradientDrawable();
}
}
}