React-Native 图像高度(全宽)
React-Native Image Height (Full Width)
我有一张从 url 上拉下来的图片。我不知道这张图片的尺寸。
如何设计/布局 <Image/>
使其成为父视图的全宽并计算高度以保持纵横比?
我试过在 0.34.0-rc.0 中使用 onLoad
并且 event.nativeEvent.source
中的高度/宽度均为 0。
图像在 <ScrollView/>
中。我不喜欢全屏图像。
React Native 内置了一个函数,可以 return 图像的宽度和高度:Image.getSize()
。 Check out the documentation here
我的使用非常相似,因为我需要以 全屏宽度 显示图像,但 保持其纵横比 。
根据@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>
)
}
}
如果你还不能解决它,React Native v.0.42.0 有 resizeMode
<Image style={styles.intro_img} source={require('img.png')}
它对我有用。
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;
我是 react-native 的新手,但我遇到了这个使用简单样式的解决方案:
imageStyle: {
height: 300,
flex: 1,
width: null}
来自我的 1º 应用程序的全宽图像:
它对我有用。
如果你有静态图片,你可以像这样使用
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>
);
}
}
试试这个:
传递图像 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),
})}
}
/>
);
}
}
我有一张从 url 上拉下来的图片。我不知道这张图片的尺寸。
如何设计/布局 <Image/>
使其成为父视图的全宽并计算高度以保持纵横比?
我试过在 0.34.0-rc.0 中使用 onLoad
并且 event.nativeEvent.source
中的高度/宽度均为 0。
图像在 <ScrollView/>
中。我不喜欢全屏图像。
React Native 内置了一个函数,可以 return 图像的宽度和高度:Image.getSize()
。 Check out the documentation here
我的使用非常相似,因为我需要以 全屏宽度 显示图像,但 保持其纵横比 。
根据@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>
)
}
}
如果你还不能解决它,React Native v.0.42.0 有 resizeMode
<Image style={styles.intro_img} source={require('img.png')}
它对我有用。
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;
我是 react-native 的新手,但我遇到了这个使用简单样式的解决方案:
imageStyle: {
height: 300,
flex: 1,
width: null}
来自我的 1º 应用程序的全宽图像:
它对我有用。
如果你有静态图片,你可以像这样使用
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>
);
}
}
试试这个:
传递图像 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),
})}
}
/>
);
}
}