0

I have an image like so:

enter image description here

I am trying to make a 100x100 square and then centering that image inside it. I am able to get the 100x100 square with this code:

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;

However that cuts off the image:

enter image description here

Is there a way I can set a 100x100 width/height, but allow the image to resize as necessary to fit and be centered inside the square? Thanks!

inhwrbp
  • 569
  • 2
  • 4
  • 17

3 Answers3

1

Have you tried something like this? Its called resizeMode from React Native.

enter image description here

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

Here is a link, for my has been very useful! is the jQuery Image Center. Centers an image by moving, cropping and filling spaces inside its parent container. Maintains aspect ratio.

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

0

For resize see the doc of resizeMode and resizeMethod in doc.

Suresh Tamang
  • 42
  • 1
  • 9