ContentMessageTabView.js
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Created by zzz on 2019/3/5.
*/
'use strict';
import React, {Component} from "react";
import {StyleSheet, Text, View, Image, ART} from "react-native";
import YH_Image from "../../../common/components/YH_Image";
const {Path, Shape, Surface, Group} = ART;
const INVALID_POSITION = -1;
const IMAGE_WIDTH = 50;
const IMAGE_HEIGHT = 50;
export default class ContentMessageTabView extends Component {
constructor(props) {
super(props);
this.state = {
badgeX: INVALID_POSITION,
badgeY: INVALID_POSITION,
badgeWidth: INVALID_POSITION,
badgeHeight: INVALID_POSITION
};
this.onImageLayout = this.onImageLayout.bind(this)
this.onBadgeLayout = this.onBadgeLayout.bind(this)
}
onImageLayout(event) {
let imageLayout = event.nativeEvent.layout
this.setState({
badgeY: imageLayout.y - 4,
badgeX: imageLayout.x + imageLayout.width / 2 + 17,
})
}
onBadgeLayout(event) {
let badgeLayout = event.nativeEvent.layout
this.setState({badgeWidth: badgeLayout.width, badgeHeight: badgeLayout.height})
}
renderSurface() {
if (this.state.badgeWidth != INVALID_POSITION && this.state.badgeHeight != INVALID_POSITION) {
let width = this.state.badgeWidth, height = this.state.badgeHeight
let strokeWidth = 1.5
const radius = (height - strokeWidth) / 2, x = strokeWidth / 2 + radius, y = (height) / 2;
const path = new Path()
.moveTo(x, y - radius)
.counterArc(0, radius * 2, radius)
.lineTo(width - radius - strokeWidth / 2, y + radius)
.counterArc(0, -radius * 2, radius).close();
return <View style={styles.surfaceContainer}>
<Surface width={width} height={height}>
<Group>
<Shape d={path} stroke={'#FB2330'} fill={'#FB2330'} strokeWidth={strokeWidth}/>
</Group>
</Surface>
</View>
}
return null;
}
render() {
return <View style={[styles.container, this.props.style]}>
<Image style={styles.icon} source={this.props.icon} onLayout={this.onImageLayout}/>
<Text style={styles.title}>{this.props.title}</Text>
{this.props.badge > 0 && this.state.badgeX != INVALID_POSITION && this.state.badgeY != INVALID_POSITION &&
<View style={[styles.badgeContainer, {left: this.state.badgeX, top: this.state.badgeY}]}
onLayout={this.onBadgeLayout}>
{this.renderSurface()}
<Text style={styles.badgeTitle}>{this.props.badge}</Text>
</View>
}
</View>
}
}
let styles = StyleSheet.create({
container: {
marginTop: 33,
marginBottom: 40,
flexDirection: 'column',
justifyContent: 'center',
paddingLeft: 30,
paddingRight: 30,
},
title: {
fontFamily: 'PingFang-SC-Medium',
fontSize: 14,
marginTop: 15,
color: '#4A4A4A',
textAlign:'center',
},
icon: {
width: 50,
height: 50,
},
badgeContainer: {
backgroundColor: 'transparent',
height: 17,
minWidth: 17,
alignItems: 'center',
justifyContent: 'center',
flex: 0,
position: 'absolute'
},
surfaceContainer: {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
},
badgeTitle: {
marginLeft: 5,
marginRight: 5,
fontSize: 10,
color: 'white',
fontWeight: 'bold',
}
});