【问题标题】:How to add opacity to touchable opacity with image background in a flatlist?如何在平面列表中将不透明度添加到具有图像背景的可触摸不透明度?
【发布时间】:2019-12-01 10:14:39
【问题描述】:

我有一个平面列表,它呈现一个可点击的项目(具有可触摸的不透明度)和一个中间有一些文本的图像背景。

目标是呈现如下所示的平面列表:

我正在尝试通过添加带有叠加层的视图来为图像添加光不透明度,我尝试了多种解决方案,但似乎没有任何效果。

这是我迄今为止所取得的成就:

这是平面列表中的项目:

    // console.log("item", item.latest_image.media[0].url);
    if (item.empty === true) {
      return <View style={[styles.item, styles.itemInvisible]} />;
    }
    return (
      <TouchableOpacity
        onPress={() =>
          this.props.navigation.push(Screens.Photos, {
            team: item,
            id: this.props.navigation.getParam("team").id
          })
        }
      >
        <ImageBackground
          style={styles.backgroundImage}
          imageStyle={{ borderRadius: theme.borders_MediumRadius.borderRadius, backgroundColor: 'rgba(255,0,0,.6)' }}
          source={{ uri: item.latest_image.media[0].url }}
        >
            <Text style={styles.itemText}>{item.title.toUpperCase()}</Text>
        </ImageBackground>
      </TouchableOpacity>
    );
  };

这是平面列表:

      <SafeAreaView style={{ flex: 1 }}>
        <ScrollView
          style={{ flex: 1, backgroundColor: "black" }}
          scrollEnabled={scrollEnabled}
          onContentSizeChange={this.onContentSizeChange}
        >
          <FlatList
            data={formatData(this.state.data, numColumns)}
            style={styles.container}
            renderItem={this.renderItem}
            numColumns={numColumns}
            scrollEnabled={scrollEnabled}
          />
          <FollowUs />
        </ScrollView>
      </SafeAreaView>
    );

这些是样式道具:

  container: {
    margin: 7.5,
    backgroundColor: "black"
  },
  backgroundImage: {
    flex: 1,
    height: hp("25"),
    backgroundColor: "black",
    margin: 7.5,
    justifyContent: "center",
    alignItems: "center"
  },
  itemInvisible: {
    backgroundColor: "transparent"
  },
  itemText: {
    fontFamily: "RobotoCondensed-Bold",
    fontSize: RF(4),
    color: "black"
  },
  overlay: {
    backgroundColor: "rgba(255,0,0,0.5)",
    alignItems: "center",
    justifyContent: "center",
    flex: 1,
    margin: 7.5,
    height: hp("25"), // approximate a square
    borderRadius: theme.borders_MediumRadius.borderRadius
  }
});

如果有人可以帮助我,将不胜感激!

谢谢

【问题讨论】:

  • 你的图片好像坏了。
  • @Maurice 我已将图片的链接附在它们旁边,我不确定它们为什么没有显示。
  • 您可以在TouchableOpacity 上设置白色的背景颜色,然后将一些opacity 添加到ImageBackground

标签: react-native


【解决方案1】:

通过在文本周围添加一个视图并给它这些道具来解决它:

          <View
            style={{
              flex: 1,
              alignItems: "center",
              justifyContent: "center",
              backgroundColor: "rgba(255,255,255,0.75)",
              borderRadius: theme.borders_MediumRadius.borderRadius
            }}
          >
            <Text style={styles.itemText}>{item.title.toUpperCase()}</Text>
          </View>

【讨论】:

    【解决方案2】:

    这是一个如何实现此目标的示例

    export default class App extends React.Component {
      render() {
        return (
          <View style={styles.container}>
            <TouchableOpacity
              style={{
                backgroundColor: '#FFF',
                height: 200,
                width: 200,
                position: 'relative'
              }}>
              <ImageBackground
                source={{ uri: 'https://placehold.it/200x200' }}
                style={{
                  height: 200,
                  width: 200,
                  opacity: 0.6,
                  position: 'absolute',
                }}
              />
              <View
                style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Hello World!</Text>
              </View>
            </TouchableOpacity>
          </View>
        );
      }
    }
    

    这是一个小吃示例 - https://snack.expo.io/@hannigan/lonely-bubblegum

    您还应该将TouchableOpacity 上的activeOpacity 设置为与您将在ImageBackground 上设置的值相同(0.6)

    【讨论】:

    • 谢谢,但我似乎无法获得白色不透明度的效果(这是我之前尝试过的方法之一),但每当我添加不透明度和主动不透明度时,它都会给我一个暗淡的颜色。我希望它是白色的,而不是像帖子中的第一张图片那样暗。
    猜你喜欢
    • 2015-05-15
    • 2015-01-20
    • 2016-09-06
    • 2021-02-16
    • 2013-12-08
    • 1970-01-01
    • 2012-05-26
    • 2011-10-16
    • 2012-07-11
    相关资源
    最近更新 更多