react-native实现假象的水印

react-native实现假象的水印

主要代码块

一:

import React, { Component } from "react";
import {
  View,
  Text,
  Dimensions,
  StyleSheet,
  ViewPropTypes
} from 'react-native';
import PropTypes from 'prop-types';

const WIDTH = Dimensions.get('window').width
const HEIGHT = Dimensions.get('window').height

class WatermarkView extends Component {
  constructor(props) {
    super(props);
    this.renderWatermark = this.renderWatermark.bind(this);
  }

  static propTypes = {
    foreground: PropTypes.bool,
    style: ViewPropTypes.style,
    watermark: PropTypes.string,
    itemWidth: PropTypes.number,
    itemHeight: PropTypes.number,
    watermarkTextStyle: Text.propTypes.style,
    rotateZ: PropTypes.number
  }

  renderWatermark() {
    const { watermark='', itemWidth=160, itemHeight=100, watermarkTextStyle, rotateZ = -45 } = this.props;
    if (typeof watermark != 'string' || (typeof watermark == 'string' && watermark.length < 1)) {
      return null;
    }
    const items = new Array()
    for (let index = 0; index < 50; index++) {
      const item = (
        <View
          key={'watermark_item' + index}
          style={[styles.watermarkItem, { width: itemWidth, height: itemHeight, transform: [{ rotateZ: rotateZ+'deg' }] }]}
        >
          <Text style={[{ color: 'rgba(0,0,0,0.2)', fontSize: 20 }, watermarkTextStyle]}>{watermark}</Text>
        </View>
      )
      items.push(item);
    }
    return (
      <View style={styles.watermarkContainer} pointerEvents={'none'}>
        {items}
      </View>
    )
  }

  render() {
    const { foreground, style } = this.props;
    return (
      <View style={[styles.container, style, { overflow: 'hidden' }]}>
        {!foreground ? this.renderWatermark() : null}
        {this.props.children}
        {foreground ? this.renderWatermark() : null}
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  watermarkContainer: {
    flex:1,
    backgroundColor: 'rgba(0,0,0,0)',
    position: 'absolute',
    // left: 0,
    // right: 0,
    // top: 0,
    // bottom: 0,
    flexDirection: 'row',
    justifyContent: 'center',
    // alignItems: 'center',
    flexWrap: 'wrap',
    alignContent: 'flex-start'
  },
  watermarkItem: {
    backgroundColor: 'rgba(0,0,0,0)',
    justifyContent: 'center',
    alignItems: 'center',
  }
})

export default WatermarkView;

二:引用。不知道的属性可以查看源码

<WatermarkView
    foreground={visible?true:false}
     watermark={this.state.watermark}
     style={{justifyContent:'flex-start'}}
     itemWidth={this.state.itemWidth}
     itemHeight={this.state.itemHeight}
     rotateZ={this.state.rotateZ}
     watermarkTextStyle={ styles.watermarkTextStyle }
   >
</WatermarkView>

引用地址:https://github.com/xiaochenjie/react-native-image-marker

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值