Image样式属性
本文档贡献者:sunnylqm(100.00%)
示例
- 函数组件示例
- Class组件示例
import React from "react";
import { View, Image, Text, StyleSheet } from "react-native";
const DisplayAnImageWithStyle = () => {
return (
<View style={styles.container}>
<View>
<Image
style={{
resizeMode: "cover",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : cover</Text>
</View>
<View>
<Image
style={{
resizeMode: "contain",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : contain</Text>
</View>
<View>
<Image
style={{
resizeMode: "stretch",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : stretch</Text>
</View>
<View>
<Image
style={{
resizeMode: "repeat",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : repeat</Text>
</View>
<View>
<Image
style={{
resizeMode: "center",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : center</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "space-around",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
import React, { Component } from "react";
import { View, Image, StyleSheet, Text } from "react-native";
class DisplayAnImageWithStyle extends Component {
render() {
return (
<View style={styles.container}>
<View>
<Image
style={{
resizeMode: "cover",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : cover</Text>
</View>
<View>
<Image
style={{
resizeMode: "contain",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : contain</Text>
</View>
<View>
<Image
style={{
resizeMode: "stretch",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : stretch</Text>
</View>
<View>
<Image
style={{
resizeMode: "repeat",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : repeat</Text>
</View>
<View>
<Image
style={{
resizeMode: "center",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>resizeMode : center</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "space-around",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
- 函数组件示例
- Class组件示例
import React from "react";
import { View, Image, StyleSheet, Text } from "react-native";
const DisplayAnImageWithStyle = () => {
return (
<View style={styles.container}>
<View>
<Image
style={{
borderTopRightRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderTopRightRadius</Text>
</View>
<View>
<Image
style={{
borderBottomRightRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderBottomRightRadius</Text>
</View>
<View>
<Image
style={{
borderBottomLeftRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderBottomLeftRadius</Text>
</View>
<View>
<Image
style={{
borderTopLeftRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderTopLeftRadius</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "space-around",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
import React, { Component } from "react";
import { View, Image, StyleSheet, Text } from "react-native";
class DisplayAnImageWithStyle extends Component {
render() {
return (
<View style={styles.container}>
<View>
<Image
style={{
borderTopRightRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderTopRightRadius</Text>
</View>
<View>
<Image
style={{
borderBottomRightRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderBottomRightRadius</Text>
</View>
<View>
<Image
style={{
borderBottomLeftRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderBottomLeftRadius</Text>
</View>
<View>
<Image
style={{
borderTopLeftRadius: 20,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderTopLeftRadius</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "space-around",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
- 函数组件示例
- Class组件示例
import React from "react";
import { View, Image, StyleSheet, Text } from "react-native";
const DisplayAnImageWithStyle = () => {
return (
<View style={styles.container}>
<Image
style={{
borderColor: "red",
borderWidth: 5,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>
<Text>borderColor & borderWidth</Text>
</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "center",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
import React, { Component } from "react";
import { View, Image, StyleSheet, Text } from "react-native";
class DisplayAnImageWithStyle extends Component {
render() {
return (
<View style={styles.container}>
<Image
style={{
borderColor: "red",
borderWidth: 5,
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>borderColor & borderWidth</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "center",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
- 函数组件示例
- Class组件示例
import React from "react";
import { View, Image, StyleSheet, Text } from "react-native";
const DisplayAnImageWithStyle = () => {
return (
<View style={styles.container}>
<Image
style={{
tintColor: "#000000",
resizeMode: "contain",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>tintColor</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "space-around",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
import React, { Component } from "react";
import { View, Image, StyleSheet, Text } from "react-native";
class DisplayAnImageWithStyle extends Component {
render() {
return (
<View style={styles.container}>
<Image
style={{
tintColor: "#000000",
resizeMode: "contain",
height: 100,
width: 200
}}
source={require("@expo/snack-static/react-native-logo.png")}
/>
<Text>tintColor</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "vertical",
justifyContent: "space-around",
alignItems: "center",
height: "100%",
textAlign: "center"
}
});
export default DisplayAnImageWithStyle;
文档
Props
borderTopRightRadius
类型 | 必填 |
---|---|
number | 否 |
backfaceVisibility
类型 | 必填 |
---|---|
enum('visible', 'hidden') | 否 |
borderBottomLeftRadius
类型 | 必填 |
---|---|
number | 否 |
borderBottomRightRadius
类型 | 必填 |
---|---|
number | 否 |
borderColor
类型 | 必填 |
---|---|
color | 否 |
borderRadius
类型 | 必填 |
---|---|
number | 否 |
borderTopLeftRadius
类型 | 必填 |
---|---|
number | 否 |
backgroundColor
类型 | 必填 |
---|---|
color | 否 |
borderWidth
类型 | 必填 |
---|---|
number | 否 |
opacity
类型 | 必填 |
---|---|
number | 否 |
overflow
类型 | 必填 |
---|---|
enum('visible', 'hidden') | 否 |
resizeMode
类型 | 必填 |
---|---|
enum('cover', 'contain', 'stretch', 'repeat', 'center') | 否 |
tintColor
为所有非透明的像素指定一个颜色。
类型 | 必填 |
---|---|
color | 否 |
overlayColor
当图片有圆角的时候,指定一个颜色用于填充圆角处的空白。虽然一般情况下圆角处是透明的,但在某些情况下,Android 并不支持圆角透明,比如:
- 某些 resize 模式比如'contain'
- GIF 动画
常见的用法就是在不能圆角透明时,设置overlayColor
和背景色一致。
详细说明可参考https://frescolib.org/docs/rounded-corners-and-circles.html。
类型 | 必填 | 平台 |
---|---|---|
string | 否 | Android |