我有一个高度大于宽度的矩形图像。所以使用了 resizeMode='cover'。它保持纵横比相等,但从图像的中心和顶部扩展看不到。我想要做的是保持纵横比并从顶部显示图像。
请尝试小吃中的代码:https : //snack.expo.io/@codebyte99/new-addition-to-home-page
代码:
<View>
<Image
source={{
uri:
'https://img.kpopmap.com/2017/09/HanYeSul-min.jpg',
}}
resizeMode={'cover'}
style={{ width: 120, height: 120 }}
/>
</View>
Run Code Online (Sandbox Code Playgroud)
现在图片
想象我想要实现的目标
图片原件
我想从顶部对齐图像,以便清楚地看到头部。我们可以通过 object-fit:cover 和 object-position:top 在 css 中做到这一点,但是如何在 react native 中做到这一点?
您可以尝试使用这样的ImageBackground一些:overflow hidden
import * as React from 'react';
import { Text, View, StyleSheet, ImageBackground } from 'react-native';
import Constants from 'expo-constants';
export default function App() {
return (
<View style={styles.container}>
<View styles={styles.imageContainer}>
<ImageBackground
source={{uri: 'https://i.stack.imgur.com/n5xcc.jpg'}}
style={{
height: 200, // <-- you can adjust visible area
width: 200, // <-- same here
overflow: 'hidden'
}}
imageStyle={{
resizeMode: "cover",
height: 260, // <-- you can adjust this height to play with zoom
}}
>
</ImageBackground>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: 'pink',
alignItems: 'center',
padding: 8,
},
});
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的:
| 归档时间: |
|
| 查看次数: |
487 次 |
| 最近记录: |