@@ -16,14 +16,19 @@ public class GradientButton extends FrameLayout {
1616 private TextView textView ;
1717 private View root ;
1818
19+ public static int RIGHT_LEFT = 1 ;
20+ public static int LEFT_RIGHT = 2 ;
21+ public static int BOTTOM_TOP = 3 ;
22+ public static int TOP_BOTTOM = 4 ;
23+
1924 public GradientButton (@ NonNull Context context ) {
2025 super (context );
2126 initView ();
2227 }
2328
2429 public GradientButton (Context context , AttributeSet attrs ) {
2530 super (context , attrs );
26- initView (context , attrs );
31+ initView (context , attrs );
2732 }
2833
2934 public GradientButton (Context context , AttributeSet attrs , int defStyleAttr ) {
@@ -61,21 +66,23 @@ private void initView(Context context, AttributeSet attrs) {
6166 mEndColor = context .getResources ().getColor (mEndColor );
6267 }
6368
69+ int orientation = ta .getInteger (R .styleable .GradientButton_orientation , 1 );
70+
6471 String Font = ta .getString (R .styleable .GradientButton_typeface );
6572
66- if (Font != null ){
67- setFont (context , Font );
73+ if (Font != null ) {
74+ setFont (context , Font );
6875 }
6976
7077 ColorStateList color = ta .getColorStateList (R .styleable .GradientButton_textColor );
7178
72- setRadius (Radius * 2 , mStartColor , mEndColor );
79+ setRadius (Radius * 2 , mStartColor , mEndColor , orientation );
7380
7481 setText (Text );
7582
76- setTextSize ((float ) (textSize * 0.4 ));
83+ setTextSize ((float ) (textSize * 0.4 ));
7784
78- if (color != null ){
85+ if (color != null ) {
7986 setTextColor (color );
8087 }
8188
@@ -87,12 +94,12 @@ private void initView(Context context, AttributeSet attrs) {
8794 addView (view );
8895 }
8996
90- public void setFont (Context context , String font ) {
91- textView .setTypeface (Typeface .createFromAsset (context .getAssets (), "fonts/" + font ));
97+ public void setFont (Context context , String font ) {
98+ textView .setTypeface (Typeface .createFromAsset (context .getAssets (), "fonts/" + font ));
9299 }
93100
94- public void setTextColor (ColorStateList color ){
95- if (color != null ){
101+ public void setTextColor (ColorStateList color ) {
102+ if (color != null ) {
96103 textView .setTextColor (color );
97104 }
98105 }
@@ -105,9 +112,29 @@ public void setText(String Text) {
105112 textView .setText (Text );
106113 }
107114
108- public void setRadius (float Radius , int start , int end ) {
109- GradientDrawable drawable = new GradientDrawable (GradientDrawable .Orientation .RIGHT_LEFT , new int [] {start , end });
110- drawable .setCornerRadii (new float [] { Radius , Radius , Radius , Radius , Radius , Radius , Radius , Radius });
115+ public void setRadius (float Radius , int start , int end , int Orientation ) {
116+
117+ GradientDrawable .Orientation orientation = null ;
118+
119+ switch (Orientation ) {
120+ case 1 :
121+ orientation = GradientDrawable .Orientation .RIGHT_LEFT ;
122+ break ;
123+ case 2 :
124+ orientation = GradientDrawable .Orientation .LEFT_RIGHT ;
125+ break ;
126+ case 3 :
127+ orientation = GradientDrawable .Orientation .BOTTOM_TOP ;
128+ break ;
129+ case 4 :
130+ orientation = GradientDrawable .Orientation .TOP_BOTTOM ;
131+ break ;
132+ default :
133+ orientation = GradientDrawable .Orientation .RIGHT_LEFT ;
134+ }
135+
136+ GradientDrawable drawable = new GradientDrawable (orientation , new int []{start , end });
137+ drawable .setCornerRadii (new float []{Radius , Radius , Radius , Radius , Radius , Radius , Radius , Radius });
111138 root .setBackground (drawable );
112139 }
113140}
0 commit comments