AttributeModifierUtil.ets 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { CommonModifier } from '@kit.ArkUI';
  2. export class ShadowModifier extends CommonModifier {
  3. constructor() {
  4. super()
  5. }
  6. applyNormalAttribute(instance: CommonAttribute): void {
  7. instance.shadow({ radius: 26, color: $r('app.color.shadow_color') })
  8. .backgroundColor($r('app.color.start_window_background_blur'))
  9. .backdropBlur(150)
  10. }
  11. }
  12. export class ImageFancyModifier implements AttributeModifier<ImageAttribute> {
  13. private borderRadius: Length | BorderRadiuses | LocalizedBorderRadiuses;
  14. private width: number | string;
  15. private height: number | string;
  16. constructor(borderRadius: Length | BorderRadiuses | LocalizedBorderRadiuses, width: number | string,
  17. height: number | string) {
  18. this.borderRadius = borderRadius;
  19. this.width = width;
  20. this.height = height;
  21. }
  22. applyNormalAttribute(attr: ImageAttribute): void {
  23. attr
  24. .alt($r("app.media.avatar"))
  25. .backgroundImageSize(ImageSize.Auto)
  26. .borderRadius(this.borderRadius)
  27. .width(this.width)
  28. .height(this.height)
  29. .interpolation(ImageInterpolation.Medium)// 用于重采样后的抗锯齿
  30. .draggable(false)// 禁止长按手势拖动
  31. .autoResize(true) // 重采样,可减少内存占用
  32. }
  33. }
  34. export class SymbolGlyphFancyModifier implements AttributeModifier<SymbolGlyphAttribute> {
  35. private fontSize: number;
  36. private width: number | string;
  37. private height: number | string;
  38. constructor(fontSize: number, width: number | string, height: number | string) {
  39. this.fontSize = fontSize;
  40. this.width = width;
  41. this.height = height;
  42. }
  43. applyNormalAttribute(attr: SymbolGlyphAttribute): void {
  44. attr
  45. .fontSize(this.fontSize)
  46. .fontColor([$r('app.color.text_color')])
  47. .width(this.width)
  48. .height(this.height);
  49. }
  50. }
  51. export class ButtonFancyModifier implements AttributeModifier<ButtonAttribute> {
  52. private width: number | string;
  53. private height: number | string;
  54. constructor(width: number | string, height: number | string) {
  55. this.width = width;
  56. this.height = height;
  57. }
  58. applyNormalAttribute(attr: ButtonAttribute): void {
  59. attr
  60. .borderRadius(16)
  61. .clickEffect({ level: ClickEffectLevel.MIDDLE, scale: 0.5 })
  62. .width(this.width)
  63. .height(this.height)
  64. .backgroundColor($r('app.color.start_window_background_blur'))
  65. }
  66. }
  67. export class SelectItemTextFancyModifier implements AttributeModifier<TextAttribute> {
  68. private selected: boolean;
  69. constructor(selected: boolean) {
  70. this.selected = selected
  71. }
  72. applyNormalAttribute(attr: TextAttribute): void {
  73. if (this.selected) {
  74. attr
  75. .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
  76. .fontSize($r('sys.float.ohos_id_text_size_body1'))
  77. .fontWeight(FontWeight.Regular)
  78. } else {
  79. attr
  80. .fontSize($r('sys.float.ohos_id_text_size_body1'))
  81. .fontWeight(FontWeight.Regular)
  82. }
  83. attr.margin({ left: 20, right: 20 })
  84. }
  85. }
  86. export class MenuModifier extends CommonModifier {
  87. constructor() {
  88. super()
  89. }
  90. applyNormalAttribute(instance: MenuAttribute): void {
  91. instance.font({ size: 15, weight: FontWeight.Normal }).radius(16)
  92. }
  93. }