0

我有这样的图像:

在此处输入图像描述

我正在尝试制作一个 100x100 的正方形,然后将该图像居中。我可以使用以下代码获得 100x100 的正方形:

import * as React from 'react';
import { View, Image, StyleSheet } from 'react-native';

class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.imageContainer}>
          <Image
            source={{ uri: 'https://storage.googleapis.com/iex/api/logos/GOOGL.png', }}
            style={styles.image}
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  imageContainer: {
    borderWidth: 1
  },
  image: {
    width: 100,
    height: 100
  }
});

export default App;

但是,这会切断图像:

在此处输入图像描述

有没有办法可以设置 100x100 的宽度/高度,但允许图像根据需要调整大小以适合并在正方形内居中?谢谢!

4

3 回答 3

1

你有没有尝试过这样的事情?它从 React Native 中调用 resizeMode。

在此处输入图像描述

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  imageContainer: {
    borderWidth: 1
  },
  image: {
    width: 100,
    height: 100,
    resizeMode: 'contain'
  }
});
于 2018-12-21T01:26:10.207 回答
0

这是一个链接,对我来说非常有用!是jQuery 图像中心通过在其父容器内移动、裁剪和填充空间来居中图像。保持纵横比

http://boxlight.github.com/bl-jquery-image-center

于 2018-12-21T01:25:10.043 回答
0

有关调整大小,请参阅文档中的 resizeMode 和 resizeMethod 文档。

于 2018-12-21T01:56:03.830 回答