React Native之Modal组件实现遮罩层效果

React-Native中Modal的使用
/**
 * React Native App
 * dongtao 2017/04/22
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  Modal,
  PixelRatio,
  View
} from 'react-native';


class ModalDemo extends Component {

  constructor(props) {
    super(props);//这一句不能省略,照抄即可
    this.state = {
      animationType: 'none',//none slide fade
      modalVisible: false,//模态场景是否可见
      transparent: true,//是否透明显示
    };
  }

  render() {
    let modalBackgroundStyle = {
      backgroundColor: this.state.transparent ? 'rgba(0, 0, 0, 0.5)' : 'red',
    };
    let innerContainerTransparentStyle = this.state.transparent
      ? { backgroundColor: '#fff', padding: 20 }
      : null;

    return (

      <View style={{ alignItems: 'center', flex: 1 }}>
        <Modal
          animationType={this.state.animationType}
          transparent={this.state.transparent}
          visible={this.state.modalVisible}
          onRequestClose={() => { this._setModalVisible(false) } }
          onShow={this.startShow}
          >
          <View style={[styles.container, modalBackgroundStyle]}>
            <View style={[styles.innerContainer, innerContainerTransparentStyle]}>
              <Text style={styles.date}>2016-08-11</Text>
              <View style={styles.row}>
                <View >
                  <Text style={styles.station}>长沙站</Text>
                  <Text style={styles.mp10}>8: 00出发</Text>
                </View>
                <View>
                  <View style={styles.at}></View>
                  <Text style={[styles.mp10, { textAlign: 'center' }]}>G888</Text>
                </View>
                <View >
                  <Text style={[styles.station, { textAlign: 'right' }]}>北京站</Text>
                  <Text style={[styles.mp10, { textAlign: 'right' }]}>18: 00抵达</Text>
                </View>
              </View>
              <View style={styles.mp10}>
                <Text>票价:¥600.00元</Text>
                <Text>乘车人:东方耀</Text>
                <Text>长沙站 火车南站 网售</Text>
              </View>
              <View style={[styles.mp10, styles.btn, { alignItems: 'center' }]}>
                <Text style={styles.btn_text}>去支付</Text>
              </View>
              <Text
                onPress={this._setModalVisible.bind(this,false) }
                style={{fontSize:20,marginTop:10}}>
                关闭
              </Text>
            </View>
          </View>
        </Modal>

          <Text style={{ fontSize: 30,color:'red' }}  onPress={this._setModalVisible.bind(this, true) }>预定火车票</Text>


      </View>
    );
  }


  _setModalVisible = (visible) => {
    this.setState({ modalVisible: visible });
  }

  startShow=()=>{
    alert('开始显示了');
  }




}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    padding: 40,
  },
  innerContainer: {
    borderRadius: 10,
    alignItems: 'center',
  },
  row: {
    alignItems: 'center',

    flex: 1,
    flexDirection: 'row',
    marginBottom: 20,
  },
  rowTitle: {
    flex: 1,
    fontWeight: 'bold',
  },
  button: {
    borderRadius: 5,
    flex: 1,
    height: 44,
    alignSelf: 'stretch',
    justifyContent: 'center',
    overflow: 'hidden',
  },
  buttonText: {
    fontSize: 18,
    margin: 5,
    textAlign: 'center',
  },

  page: {
    flex: 1,
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
    top: 0,
  },
  zhifu: {
    height: 150,
  },

  flex: {
    flex: 1,
  },
  at: {
    borderWidth: 1 / PixelRatio.get(),
    width: 80,
    marginLeft:10,
    marginRight:10,
    borderColor: '#18B7FF',
    height: 1,
    marginTop: 10

  },
  date: {
    textAlign: 'center',
    marginBottom: 5
  },
  station: {
    fontSize: 20
  },
  mp10: {
    marginTop: 5,
  },
  btn: {
    width: 60,
    height: 30,
    borderRadius: 3,
    backgroundColor: '#FFBA27',
    padding: 5,
  },
  btn_text: {
    lineHeight: 18,
    textAlign: 'center',
    color: '#fff',
  },
});

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

1.png
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native中可以使用Image组件和ImageEditor组件实现页面组件的黑白滤镜效果。以下是实现步骤: 1. 导入Image和ImageEditor组件: ```javascript import { Image, ImageEditor } from 'react-native'; ``` 2. 在组件中使用Image组件来渲染需要添加黑白滤镜的组件,并添加样式: ```javascript <Image source={require('./assets/image.jpg')} style={styles.image} /> ``` 其中,require('./assets/image.jpg')指定了需要添加黑白滤镜的组件的路径,styles.image定义了Image组件的样式: ```javascript const styles = StyleSheet.create({ image: { width: 200, height: 200, }, }); ``` 3. 使用ImageEditor组件组件进行黑白滤镜处理: ```javascript ImageEditor.cropImage(uri, { offset: { x: 0, y: 0 }, size: { width: screenWidth, height: screenHeight }, displaySize: { width: screenWidth, height: screenHeight }, resizeMode: 'contain', }, (croppedImageURI) => { ImageEditor.processImage(croppedImageURI, { width: screenWidth, height: screenHeight, resizeMode: 'contain', colorMatrix: [ 0.33, 0.33, 0.33, 0, 0, 0.33, 0.33, 0.33, 0, 0, 0.33, 0.33, 0.33, 0, 0, 0, 0, 0, 1, 0 ], }, (processedImageURI) => { this.setState({ processedImageURI }); }, (error) => console.error(error)); }, (error) => console.error(error)); ``` 其中,uri参数指定了需要添加黑白滤镜的组件的路径,colorMatrix参数指定了黑白滤镜的矩阵。处理完成后,将处理后的图片设置为组件的state,并在render方法中使用Image组件来渲染处理后的图片: ```javascript render() { return ( <View style={styles.container}> <Image source={{ uri: this.state.processedImageURI }} style={styles.image} /> // 其他组件 </View> ); } ``` 注意,ImageEditor组件是异步执行的,因此需要使用回调函数来处理处理后的图片。详细使用方法可以查看React Native官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值