閃屏(Splash)

好久沒弄RN了, 寫個如何實現閃屏的文章吧.

注意:
(1) 如何切換頁面.
(2) 如何使用計時器TimerMixin.
(3) 如何使用動畫效果.
(4) 如何加載Android的項目資源(圖片).

效果

1. 準備

新建項目, 添加主模塊index.android.js.

/* @flow */
/**
 * 測試
 * @author wangchenlong
 */
'use strict';

var React = require('react-native');
var {
  AppRegistry,
  } = React;

var LearningRN = require('./main_modules/index.js');

AppRegistry.registerComponent('LearningRN', () => LearningRN);

/*@flow*/作為跳轉和檢查的注解. 參考.

2. 首頁

主要包含閃屏和主頁, 使用Navigator的棧, 用于添加額外的頁面.

/* @flow*/
'use strict';

var React = require('react-native');
var {
  Text,
  View,
  Navigator,
  BackAndroid,
} = React;

var styles = require('./styles'); // 樣式

var TimerMixin = require('react-timer-mixin'); // RN的計時器

var SplashScreen = require('./splash_screen/index'); // 飛屏

var MainScreen = require('./main_screen/index'); // 主屏

var _navigator; // 頁面管理器


// 后退按鈕
BackAndroid.addEventListener('hardwareBackPress', function () {
  if (_navigator && _navigator.getCurrentRoutes().length > 1) {
    _navigator.pop();
    return true;
  }
  return false;
});

var LearningRN = React.createClass({

  mixins: [TimerMixin], // 延遲器

  // 初始化狀態
  getInitialState: function () {
    return {
      splashed: true
    };
  },

  // 頁面加載
  componentDidMount: function () {
    this.setTimeout(
      ()=> {
        this.setState({splashed: false});
      }, 2000);
    },

    // 線路映射
    routeMapper: function (route: Map, navigator: Navigator) {
      _navigator = navigator;

      if (route.name === 'home') {
        return (
          <View style={styles.container}>
            <MainScreen/>
          </View>
        );
      }
    },

    render: function () {
      if (!this.state.splashed) {
        // 初始路徑
        var initialRoute = {name: 'home'};

        return (
          <Navigator
          style={styles.container}
          initialRoute={initialRoute}
          configureScene={() => Navigator.SceneConfigs.FadeAndroid}
          renderScene={this.routeMapper}/>
        );
      } else {
        return (
          <SplashScreen/> /*飛屏*/
        );
      }
    }
  });

  module.exports = LearningRN;

后退按鈕優先退出棧的頁面, 最后作為結束.
閃屏顯示兩秒鐘, 使用TimerMixin計時器, 再更新狀態跳轉主頁.
routeMapper中, 目前主頁, 以后再添加其他頁面.

樣式

/*@flow*/
/**
 * Created by wangchenlong on 15/11/29.
 */
'use strict';

var React = require('react-native');

var {
  StyleSheet
  } = React;

var styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column'
  },
});

module.exports = styles;

3. 閃屏

首頁圖片, 有個放大效果, 至1.2倍, 持續兩秒(2000ms).
資源文件(uri)使用項目資源, 放在Android項目的?drawable文件夾.

/* @flow*/
/**
 * 啟動閃屏
 * @author C.L.Wang
 */
'use strict';

var React = require('react-native');

var {
  View,
  Text,
  Image,
  Dimensions, // 尺寸
  Animated,   // 動畫
  } = React;

var styles = require('./styles.js');

var WIDTH = Dimensions.get('window').width;

var SplashScreen = React.createClass({

  // 初始化狀態
  getInitialState: function () {
    return {
      cover: {image: {uri: 'splash'}, text: 'Girl\'s Generation'}, // 封面
      bounceValue: new Animated.Value(1) // 彈力值
    };
  },

  // 組件初始化
  componentDidMount: function () {
    Animated.timing(
      this.state.bounceValue, {toValue: 1.2, duration: 2000}
    ).start();
  },

  render: function () {
    return (
      <View style={styles.container}>
        <Animated.Image
          source={{uri: 'splash'}} // 混合資源
          style={{
            flex: 1,
            width: WIDTH,
            height: 1,
            transform: [{scale: this.state.bounceValue}]
          }}/>
        <Text style={styles.text}>
          {this.state.cover.text}
        </Text>
      </View>
    );
  }
});

module.exports = SplashScreen;

在Android工程中放置圖片資源, 修改時重新編譯打包, 適配屏幕尺寸.
在RN工程中放置, 修改時刷新即可, 但無法適配. 使用時, 根據圖片特點, 選擇位置.

樣式

/*@flow*/
/**
 * Created by C.L.Wang on 15/11/29.
 */
'use strict';

var React = require('react-native');

var {
  StyleSheet
  } = React;

var styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column'
  },
  text: {
    flex: 1,
    fontSize: 16,
    textAlign: 'center',
    color: '#FF1493',
    position: 'absolute',
    left: 0,
    right: 0,
    bottom: 10,
    backgroundColor: 'transparent'
  }
});

module.exports = styles;
動畫

Github下載地址

這次的比較簡單. 我會再寫一些復雜的頁面.
OK, Enjoy it.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,334評論 25 708
  • 夜雨林風驚飛鳥, 心事誰知曉。 窗外花落綠搖紅, 孤枕難眠憑欄自惆悵。 去歲今日影猶在, 兩鬢隱霜白, 但得前路多...
    琴癡1102閱讀 246評論 0 0