要实现流畅的底部导航栏多图动画切换效果,可以考虑以下几种技术方案:
// React Native示例
import Lottie from 'lottie-react-native';
function TabIcon({ focused, animation }) {
return (
<Lottie
source={animation}
progress={focused ? 1 : 0}
speed={focused ? 1 : -1}
autoPlay={false}
loop={false}
/>
);
}
/* CSS示例 */
.tab-icon {
transition: all 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
transform: scale(1);
}
.tab-icon.active {
transform: scale(1.2);
filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}
// Android示例
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0.8f, 1.2f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0.8f, 1.2f);
AnimatorSet set = new AnimatorSet();
set.playTogether(scaleX, scaleY);
set.setDuration(300);
set.setInterpolator(new OvershootInterpolator());
set.start();
// iOS示例
UIView.animate(withDuration: 0.3,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 0.5,
options: .curveEaseInOut,
animations: {
icon.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
},
completion: nil)
性能优化:
流畅度优化:
视觉优化:
AnimatedContainer(
duration: Duration(milliseconds: 300),
curve: Curves.easeInOut,
transform: isSelected
? Matrix4.identity()..scale(1.2)
: Matrix4.identity(),
child: Image.asset(
isSelected ? 'selected_icon.png' : 'normal_icon.png',
width: 24,
height: 24,
),
)
import { motion } from 'framer-motion';
function TabIcon({ isActive }) {
return (
<motion.div
animate={{
scale: isActive ? 1.2 : 1,
opacity: isActive ? 1 : 0.6
}}
transition={{ type: "spring", stiffness: 500, damping: 30 }}
>
<img src={isActive ? activeIcon : inactiveIcon} />
</motion.div>
);
}
选择哪种方案取决于您的具体需求、目标平台和性能要求。Lottie和CSS方案适合Web和跨平台应用,而原生方案能提供最佳性能。