RotatingCover.ets 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2024 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { displaySync } from '@kit.ArkGraphics2D';
  16. import { PreferencesUtil, StrUtil } from '@pura/harmony-utils';
  17. import { CommonConstants } from '../common/constants/CommonConstants';
  18. @Component
  19. export struct RotatingCover {
  20. @StorageLink('isPlaying') @Watch('animationFun') isPlaying: boolean = false;
  21. @Prop songLabel: string;
  22. @State timer: number = 0
  23. @State rotateAngle: number = 0
  24. @StorageProp('themeColor') themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
  25. //封面旋转动画
  26. animationFun() {
  27. if (this.isPlaying) {
  28. this.timer = setInterval(()=>{
  29. this.rotateAngle += 1
  30. }, 100)
  31. } else {
  32. clearInterval(this.timer)
  33. }
  34. }
  35. aboutToAppear() {
  36. let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', CommonConstants.DEFAULT_THEME_COLOR);
  37. AppStorage.setOrCreate('themeColor', themeColor);
  38. this.themeColor=themeColor
  39. }
  40. aboutToDisappear(): void {
  41. }
  42. build() {
  43. Column() {
  44. Image(this.songLabel)
  45. .height(42)
  46. .width(42)
  47. .alt( $r('app.media.music_red'))
  48. .fillColor(this.themeColor)
  49. .borderRadius('100%')
  50. .shadow({
  51. radius: 10,
  52. type: ShadowType.BLUR,
  53. color: 'on_primary'
  54. })
  55. .clip(true)
  56. .margin({ right: 12 })
  57. .rotate({
  58. angle: this.rotateAngle,
  59. centerX: "50%",
  60. centerY: "50%",
  61. })
  62. .animation({
  63. duration: 100,
  64. curve: Curve.Linear
  65. })
  66. }
  67. }
  68. }