RotatingCover.ets 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 === undefined ||StrUtil.isEmpty(this.songLabel)? $r('app.media.music_red') : this.songLabel)
  45. .height(38)
  46. .width(38)
  47. .alt( $r('app.media.music_red'))
  48. .fillColor(this.themeColor)
  49. .borderRadius('100%')
  50. .clip(true)
  51. .margin({ right: 12 })
  52. .rotate({
  53. angle: this.rotateAngle,
  54. centerX: "50%",
  55. centerY: "50%",
  56. })
  57. .animation({
  58. duration: 100,
  59. curve: Curve.Linear
  60. })
  61. }
  62. }
  63. }