图像拉伸到父容器

huangapple go评论134阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月15日 02:36:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定