|
1 | 1 | package io.supernova.uitoolkit.drawable; |
2 | 2 |
|
| 3 | +import android.content.Context; |
3 | 4 | import android.graphics.Canvas; |
4 | 5 | import android.graphics.Color; |
5 | 6 | import android.graphics.ColorFilter; |
|
14 | 15 | import android.graphics.drawable.Drawable; |
15 | 16 | import android.os.Build; |
16 | 17 | import android.support.annotation.ColorInt; |
| 18 | +import android.support.annotation.ColorRes; |
| 19 | +import android.support.annotation.DimenRes; |
| 20 | +import android.support.annotation.Dimension; |
17 | 21 | import android.support.annotation.NonNull; |
18 | 22 | import android.support.annotation.Nullable; |
19 | 23 | import android.support.annotation.RequiresApi; |
| 24 | +import android.support.v4.content.ContextCompat; |
| 25 | +import android.util.TypedValue; |
20 | 26 |
|
21 | 27 | import java.util.ArrayList; |
22 | 28 | import java.util.Arrays; |
@@ -209,4 +215,103 @@ private RectF getFloatReusableBounds() { |
209 | 215 | private boolean hasStroke() { |
210 | 216 | return this.strokePaint.getStrokeWidth() != 0; |
211 | 217 | } |
| 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 | + } |
212 | 317 | } |
0 commit comments