Skip to content

Commit 2152bf0

Browse files
committed
add orientation
1 parent 10ddc36 commit 2152bf0

4 files changed

Lines changed: 53 additions & 16 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Add it in your root build.gradle at the end of repositories:
3030

3131
```gradle
3232
dependencies {
33-
implementation 'com.github.Far5had70:GradientButton:1.0.0'
33+
implementation 'com.github.Far5had70:GradientButton:1.1.0'
3434
}
3535
```
3636

@@ -50,7 +50,7 @@ Step 2. Add the dependency
5050

5151
## Demo
5252

53-
You can try it out here [Sample Application](https://github.com/Far5had70/GradientButton/blob/master/app/src/main/res/layout/activity_main.xml)
53+
You can try it out here [Sample View](https://github.com/Far5had70/GradientButton/blob/master/app/src/main/res/layout/activity_main.xml)
5454

5555

5656

@@ -62,7 +62,7 @@ You can try it out here [Sample Application](https://github.com/Far5had70/Gradie
6262
**Example:**
6363

6464
```xml
65-
<com.waspar.gradientbutton.GradientButton
65+
<com.waspar.gradientbutton.GradientButton
6666
android:layout_width="match_parent"
6767
android:layout_height="50dp"
6868
android:layout_marginTop="50dp"
@@ -74,6 +74,7 @@ You can try it out here [Sample Application](https://github.com/Far5had70/Gradie
7474
app:startColor="@color/colorBlue_A400"
7575
app:endColor="@color/colorBlue_900"
7676
app:textColor="#fff"
77+
app:orientation="right_left"
7778
/>
7879
```
7980

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
app:startColor="@color/colorBlue_A400"
2020
app:endColor="@color/colorBlue_900"
2121
app:textColor="#fff"
22+
app:orientation="right_left"
2223
/>
2324

2425
</RelativeLayout>

gradientbutton/src/main/java/com/waspar/gradientbutton/GradientButton.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

gradientbutton/src/main/res/values/attrs.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
<attr name="endColor" format="reference"/>
99
<attr name="textColor" format="color"/>
1010
<attr name="typeface" format="string"/>
11+
<attr name="orientation"/>
1112
</declare-styleable>
1213

14+
<attr name="orientation">
15+
<flag name="right_left" value="1" />
16+
<flag name="left_right" value="2" />
17+
<flag name="bottom_top" value="3" />
18+
<flag name="top_bottom" value="4" />
19+
</attr>
20+
1321
</resources>

0 commit comments

Comments
 (0)