英文:
Image stretching the parent container
问题
我有一张想要显示的图片,但不希望它拉伸或超出屏幕。
我设置了 maxWidth: "90%" 以及 resizeMode: "contain"。 图像相应地缩小了,但其父容器保持了原始高度。 除了明确设置 height 属性之外,我是否可以做些什么,以使父容器也缩小?
我已经准备了一个小演示,在这里查看。
英文:
I have an image that I want to display, but don't want it to stretch or go off the screen.
I set maxWidth: "90%" along with resizeMode: "contain". The image shrunk accordingly, but it's parent container kept the original height. Is there something I can do (except for explicitly setting the height property) so the parent also shrinks down?
I have prepared a small demo here
答案1
得分: 0
以下是您要翻译的内容:
"You can load/import image and scale it to fit in specific area as below:"
import {
  StyleSheet,
  SafeAreaView,
  View,
  Image,
  useWindowDimensions,
} from "react-native";
import { useState } from "react";
/**
 * Conserve aspect ratio of the original image. Useful when shrinking/enlarging
 * images to fit into a certain area.
 *
 * @param {Number} srcWidth width of source image
 * @param {Number} srcHeight height of source image
 * @param {Number} maxWidth maximum available width
 * @param {Number} maxHeight maximum available height
 * @return {Object} { width, height }
 */
export function calculateAspectRatioFit(
  srcWidth: number,
  srcHeight: number,
  maxWidth: number,
  maxHeight: number
) {
  const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
  return { width: Math.fround(srcWidth * ratio), height: srcHeight * ratio };
}
const QuestionImages = {
  bonk: require("./bonk.jpeg"),
};
const App = () => {
  const { width } = useWindowDimensions();
  const [imageSize, setImageRealSize] = useState({ width: 0, height: 0 });
  console.log(imageSize);
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.View}>
        <Image
          onLoad={({ nativeEvent: { source: { width, height } } }) => setImageRealSize({ width, height })}
          source={QuestionImages.bonk}
          style={[
            styles.Image,
            calculateAspectRatioFit(
              imageSize.width,
              imageSize.height,
              width,
              imageSize.height
            ),
          ]}
        />
      </View>
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  Image: {
    resizeMode: "contain",
    alignSelf: "center",
  },
  View: {
    marginTop: 40,
    padding: 3,
    backgroundColor: "#484848",
  },
});
export default App;
Demo - https://snack.expo.dev/@emmbyiringiro/78805a?platform=android
英文:
You can load/import image and scale it to fit in specific area as below:
import {
  StyleSheet,
  SafeAreaView,
  View,
  Image,
  useWindowDimensions,
} from "react-native";
import { useState } from "react";
/**
 * Conserve aspect ratio of the original image. Useful when shrinking/enlarging
 * images to fit into a certain area.
 *
 * @param {Number} srcWidth width of source image
 * @param {Number} srcHeight height of source image
 * @param {Number} maxWidth maximum available width
 * @param {Number} maxHeight maximum available height
 * @return {Object} { width, height }
 */
export function calculateAspectRatioFit(
  srcWidth: number,
  srcHeight: number,
  maxWidth: number,
  maxHeight: number
) {
  const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
  return { width: Math.fround(srcWidth * ratio), height: srcHeight * ratio };
}
const QuestionImages = {
  bonk: require("./bonk.jpeg"),
};
const App = () => {
  const { width } = useWindowDimensions();
  const [imageSize, setImageRealSize] = useState({ width: 0, height: 0 });
  console.log(imageSize);
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.View}>
        <Image
          onLoad={({
            nativeEvent: {
              source: { width, height },
            },
          }) => setImageRealSize({ width, height })}
          source={QuestionImages.bonk}
          style={[
            styles.Image,
            calculateAspectRatioFit(
              imageSize.width,
              imageSize.height,
              width,
              imageSize.height
            ),
          ]}
        />
      </View>
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  Image: {
    resizeMode: "contain",
    alignSelf: "center",
  },
  View: {
    marginTop: 40,
    padding: 3,
    backgroundColor: "#484848",
  },
});
export default App;
Demo - https://snack.expo.dev/@emmbyiringiro/78805a?platform=android
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论