Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 42069e4

Browse files
committed
Add LinearGradientDrawable convenience Builder
1 parent a09586b commit 42069e4

File tree

3 files changed

+126
-20
lines changed

3 files changed

+126
-20
lines changed

android-ui-toolkit/src/main/java/io/supernova/uitoolkit/drawable/LinearGradientDrawable.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.supernova.uitoolkit.drawable;
22

3+
import android.content.Context;
34
import android.graphics.Canvas;
45
import android.graphics.Color;
56
import android.graphics.ColorFilter;
@@ -14,9 +15,14 @@
1415
import android.graphics.drawable.Drawable;
1516
import android.os.Build;
1617
import android.support.annotation.ColorInt;
18+
import android.support.annotation.ColorRes;
19+
import android.support.annotation.DimenRes;
20+
import android.support.annotation.Dimension;
1721
import android.support.annotation.NonNull;
1822
import android.support.annotation.Nullable;
1923
import android.support.annotation.RequiresApi;
24+
import android.support.v4.content.ContextCompat;
25+
import android.util.TypedValue;
2026

2127
import java.util.ArrayList;
2228
import java.util.Arrays;
@@ -209,4 +215,103 @@ private RectF getFloatReusableBounds() {
209215
private boolean hasStroke() {
210216
return this.strokePaint.getStrokeWidth() != 0;
211217
}
218+
219+
220+
public static class Builder {
221+
222+
@NonNull
223+
private Context context;
224+
225+
private final PointF start;
226+
private final PointF end;
227+
228+
private final List<GradientStop> stops = new ArrayList<>();
229+
230+
private float cornerRadius = 0f;
231+
private float strokeWidth = 0f;
232+
@ColorInt
233+
private int strokeColor = Color.TRANSPARENT;
234+
235+
236+
public Builder(@NonNull Context context, @NonNull PointF start, @NonNull PointF end) {
237+
this.context = context;
238+
this.start = start;
239+
this.end = end;
240+
}
241+
242+
243+
/* STOPS */
244+
245+
public Builder addStop(GradientStop stop) {
246+
this.stops.add(stop);
247+
return this;
248+
}
249+
250+
public Builder addStop(float position, @ColorInt int color) {
251+
return this.addStop(new GradientStop(position, color));
252+
}
253+
254+
public Builder addStopWithResource(float position, @ColorRes int color) {
255+
return this.addStop(new GradientStop(position, ContextCompat.getColor(context, color)));
256+
}
257+
258+
259+
/* RAW VALUES */
260+
261+
public Builder cornerRadiusPx(@Dimension float cornerRadius) {
262+
this.cornerRadius = cornerRadius;
263+
return this;
264+
}
265+
266+
public Builder strokeWidthPx(@Dimension float width) {
267+
this.strokeWidth = width;
268+
return this;
269+
}
270+
271+
public Builder cornerRadiusDp(@Dimension float radius) {
272+
this.cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, this.context.getResources().getDisplayMetrics());
273+
return this;
274+
}
275+
276+
public Builder strokeWidthDp(@Dimension float width) {
277+
this.strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, this.context.getResources().getDisplayMetrics());
278+
return this;
279+
}
280+
281+
public Builder strokeColor(@ColorInt int color) {
282+
this.strokeColor = color;
283+
return this;
284+
}
285+
286+
287+
/* RESOURCE VALUES */
288+
289+
public Builder cornerRadius(@DimenRes int cornerRadiusRes) {
290+
this.cornerRadius = this.context.getResources().getDimension(cornerRadiusRes);
291+
return this;
292+
}
293+
294+
public Builder strokeWidth(@DimenRes int strokeWidthRes) {
295+
this.strokeWidth = this.context.getResources().getDimension(strokeWidthRes);
296+
return this;
297+
}
298+
299+
public Builder strokeColorRes(@ColorRes int strokeColorRes) {
300+
this.strokeColor = ContextCompat.getColor(this.context, strokeColorRes);
301+
return this;
302+
}
303+
304+
305+
/* BUILD */
306+
307+
public LinearGradientDrawable build() {
308+
LinearGradientDrawable gradient = new LinearGradientDrawable(this.start, this.end, this.stops);
309+
310+
gradient.setStrokeColor(this.strokeColor);
311+
gradient.setStrokeWidth(this.strokeWidth);
312+
gradient.setCornerRadius(this.cornerRadius);
313+
314+
return gradient;
315+
}
316+
}
212317
}

app/src/main/java/io/supernova/toolkit/example/MainActivity.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
import android.graphics.Color;
44
import android.graphics.PointF;
5-
import android.graphics.drawable.GradientDrawable;
65
import android.os.Bundle;
76
import android.support.v7.app.AppCompatActivity;
87
import android.text.SpannableString;
98
import android.text.Spanned;
10-
import android.util.TypedValue;
119
import android.view.View;
1210
import android.widget.TextView;
1311

1412
import io.supernova.supernovauitoolkit.R;
15-
import io.supernova.uitoolkit.drawable.GradientStop;
1613
import io.supernova.uitoolkit.drawable.LinearGradientDrawable;
1714
import io.supernova.uitoolkit.text.LetterSpacingSpan;
1815
import io.supernova.uitoolkit.text.LineHeightSpan;
@@ -83,26 +80,26 @@ private void setupLetterSpacingExample() {
8380

8481
private void setupGradient() {
8582

86-
// Create GradientStop array defining colors their position
87-
// GradientStop position must be between 0 and 1
88-
GradientStop[] stops = {
89-
new GradientStop(0, Color.BLUE),
90-
new GradientStop(0.3f, Color.BLACK),
91-
new GradientStop(0.6f, Color.WHITE),
92-
new GradientStop(1, Color.RED)
93-
};
83+
// Create gradient and pass it as the background to the view
84+
//
85+
// LinearGradientDrawable.Builder requires context to convert resource references and DP dimensions to pixels and
86+
// start and end points that define the gradient orientation.
87+
this.gradientView.setBackground(new LinearGradientDrawable.Builder(this, new PointF(0, 0), new PointF(1, 1))
88+
// Stops
89+
.addStop(0, Color.BLUE)
90+
.addStop(0.3f, Color.BLACK)
91+
.addStop(0.6f, Color.WHITE)
92+
.addStopWithResource(1, R.color.colorPrimaryDark)
9493

95-
// Create LinearGradientDrawable with the stops, define gradient angle with 2 points
96-
LinearGradientDrawable linearGradientDrawable = new LinearGradientDrawable(new PointF(0, 0), new PointF(1, 1), stops);
94+
// Corner radius resource
95+
.cornerRadius(R.dimen.test_gradient_corner_radius)
9796

98-
// Set gradient corner radius in DPs
99-
linearGradientDrawable.setCornerRadius(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, this.getResources().getDisplayMetrics()));
97+
// Stroke width converted from DPs
98+
.strokeWidthDp(2)
10099

101-
// Set gradient stroke
102-
linearGradientDrawable.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, this.getResources().getDisplayMetrics()));
103-
linearGradientDrawable.setStrokeColor(Color.BLACK);
100+
// Stroke color resource
101+
.strokeColorRes(R.color.colorPrimary)
104102

105-
// Pass gradient to the view
106-
this.gradientView.setBackground(linearGradientDrawable);
103+
.build());
107104
}
108105
}

app/src/main/res/values/dimens.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="test_gradient_corner_radius">8dp</dimen>
4+
</resources>

0 commit comments

Comments
 (0)