|
2 | 2 |
|
3 | 3 | import android.content.Context; |
4 | 4 | import android.content.res.ColorStateList; |
| 5 | +import android.graphics.Bitmap; |
| 6 | +import android.graphics.Canvas; |
5 | 7 | import android.graphics.Color; |
| 8 | +import android.graphics.Paint; |
| 9 | +import android.graphics.Path; |
| 10 | +import android.graphics.drawable.BitmapDrawable; |
6 | 11 | import android.graphics.drawable.Drawable; |
7 | 12 | import android.graphics.drawable.GradientDrawable; |
8 | 13 | import android.graphics.drawable.LayerDrawable; |
|
13 | 18 | import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand; |
14 | 19 | import com.beardedhen.androidbootstrap.api.attributes.ViewGroupPosition; |
15 | 20 | import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand; |
| 21 | +import com.beardedhen.androidbootstrap.api.defaults.ExpandDirection; |
16 | 22 | import com.beardedhen.androidbootstrap.utils.ColorUtils; |
17 | 23 | import com.beardedhen.androidbootstrap.utils.DimenUtils; |
18 | 24 |
|
@@ -314,6 +320,74 @@ private static int[] getColorList(int defaultColor, int activeColor, int disable |
314 | 320 | } |
315 | 321 | } |
316 | 322 |
|
| 323 | + static StateListDrawable bootstrapDropDownArrow(Context context, int width, int height, ExpandDirection expandDirection, boolean outline, BootstrapBrand brand) { |
| 324 | + StateListDrawable stateListDrawable = new StateListDrawable(); |
| 325 | + |
| 326 | + int defaultColor = outline ? brand.defaultFill(context) : brand.defaultTextColor(context); |
| 327 | + int activeColor = outline ? ColorUtils.resolveColor(android.R.color.white, context) : brand.activeTextColor(context); |
| 328 | + int disabledColor = outline ? brand.disabledFill(context) : brand.disabledTextColor(context); |
| 329 | + |
| 330 | + if (Build.VERSION.SDK_INT >= 14) { |
| 331 | + stateListDrawable.addState(new int[]{android.R.attr.state_hovered}, createArrowIcon(context, width, height, activeColor, expandDirection)); |
| 332 | + } |
| 333 | + |
| 334 | + stateListDrawable.addState(new int[]{android.R.attr.state_activated}, createArrowIcon(context, width, height, activeColor, expandDirection)); |
| 335 | + stateListDrawable.addState(new int[]{android.R.attr.state_focused}, createArrowIcon(context, width, height, activeColor, expandDirection)); |
| 336 | + stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, createArrowIcon(context, width, height, activeColor, expandDirection)); |
| 337 | + stateListDrawable.addState(new int[]{android.R.attr.state_selected}, createArrowIcon(context, width, height, activeColor, expandDirection)); |
| 338 | + stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, createArrowIcon(context, width, height, disabledColor, expandDirection)); |
| 339 | + stateListDrawable.addState(new int[]{}, createArrowIcon(context, width, height, defaultColor, expandDirection)); |
| 340 | + return stateListDrawable; |
| 341 | + } |
| 342 | + |
| 343 | + /** |
| 344 | + * Creates arrow icon that depends on ExpandDirection |
| 345 | + * |
| 346 | + * @param context context |
| 347 | + * @param width width of icon in pixels |
| 348 | + * @param height height of icon in pixels |
| 349 | + * @param color arrow color |
| 350 | + * @param expandDirection arrow direction |
| 351 | + * @return icon drawable |
| 352 | + */ |
| 353 | + private static Drawable createArrowIcon(Context context, int width, int height, int color, ExpandDirection expandDirection) { |
| 354 | + Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 355 | + Canvas canvas = new Canvas(canvasBitmap); |
| 356 | + Paint paint = new Paint(); |
| 357 | + paint.setStyle(Paint.Style.FILL_AND_STROKE); |
| 358 | + paint.setStrokeWidth(1); |
| 359 | + paint.setColor(color); |
| 360 | + paint.setAntiAlias(true); |
| 361 | + Path path = new Path(); |
| 362 | + path.setFillType(Path.FillType.EVEN_ODD); |
| 363 | + switch (expandDirection) { |
| 364 | + case UP: |
| 365 | + path.moveTo(0, (height / 3) * 2); |
| 366 | + path.lineTo(width, (height / 3) * 2); |
| 367 | + path.lineTo(width / 2, height / 3); |
| 368 | + path.lineTo(0, (height / 3) * 2); |
| 369 | + break; |
| 370 | + case DOWN: |
| 371 | + path.moveTo(0, height / 3); |
| 372 | + path.lineTo(width, height / 3); |
| 373 | + path.lineTo(width / 2, (height / 3) * 2); |
| 374 | + path.lineTo(0, height / 3); |
| 375 | + break; |
| 376 | + } |
| 377 | + path.close(); |
| 378 | + canvas.drawPath(path, paint); |
| 379 | + return new BitmapDrawable(context.getResources(), canvasBitmap); |
| 380 | + } |
| 381 | + |
| 382 | + static ColorStateList bootstrapDropDownViewText(Context context) { |
| 383 | + |
| 384 | + int defaultColor = context.getResources().getColor(R.color.bootstrap_gray_dark); |
| 385 | + int activeColor = context.getResources().getColor(android.R.color.black); |
| 386 | + int disabledColor = context.getResources().getColor(R.color.bootstrap_gray_light); |
| 387 | + |
| 388 | + return new ColorStateList(getStateList(), getColorList(defaultColor, activeColor, disabledColor)); |
| 389 | + } |
| 390 | + |
317 | 391 | private static void setInsetOnLayers(LayerDrawable[] ary, int l, int t, int r, int b) { |
318 | 392 | for (LayerDrawable ld : ary) { |
319 | 393 | ld.setLayerInset(0, l, t, r, b); |
|
0 commit comments