javascript - 无法在 React Native 应用程序中调整 SVG 图像的大小

标签 javascript reactjs react-native svg expo

我有以下 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/

相关文章:

javascript - Asset.fromModule 不是函数。 (在 'Asset.fromModule(image)' 中, 'Asset.fromModule' 未定义)

java - 为什么 SuperDevMode 只提供 LogLevel SEVERE?

javascript - vue.js 中的动态表 rowspan

reactjs - 我可以在react组件的render()方法中调用redux的store.subscribe方法吗?

javascript - 升级到 React Native 0.55.4 后,isMounted() 警告不断出现

javascript - React native 应用卡在白屏上,没有任何错误

javascript - Facebook 点赞按钮未显示

javascript - 需要在 CoffeeScript 中配置的库?

javascript - 在不安全的前端计算价格?

Javascript Math.random() 返回非常相似的值