|
615 | 615 | ```java |
616 | 616 | ColorRoles colorRoles = MaterialColors.getColorRoles(color, /* isLightTheme= */ booleanValue); |
617 | 617 | ``` |
| 618 | + |
| 619 | +## Content-based Dynamic Colors |
| 620 | + |
| 621 | +Content-based color describes the color system’s capability to generate and |
| 622 | +apply a color scheme based on in-app content. In-app content colors can be |
| 623 | +derived from a range of sources, such as album artwork, a brand logo, or a video |
| 624 | +tile. |
| 625 | + |
| 626 | +##### *Use Content-based Dynamic Colors* |
| 627 | + |
| 628 | +A single source color is extracted from a bitmap and then used to derive five |
| 629 | +key colors. Specific tones are mapped into specific color roles that are then |
| 630 | +mapped to Material components. |
| 631 | + |
| 632 | +During this process, chroma fidelity enables Material colors to flex to |
| 633 | +consistently achieve desired chroma, whether high or low. It maintains color |
| 634 | +schemes’ integrity, so existing products will not break. A content scheme then |
| 635 | +produces the range of tones needed for both light and dark theme applications. |
| 636 | + |
| 637 | +We have provided the following two APIs in the `DynamicColorsOptions` class. |
| 638 | + |
| 639 | +API Method | Description |
| 640 | +------------------------------ | --------------------------------------- |
| 641 | +#setContentBasedSource(Bitmap) | Provides a Bitmap from which a single source color is extracted as input |
| 642 | +#setContentBasedSource(int) | Provides a single source color as input |
| 643 | + |
| 644 | +An example usage for applying content-based dynamic colors to a specific |
| 645 | +activity can be seen below. Since we are overriding color resources in xml at |
| 646 | +runtime, make sure the method is invoked before you inflate the view to take |
| 647 | +effect. |
| 648 | + |
| 649 | +``` |
| 650 | +import com.google.android.material.color.DynamicColorsOptions; |
| 651 | +import com.google.android.material.color.DynamicColors; |
| 652 | +
|
| 653 | + @Override |
| 654 | + protected void onCreate(Bundle savedInstanceState) { |
| 655 | +
|
| 656 | + // Invoke before the view is inflated in your activity. |
| 657 | + DynamicColors.applyToActivityIfAvailable( |
| 658 | + this, |
| 659 | + new DynamicColorsOptions.Builder() |
| 660 | + .setContentBasedSource(bitmap) |
| 661 | + .build() |
| 662 | + ); |
| 663 | +
|
| 664 | + setContentView(R.layout.xyz); |
| 665 | + } |
| 666 | +``` |
| 667 | + |
| 668 | +An example usage for applying content-based dynamic colors to a specific |
| 669 | +fragment/view: |
| 670 | + |
| 671 | +``` |
| 672 | +import com.google.android.material.color.DynamicColorsOptions; |
| 673 | +import com.google.android.material.color.DynamicColors; |
| 674 | +
|
| 675 | + @Override |
| 676 | + public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) { |
| 677 | +
|
| 678 | + Context context = DynamicColors.wrapContextIfAvailable( |
| 679 | + requireContext(), |
| 680 | + new DynamicColorsOptions.Builder() |
| 681 | + .setContentBasedSource(sourceColor) |
| 682 | + .build()); |
| 683 | +
|
| 684 | + return layoutInflater.cloneInContext(context).inflate(R.layout.xyz, viewGroup, false); |
| 685 | + } |
| 686 | +``` |
| 687 | + |
| 688 | +This method will return a context with a content-based dynamic colors theme |
| 689 | +overlay applied, if Dynamic Colors are available on the device. |
| 690 | + |
| 691 | +**Important:** Please note that this feature is only available for S+. |
| 692 | + |
| 693 | +## Contrast Control |
| 694 | + |
| 695 | +Tone quantifies the lightness or darkness of colors. It's one foundational |
| 696 | +dimension of the Material color system and schemes. The difference in tone |
| 697 | +between two colors creates visual contrast. A greater difference creates higher |
| 698 | +contrast. Color contrast control allows users to adjust their UI contrast levels |
| 699 | +in the system so they can comfortably see and use digital experiences. |
| 700 | + |
| 701 | +##### *Use Contrast Control - non-Dynamic* |
| 702 | + |
| 703 | +If you are not using dynamic colors and would like to use contrast control for |
| 704 | +your branded or custom themes, we have created the following API in the |
| 705 | +`ColorContrast` class that you can call manually. |
| 706 | + |
| 707 | +*Apply contrast to all activities in the app* |
| 708 | + |
| 709 | +In your application class’ `onCreate()` method, call: |
| 710 | + |
| 711 | +``` |
| 712 | +ColorContrast.applyToActivitiesIfAvailable( |
| 713 | + this, |
| 714 | + new ColorContrastOptions.Builder() |
| 715 | + .setMediumContrastThemeOverlay(mediumContrastThemeOverlayResId) |
| 716 | + .setHighContrastThemeOverlay(highContrastThemeOverlayResId) |
| 717 | + .build(); |
| 718 | +); |
| 719 | +``` |
| 720 | + |
| 721 | +Note that if you want contrast support for both light and dark theme, then for |
| 722 | +`mediumContrastThemeOverlayResId` and `highContrastThemeOverlayResId`, you |
| 723 | +should pass in a DayNight theme, which will help facilitate easy switching |
| 724 | +between your app’s Light and Dark theme. |
| 725 | + |
| 726 | +##### *Use Contrast Control - Custom Colors* |
| 727 | + |
| 728 | +If you have custom colors in your app that would like to obey contrast changes |
| 729 | +from the system, whether or not you are using dynamic colors, they should be |
| 730 | +included in the abovementioned theme overlays for medium and high contrast |
| 731 | +support. To make your custom colors obey contrast for all activities in the app, |
| 732 | +please refer to the API from the section above. |
0 commit comments