【问题标题】:React-Native: Mask image with a bottom radiusReact-Native:具有底部半径的蒙版图像
【发布时间】:2018-08-08 15:53:55
【问题描述】:

如何在图像中设置边框底部半径?

我怎样才能将图像屏蔽到绿色区域?

我尝试了以下代码,但我无法在上面分享的图像中获得半径比

查看代码:

<View style={styles.wrapper}>
    <View style={styles.welcomeWrapper}>
        <View style={styles.welcomeImageWrapper}>
            <Image style={{width:'100%'}} source={require('../assets/images/test-slide.jpg')}/>
        </View>
    </View>
    <View style={{
        height: 100,
        backgroundColor: colors.white,
        justifyContent: 'flex-end',
        alignItems: 'center'
    }}>
       <Text style={{marginBottom:50,fontSize:18,fontFamily:'Montserrat-Regular'}}>Deneme Text </Text>
    </View>
</View>

样式代码:

wrapper:{
    flex:1,
    display: 'flex',
    backgroundColor:colors.white,
},
welcomeWrapper:{
    flex:1,
    justifyContent:'center',   
    backgroundColor:colors.green01,
    overflow: 'hidden',
    position:'relative',
    width:'100%',
    borderBottomRightRadius:Dimensions.get('window').width/100*50,
    borderBottomLeftRadius:Dimensions.get('window').width/100*50,
},

【问题讨论】:

  • 请发布您现有的代码并列出您已经尝试过的内容
  • @JeremyLee 我分享了现有的代码..谢谢..

标签: react-native border react-native-stylesheet


【解决方案1】:

看到你的图像蒙版的形状,我认为你应该使用react-native-svg 之类的东西来创建一个真正的图像蒙版。

步骤

  1. 将背景图片设置在Viewposition: absolute中,这样你的图片就一直在背景中并且可以被屏蔽

  2. react-native-svg 添加到您的项目中,例如使用yarn add react-native-svg,并使用react-native link 链接库。最后,重新启动 Metro 捆绑器并编译您的应用(run-androidrun-ios)。

  3. 设计 svg 掩码(我使用)并将其添加到容器的View,掩码应与您的文本容器具有相同的backgroundColor

  4. 使用 react 的 flexbox 布局进行一些样式设置,以便能够在每个设备上具有几乎相同的外观。在这个例子中,掩码取屏幕高度的5/6 作为我的掩码flex 数字是5,我的文本flex 是1

所以,这就是我最终的结果:

import * as React from 'react';
import { Dimensions, Image, StyleSheet, Text, View } from 'react-native';
import { Path, Svg } from 'react-native-svg';

const mask = {
  width: Dimensions.get('window').width,
  height: 50,
  bgColor: '#ecf0f1',
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'flex-end',
  },
  image: {
    position: 'absolute',
  },
  mask: {
    flex: 5,
    justifyContent: 'flex-end',
  },
  textContainer: {
    flex: 1,
    width: '100%',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: mask.bgColor,
  },
  text: {
    fontSize: 50,
    textAlign: 'center',
  }
});

const App = () => (
  <View style={styles.container}>
    <Image style={styles.image} source={require('./assets/image.jpg')} />
    <View style={styles.mask}>
      <Svg width={mask.width} height={mask.height}>
        <Path
          fill={mask.bgColor}
          d={`M 0 0 L 0 ${mask.height} L ${mask.width} ${mask.height} L ${mask.width} 0 A ${mask.width / 2} ${mask.height / 2} 0 0 1 ${mask.width / 2} ${mask.height / 2} A ${mask.width / 2} ${mask.height / 2} 0 0 1 0 0 z `} />
      </Svg>
    </View>
    <View style={styles.textContainer}>
      <Text style={styles.text}>Text</Text>
    </View>
  </View>
);

export default App;

这是Android 模拟器中的结果

希望这会有所帮助!

零食链接:https://snack.expo.io/BJlZgpuB7(但我的图片无法加载到零食上:()

【讨论】:

  • 非常感谢您的回复,描述性强、详细且非常好。
猜你喜欢
  • 2019-03-07
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2021-07-30
  • 2021-07-22
  • 1970-01-01
  • 2022-08-18
相关资源
最近更新 更多