我画了一个简单的
SVG
文件和只是
已切换
它符合你的代码。。。这个解决方案对你来说合适吗?
import React from 'react';
import { TouchableOpacity, StyleSheet, View } from 'react-native';
import Svg, { Rect, Text as SvgText } from 'react-native-svg';
import moment from 'moment';
const DateComponent = () => {
const date = moment(new Date());
const dayName = date.format('dd').charAt(0);
const dayNumber = date.format('D');
const isFutureDate = date.isAfter(moment(), 'day');
const progress = 0.50;
const radius = 30;
const strokeWidth = 3;
const rectWidth = 60;
const rectHeight = 80;
const perimeter = 1.6 * (rectWidth + rectHeight);
const progressLength = progress * perimeter;
return (
<View style={styles.screen}>
<TouchableOpacity style={styles.container}>
<Svg width="70" height="100" viewBox="0 0 70 100">
<Rect
x="5"
y="10"
width={rectWidth}
height={rectHeight}
rx={radius}
ry={radius}
fill="black"
stroke="gray"
strokeWidth={strokeWidth}
opacity={0.2}
/>
<Rect
x="5"
y="10"
width={rectWidth}
height={rectHeight}
rx={radius}
ry={radius}
fill="none"
stroke="green"
strokeWidth={strokeWidth}
strokeDasharray={`${progressLength} ${perimeter - progressLength}`}
strokeLinecap="round"
/>
<SvgText
x="35"
y="40"
textAnchor="middle"
fill="#858585"
fontSize="20"
fontFamily="Arial"
>
{dayName}
</SvgText>
<SvgText
x="35"
y="75"
textAnchor="middle"
fill={isFutureDate ? '#757575' : 'white'}
fontSize="20"
fontFamily="Arial"
>
{dayNumber}
</SvgText>
</Svg>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
},
container: {
justifyContent: 'center',
alignItems: 'center',
width: 70,
height: 100,
},
});
export default DateComponent;
更新
pathLength版本
import React from 'react';
import { TouchableOpacity, StyleSheet, View } from 'react-native';
import Svg, { Rect, Text as SvgText } from 'react-native-svg';
import moment from 'moment';
const DateComponent = () => {
const date = moment(new Date());
const dayName = date.format('dd').charAt(0);
const dayNumber = date.format('D');
const isFutureDate = date.isAfter(moment(), 'day');
const progress = 0.50;
const radius = 30;
const strokeWidth = 3;
const rectWidth = 60;
const rectHeight = 80;
return (
<View style={styles.screen}>
<TouchableOpacity style={styles.container}>
<Svg width="70" height="100" viewBox="0 0 70 100">
<Rect
x="5"
y="10"
width={rectWidth}
height={rectHeight}
rx={radius}
ry={radius}
fill="black"
stroke="gray"
strokeWidth={strokeWidth}
opacity={0.2}
/>
<Rect
x="5"
y="10"
width={rectWidth}
height={rectHeight}
rx={radius}
ry={radius}
fill="none"
stroke="green"
strokeWidth={strokeWidth}
strokeDasharray={`${progress * 100} ${100 - (progress * 100)}`}
strokeLinecap="round"
pathLength="100"
/>
<SvgText
x="35"
y="40"
textAnchor="middle"
fill="#858585"
fontSize="20"
fontFamily="Arial"
>
{dayName}
</SvgText>
<SvgText
x="35"
y="75"
textAnchor="middle"
fill={isFutureDate ? '#757575' : 'white'}
fontSize="20"
fontFamily="Arial"
>
{dayNumber}
</SvgText>
</Svg>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
},
container: {
justifyContent: 'center',
alignItems: 'center',
width: 70,
height: 100,
},
});
export default DateComponent;