|
| 1 | +import { LinearGradient } from 'expo-linear-gradient' |
| 2 | +import { View } from 'react-native' |
| 3 | +import { useTheme } from 'react-native-paper' |
| 4 | +import Animated, { Extrapolation, interpolate, useAnimatedStyle } from 'react-native-reanimated' |
| 5 | +import { useArtworkColors } from '@/hooks/useArtworkColors' |
| 6 | +import { usePlayerAnimation } from './Context' |
| 7 | + |
| 8 | +interface PlayerBackgroundProps { |
| 9 | + artworkUrl?: string |
| 10 | +} |
| 11 | + |
| 12 | +export default function PlayerBackground({ artworkUrl }: PlayerBackgroundProps) { |
| 13 | + const { dominant, vibrant, muted } = useArtworkColors(artworkUrl) |
| 14 | + const { colors } = useTheme() |
| 15 | + const { thresholdPercent } = usePlayerAnimation() |
| 16 | + |
| 17 | + // 确保颜色值有效 |
| 18 | + const safeVibrant = vibrant || colors.primary |
| 19 | + const safeDominant = dominant || colors.secondary |
| 20 | + const safeMuted = muted || colors.outline |
| 21 | + |
| 22 | + const baseStyle = { |
| 23 | + flex: 1, |
| 24 | + position: 'absolute' as const, |
| 25 | + top: 0, |
| 26 | + left: 0, |
| 27 | + right: 0, |
| 28 | + bottom: 0, |
| 29 | + } |
| 30 | + |
| 31 | + const animatedStyle = useAnimatedStyle(() => ({ |
| 32 | + opacity: interpolate( |
| 33 | + thresholdPercent.value, |
| 34 | + [0, 1], |
| 35 | + [0, 1], |
| 36 | + Extrapolation.CLAMP, |
| 37 | + ), |
| 38 | + })) |
| 39 | + |
| 40 | + return ( |
| 41 | + <Animated.View style={[baseStyle, animatedStyle]}> |
| 42 | + {/* 基础背景色 */} |
| 43 | + <View style={[baseStyle, { backgroundColor: colors.elevation.level1 }]} /> |
| 44 | + |
| 45 | + {/* 从专辑中心向上左发散 */} |
| 46 | + <LinearGradient |
| 47 | + colors={[ |
| 48 | + `${safeDominant}25`, // 专辑区域:更柔和 |
| 49 | + `${safeMuted}15`, // 中间区域:更淡 |
| 50 | + 'transparent', // 边缘:透明 |
| 51 | + ]} |
| 52 | + style={baseStyle} |
| 53 | + start={{ x: 0.5, y: 0.6 }} // 从专辑位置开始 |
| 54 | + end={{ x: 0.3, y: 0 }} // 向上左斜发散 |
| 55 | + locations={[0, 0.4, 1]} |
| 56 | + /> |
| 57 | + |
| 58 | + {/* 从专辑中心向下右发散 */} |
| 59 | + <LinearGradient |
| 60 | + colors={[ |
| 61 | + `${safeVibrant}20`, // 专辑区域:更柔和 |
| 62 | + `${safeMuted}12`, // 中间区域:更淡 |
| 63 | + 'transparent', // 边缘:透明 |
| 64 | + ]} |
| 65 | + style={baseStyle} |
| 66 | + start={{ x: 0.5, y: 0.6 }} // 从专辑位置开始 |
| 67 | + end={{ x: 0.7, y: 1 }} // 向下右斜发散 |
| 68 | + locations={[0, 0.5, 1]} |
| 69 | + /> |
| 70 | + |
| 71 | + {/* 从专辑中心向左下发散 */} |
| 72 | + <LinearGradient |
| 73 | + colors={[ |
| 74 | + `${safeDominant}25`, // 专辑区域:增加亮度 |
| 75 | + `${safeMuted}15`, // 中间区域:增加亮度 |
| 76 | + 'transparent', // 边缘:透明 |
| 77 | + ]} |
| 78 | + style={baseStyle} |
| 79 | + start={{ x: 0.5, y: 0.6 }} // 从专辑位置开始 |
| 80 | + end={{ x: 0, y: 0.8 }} // 向左下斜发散 |
| 81 | + locations={[0, 0.6, 1]} |
| 82 | + /> |
| 83 | + |
| 84 | + {/* 从专辑中心向右上发散 */} |
| 85 | + <LinearGradient |
| 86 | + colors={[ |
| 87 | + `${safeVibrant}25`, // 专辑区域:增加亮度 |
| 88 | + `${safeMuted}15`, // 中间区域:增加亮度 |
| 89 | + 'transparent', // 边缘:透明 |
| 90 | + ]} |
| 91 | + style={baseStyle} |
| 92 | + start={{ x: 0.5, y: 0.6 }} // 从专辑位置开始 |
| 93 | + end={{ x: 1, y: 0.4 }} // 向右上斜发散 |
| 94 | + locations={[0, 0.6, 1]} |
| 95 | + /> |
| 96 | + |
| 97 | + {/* 对角线发散增强效果 */} |
| 98 | + <LinearGradient |
| 99 | + colors={[ |
| 100 | + `${safeDominant}12`, // 专辑区域:非常柔和 |
| 101 | + 'transparent', // 边缘:透明 |
| 102 | + ]} |
| 103 | + style={baseStyle} |
| 104 | + start={{ x: 0.5, y: 0.6 }} // 从专辑位置开始 |
| 105 | + end={{ x: 0, y: 0 }} // 向左上角发散 |
| 106 | + locations={[0, 1]} |
| 107 | + /> |
| 108 | + |
| 109 | + <LinearGradient |
| 110 | + colors={[ |
| 111 | + `${safeVibrant}12`, // 专辑区域:非常柔和 |
| 112 | + 'transparent', // 边缘:透明 |
| 113 | + ]} |
| 114 | + style={baseStyle} |
| 115 | + start={{ x: 0.5, y: 0.6 }} // 从专辑位置开始 |
| 116 | + end={{ x: 1, y: 0 }} // 向右上角发散 |
| 117 | + locations={[0, 1]} |
| 118 | + /> |
| 119 | + </Animated.View> |
| 120 | + ) |
| 121 | +} |
0 commit comments