React Native - 图像半圆形(使用CSS)

4

我有一个圆形图片,需要使用 React Native 展示成半圆形,就像附图一样。请帮忙处理 CSS。

enter image description here

2个回答

3

这是我找到的既适用于图像左侧又适用于右侧位置的解决方案:

renderLeftRightImage (item, index) {
    var imagePath = item.image?(Strings.BASE_IMAGE_URL+item.image[0].image):'https://placeimg.com/640/480/people';        

    if(item.position == 'left'){
        return(
            <View style={{overflow: 'hidden', width : 105, height:210, position : 'absolute', left:80,  bottom:62, borderTopLeftRadius:150, borderBottomLeftRadius:150, backgroundColor:'transparent'}}>
                <Image style={{width : 210, height:210}} source={{uri:imagePath}}/>
            </View>
        )
    }
    else if(item.position == 'right'){
        return(
            <View style={{overflow: 'hidden', width : 105, height:210, position : 'absolute', right:80,  bottom:62, borderTopRightRadius:150, borderBottomRightRadius:150, backgroundColor:'transparent'}}>
                <Image style={{width : 210, height:210, position:'absolute', right:0}} source={{uri:imagePath}}/>
            </View>
        )
    }
    else{
        return null;
    }
}

0

你可以使用overflow CSS 属性与border-radius结合使用

示例

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.imageContainer}>
          <View style={styles.imageWrapper}>
            <Image source={{uri: 'https://dummyimage.com/500x500/000/fff.png'}} style={styles.image} />
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  imageContainer: {
    alignItems: 'center',
    backgroundColor: 'yellow'
  },
  imageWrapper: {
    width: 125, // half of the image width
    height: 250,
    backgroundColor: 'transparent',
    overflow: 'hidden'
  },
  image: {
    width: 250,
    height: 250,
    borderRadius: 125, // half of the image width
    backgroundColor: 'transparent'
  }
});

1
谢谢@bennygenel,但我已经进行了几次尝试和错误,并得到了这个: - Amit Malakar
@Amit 的 "overflow" 属性很有帮助。谢谢。 - Zeerak Hameem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接