如何在Android上使用react-native-svg在屏幕上绘图?

时间:2017-04-27 10:17:21

标签: react-native react-native-android

我希望圆圈在移动时留下痕迹,但到目前为止它没有。圆圈移动但svg不会渲染到屏幕上。我似乎无法找出我出错的地方

以下是代码:

export default class App extends Component {
  constructor(props){
    super(props);
    this._panResponder={};
    this._previousLeft = 0;
    this._previousTop = 0;
    this._circleStyles = {};
    this.circle = null;
    this.state={
      coordinates: []      
    }
}
  draw(x,y){
     this.state.coordinates.push([x,y]);
     var moves = "V" + this._previousLeft + " " + x + " H" + this._previousTop + " " + y;
    console.log(moves)
    return(
      <Svg height={200} width={200}>
        <Path 
        d = {moves}
        fill = "blue"
        stroke ="red"
        strokeWidth={2}
        />
      </Svg>
    )
  }

  componentWillMount(){
    this._panResponder = PanResponder.create({
      onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
      onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
      onPanResponderGrant: this._handlePanResponderGrant,
      onPanResponderMove: this._handlePanResponderMove,
      onPanResponderRelease: this._handlePanResponderEnd,
      onPanResponderTerminate: this._handlePanResponderEnd,
    });
    this._previousLeft = 20;
    this._previousTop = 84;
    this._circleStyles = {
      style: {
        left: this._previousLeft,
        top: this._previousTop,
        backgroundColor: 'green',
      }
    };
  }
  componentDidMount() {
    this._updateNativeStyles();
  }
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.circle}
          ref={(circle) => {
            this.circle = circle;
          }}
          { ...this._panResponder.panHandlers }
        />
      </View>
    );
  }

  _highlight = () => {
    this._circleStyles.style.backgroundColor = 'blue';
    this._updateNativeStyles();
  }

  _unHighlight = () => {
    this._circleStyles.style.backgroundColor = 'green';
    this._updateNativeStyles();
  }

  _updateNativeStyles() {
    this.circle && this.circle.setNativeProps(this._circleStyles);
  }

  _handleStartShouldSetPanResponder() {
    return true;
  }

  _handleMoveShouldSetPanResponder() {
    return true;
  }

  _handlePanResponderGrant = () => {
    this._highlight();
  }

    _handlePanResponderMove = (e, gestureState) => {
    this._circleStyles.style.left = this._previousLeft + gestureState.dx;
    this._circleStyles.style.top = this._previousTop + gestureState.dy;
    this.draw(this._circleStyles.style.left, this._circleStyles.style.top)     
    this._updateNativeStyles();
   }

  _handlePanResponderEnd = (e, gestureState) => {
    this._unHighlight();       
    this._previousLeft += gestureState.dx;
    this._previousTop += gestureState.dy;
    }
}
const styles = StyleSheet.create({
  circle: {
    width: CIRCLE_SIZE,
    height: CIRCLE_SIZE,
    borderRadius: CIRCLE_SIZE / 2,
    position: 'absolute',
    left: 0,
    top: 0,
  },
  container: {
    flex: 1,
    paddingTop: 64,
  },
});

0 个答案:

没有答案