RN Animated动画 - 头像自动循环缩放展示

需求:

类似pdd百亿补贴里面的头像自动循环缩放展示

注意:由于项目需要,加入头像自动循环缩放展示,研究了下动画,以下是效果,可自行在代码中调整动画执行时间

1uw9v-jm8d2.gif

1、创建动画组件

import React from 'react';
import {View, StyleSheet, Animated, Image} from 'react-native';
import px2dp from '../../../utils/px2dp';

const showTime = 2000; //启动时间
//头像动画
export default class HeaderIconView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      marginLeft: new Animated.Value(px2dp(20)),
      iconOneOpacity: new Animated.Value(1),
      iconOneScale: new Animated.Value(1),
      iconFourOpacity: new Animated.Value(0),
      iconFourScale: new Animated.Value(0),
      iconOne: (props.iconList && props.iconList[0]) || '',
      iconTwo: (props.iconList && props.iconList[1]) || '',
      iconThree: (props.iconList && props.iconList[2]) || '',
      iconFour: (props.iconList && props.iconList[3]) || '',
    };
    this.startIndex = 0;
  }

  componentDidMount() {
    const {iconList = []} = this.props;
    if (iconList.length >= 4) {
      this.animatedShowAction();
    }
  }
  animatedShowAction() {
    const {iconList = []} = this.props;
    Animated.parallel([
      Animated.timing(this.state.marginLeft, {
        toValue: 0,
        duration: showTime,
        useNativeDriver: true,
      }),
      Animated.timing(this.state.iconOneOpacity, {
        toValue: 0,
        duration: showTime,
        useNativeDriver: true,
      }),
      Animated.timing(this.state.iconOneScale, {
        toValue: 0,
        duration: showTime,
        useNativeDriver: true,
      }),
      Animated.timing(this.state.iconFourScale, {
        toValue: 1,
        duration: showTime,
        useNativeDriver: true,
      }),
      Animated.timing(this.state.iconFourOpacity, {
        toValue: 1,
        duration: showTime,
        useNativeDriver: true,
      }),
    ]).start(() => {
      ++this.startIndex;
      let twoIndex = this.startIndex + 1;
      let threeIndex = this.startIndex + 2;
      let fourIndex = this.startIndex + 3;
      if (this.startIndex > iconList.length - 1) {
        this.startIndex = 0;
        twoIndex = 1;
        threeIndex = 2;
        fourIndex = 3;
      }
      if (twoIndex > iconList.length - 1) {
        twoIndex = 0;
        threeIndex = 1;
        fourIndex = 2;
      }
      if (threeIndex > iconList.length - 1) {
        threeIndex = 0;
        fourIndex = 1;
      }
      if (fourIndex > iconList.length - 1) {
        fourIndex = 0;
      }

      this.setState(
        {
          marginLeft: new Animated.Value(px2dp(20)),
          iconOneOpacity: new Animated.Value(1),
          iconOneScale: new Animated.Value(1),
          iconFourScale: new Animated.Value(0),
          iconFourOpacity: new Animated.Value(0),
          iconOne: iconList[this.startIndex],
          iconTwo: iconList[twoIndex],
          iconThree: iconList[threeIndex],
          iconFour: iconList[fourIndex],
        },
        () => {
          this.animatedShowAction();
        },
      );
    });
  }

  render() {
    const {
      iconOne = '',
      iconTwo = '',
      iconThree = '',
      iconFour = '',
    } = this.state;
    return (
      <Animated.View
        style={{
          flexDirection: 'row',
          width: px2dp(100),
          transform: [
            {
              translateX: this.state.marginLeft,
            },
          ],
        }}>
        <Animated.View
          style={[
            styles.topTwoIconView,
            {
              opacity: this.state.iconOneOpacity,
              transform: [{scale: this.state.iconOneScale}],
            },
          ]}>
          <Image
            style={styles.iconView}
            source={{
              uri: iconOne,
            }}
            fadeDuration={0}
            contentMode="stretch"
          />
        </Animated.View>
        <View
          style={[styles.topTwoIconView, {zIndex: 3, marginLeft: -px2dp(20)}]}>
          <Image
            style={styles.iconView}
            source={{
              uri: iconTwo,
            }}
            fadeDuration={0}
            contentMode="stretch"
          />
        </View>
        <View
          style={[styles.topTwoIconView, {zIndex: 2, marginLeft: -px2dp(20)}]}>
          <Image
            style={styles.iconView}
            source={{
              uri: iconThree,
            }}
            fadeDuration={0}
            contentMode="stretch"
          />
        </View>
        <Animated.View
          style={[
            styles.topTwoIconView,
            {
              zIndex: 1,
              marginLeft: -px2dp(20),
              opacity: this.state.iconFourScale,
              transform: [{scale: this.state.iconFourScale}],
            },
          ]}>
          <Image
            style={styles.iconView}
            source={{
              uri: iconFour,
            }}
            fadeDuration={0}
            contentMode="stretch"
          />
        </Animated.View>
      </Animated.View>
    );
  }
}
const styles = StyleSheet.create({
  topTwoIconView: {
    width: px2dp(40),
    height: px2dp(40),
    borderRadius: px2dp(20),
    borderWidth: px2dp(2),
    zIndex: 4,
    borderColor: '#ffffff',
    justifyContent: 'center',
    alignItems: 'center',
    overflow: 'hidden',
  },
  iconView: {
    width: px2dp(40),
    height: px2dp(40),
    borderRadius: px2dp(20),
  },
});

2、引入动画组件

const iconList=['','','','',''];
 <HeaderBIconView iconList={iconList} />
以上就是我的头像自动循环播放的功能代码,欢迎各位童鞋评论、指导,谢谢!

PS (以上播放的图片大都是从网上找到,如若有侵权,请联系我马上删除,谢谢)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,997评论 2 59
  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 4,349评论 0 13
  • 夜莺2517阅读 127,761评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 6,963评论 1 6
  • 我是一名过去式的高三狗,很可悲,在这三年里我没有恋爱,看着同龄的小伙伴们一对儿一对儿的,我的心不好受。怎么说呢,高...
    小娘纸阅读 3,417评论 4 7