我有以下 React Native
项目:
https://snack.expo.io/BkBU8fAlV
我有以下代码:
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
import HomerSvg from './assets/HomerSvg';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Change code in the editor and watch it change on your phone! Save to get a shareable url.
</Text>
<View style={{ width: 80, height: 80 }}>
<HomerSvg />
</View>
<Card>
<AssetExample />
</Card>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
我通过使用 react-native-svg
包的 HomerSvg
组件显示 SVG
图像。
我需要的是以某种方式调整 SVG
图像的大小。在上面的代码中我进行了尝试,但没有成功。
我尝试给容器 View 一些宽度和高度,但没有成功。
您知道如何实现这一目标吗?
谢谢!
最佳答案
这实际上很容易解决,无需 React Native,只需使用 SVG 本身即可。
只需将 preserveAspectRatio
标记设置为 none
。
示例:
<Svg
width={this.props.width}
height={this.heights.h1}
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 750 352"
preserveAspectRatio="none">
...
</Svg>
关于javascript - 无法在 React Native 应用程序中调整 SVG 图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53910937/