我有一个带有 png 图像的屏幕,我正在尝试在关节上添加可触摸的东西。我想让 touchabes 在不同的屏幕尺寸上保持一致
这是我的代码
import {
Platform,
ImageBackground,
View,
StyleSheet,
TouchableOpacity,
Dimensions,
PixelRatio
} from 'react-native';
const { width, height } = Dimensions.get('window');
const radius = PixelRatio.roundToNearestPixel(width / 2);
const getWidthPercent = (percent) => (width * percent) / 100;
const getHeightPercent = (percent) => (height * percent) / 100;
<View style={styles.container}>
<ImageBackground
style={styles.image}
resizeMode="contain"
source={require('../../assets/skeleton-large.png')}>
<TouchableOpacity style={styles.leftShoulder}></TouchableOpacity>
<TouchableOpacity style={styles.rightShoulder}></TouchableOpacity>
</ImageBackground>
</View>
联合样式默认值
const jointStyleDefaults = {
backgroundColor: '#CC41444B',
position: 'absolute',
width: radius * 0.3, // to keep the size of circle responsive across screen size
height: radius * 0.3,
borderRadius: radius
};
样式表
const styles = StyleSheet.create({
container: {
flex: 1,
},
image: {
flex: 1
},
leftShoulder: {
...jointStyleDefaults,
top: getHeightPercent(13),
bottom: 0,
right: 0,
left: getWidthPercent(25)
},
rightShoulder: {
...jointStyleDefaults,
top: getHeightPercent(13),
bottom: 0,
right: 0,
left: getWidthPercent(60)
},
});
我想在不同屏幕尺寸的 png 上将圆形图像保持在固定点。我如何实现这一目标?