我有一张从网址上下来的图片。我提前不知道这张图片的尺寸。
如何设置/布局<Image/>
所以它是父视图的整个宽度,并计算高度以保持纵横比?
我尝试在0.34.0-rc.0中使用onLoad
,event.nativeEvent.source
中的高度/宽度均为0。
图片位于<ScrollView/>
。我不是在全屏图像之后。
答案 0 :(得分:14)
我的用法非常相似,因为我需要显示全屏宽度但保持其宽高比的图像。
基于@JasonGaare对使用Image.getSize()
的回答,我想出了以下实现:
class PostItem extends React.Component {
state = {
imgWidth: 0,
imgHeight: 0,
}
componentDidMount() {
Image.getSize(this.props.imageUrl, (width, height) => {
// calculate image width and height
const screenWidth = Dimensions.get('window').width
const scaleFactor = width / screenWidth
const imageHeight = height / scaleFactor
this.setState({imgWidth: screenWidth, imgHeight: imageHeight})
})
}
render() {
const {imgWidth, imgHeight} = this.state
return (
<View>
<Image
style={{width: imgWidth, height: imgHeight}}
source={{uri: this.props.imageUrl}}
/>
<Text style={styles.title}>
{this.props.description}
</Text>
</View>
)
}
}
答案 1 :(得分:10)
我是反应原生的新手但我使用简单的风格遇到了这个解决方案:
imageStyle: {
height: 300,
flex: 1,
width: null}
Image full width from my 1º App
它对我有用 - 你有什么想法?
提前致谢。
答案 2 :(得分:5)
React Native有一个内置函数,它将返回图像的宽度和高度:Image.getSize()
。 Check out the documentation here
答案 3 :(得分:3)
它对我有用。
import React from 'react'
import { View, Text, Image } from 'react-native'
class Test extends React.Component {
render() {
return (
<View>
<Image
source={{ uri: "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQL6goeE1IdiDqIUXUzhzeijVV90TDQpigOkiJGhzaJRbdecSEf" }}
style={{ height: 200, left: 0, right: 0 }}
resizeMode="contain"
/>
<Text style={{ textAlign: "center" }}>Papaya</Text>
</View>
);
}
}
export default Test;
另一种在布局事件后可以获得父视图宽度的方法。
<View
style={{ flex: 1}}
layout={(event) => { this.setState({ width: event.nativeEvent.layout.width }); }}
/>
当您从布局事件获得宽度父视图时,您可以为图像标记分配宽度。
import React from 'react';
import { View, Image } from 'react-native';
class Card extends React.Component {
constructor(props) {
super(props);
this.state = {
height: 300,
width: 0
};
}
render() {
return (
<View style={{
flex: 1,
flexDirection: 'row'
}}>
<View style={{ width: 50, backgroundColor: '#f00' }} />
<View
style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fafafa' }}
onLayout={(event) => { this.setState({ width: event.nativeEvent.layout.width }); }}
>
{
this.state.width === 0 ? null : (
<Image
source={{ uri: "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQL6goeE1IdiDqIUXUzhzeijVV90TDQpigOkiJGhzaJRbdecSEf" }}
style={{ width: this.state.width, height: this.state.height }}
resizeMode="contain"
/>
)
}
</View>
</View>
);
}
}
export default Card;
答案 4 :(得分:0)
如果您有静态图片,可以这样使用
import React, { Component } from "react";
import { View, Animated, Image, Dimensions } from "react-native";
import splashScreen from "../../../assets/imgs/splash-screen.png";
export default class MasterPage extends Component {
constructor(props) {
super(props);
this.state = {
fadeAnim: new Animated.Value(0),
imgWidth: 0,
imgHeight: 0
};
}
checkLogIn = async () => {
const width = 533; // set your local image with
const height = 527; // set your local image height
// calculate image width and height
const screenWidth = Dimensions.get("window").width;
const scaleFactor = width / screenWidth;
const imageHeight = height / scaleFactor;
this.setState({ imgWidth: screenWidth, imgHeight: imageHeight });
};
async componentDidMount() {
Animated.timing(
// Animate over time
this.state.fadeAnim, // The animated value to drive
{
toValue: 1, // Animate to opacity: 1 (opaque)
duration: 800, // Make it take a while
useNativeDriver: true
}
).start(this.checkLogIn);
}
render() {
const { imgWidth, imgHeight, fadeAnim } = this.state;
return (
<Animated.View
style={{
opacity: fadeAnim,
backgroundColor: "#ebebeb",
flex: 1,
justifyContent: "center",
alignItems: "center"
}}
>
<View>
<Image
source={splashScreen}
style={{ width: imgWidth, height: imgHeight }}
/>
</View>
</Animated.View>
);
}
}
答案 5 :(得分:0)
试试这个:
传递图像 uri
和 parentWidth(可以是 const { width } = Dimensions.get("window");
),瞧...您可以根据宽度和纵横比自动计算高度
import React, {Component} from 'react';
import FastImage from 'react-native-fast-image';
interface Props {
uri: string;
parentWidth: number;
}
interface State {
calcImgHeight: number;
width: number
aspectRatio: number
}
export default class CustomFastImage extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
calcImgHeight: 0,
width: props.parentWidth,
aspectRatio: 1
};
}
componentDidUpdate(prevProps: Props){
const { aspectRatio, width }= this.state
const { parentWidth} = this.props
if( prevProps.parentWidth !== this.props.parentWidth){
this.setState({
calcImgHeight: aspectRatio * parentWidth,
width: parentWidth
})
}
}
render() {
const {calcImgHeight, width} = this.state;
const {uri} = this.props;
return (
<FastImage
style={{width: width, height: calcImgHeight}}
source={{
uri,
}}
resizeMode={FastImage.resizeMode.contain}
onLoad={(evt) =>
{
this.setState({
calcImgHeight: (evt.nativeEvent.height / evt.nativeEvent.width) * width, // By this, you keep the image ratio
aspectRatio: (evt.nativeEvent.height / evt.nativeEvent.width),
})}
}
/>
);
}
}
答案 6 :(得分:-3)
如果你还不能解决它,React Native v.0.42.0有resizeMode
<Image style={styles.intro_img} source={require('img.png')}