首页 > 解决方案 > 如何在 react-native 中实现三角形的内角半径

问题描述

我有这样的设计

我已经尝试了所有可能的方法来实现内边界半径,但我无法实现。你有没有人知道这件事。

我已经提到了这个,但我无法绘制这个布局。

标签: react-native

解决方案


嘿,您可以使用 2 种方法来实现

  1. 堆叠视图
  2. 使用 svg

工作示例:https ://snack.expo.io/@ashwith00/honest-peach

代码:

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Svg, Path } from 'react-native-svg';

const RADVALUE = 40;
const SIZE = 200;
export default function App() {
  return (
    <View style={styles.container}>
      <View style={styles.traingleCorner}>
        <View style={styles.coloredPart} />
        <View style={styles.uncoloredPart} />
      </View>
      <View style={styles.traingleCorner}>
        <Svg
          width={`${SIZE}`}
          height={`${SIZE}`}
          fill="red"
          strokeWidth={1}
          viewBox={`0 0 ${SIZE} ${SIZE}`}>
          <Path
            d={`M0 0L0 ${SIZE}L${SIZE} ${SIZE}Q${RADVALUE} ${
              SIZE - RADVALUE
            } 0 0 `}
          />
        </Svg>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  traingleCorner: {
    width: SIZE,
    height: SIZE,
  },
  coloredPart: {
    flex: 1,
    backgroundColor: 'red',
  },
  uncoloredPart: {
    ...StyleSheet.absoluteFillObject,
    borderBottomLeftRadius: SIZE,
    backgroundColor: '#ecf0f1',
  },
});

在 svg 中,只需根据您的要求更改 SIZE、RADVALUE


推荐阅读