You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo相机视图内边框圆角及取景框配置问题

解决方案:实现带内圆角的相机取景框

核心思路

通过多层视图组合构建半透明遮罩,底层放置全屏相机组件,上层用四个半透明区块(上下左右)围出中间带圆角的清晰取景区域,避开单个遮罩无法实现内圆角的问题。

代码修改示例

基于现有代码调整遮罩部分:

import { View, StyleSheet, Dimensions } from 'react-native';
import { Camera } from 'expo-camera';

const { width, height } = Dimensions.get('window');
const frameSize = width * 0.8; // 自定义取景框尺寸

export default function CameraScreen() {
  // 相机权限、状态逻辑保持原有实现

  return (
    <View style={styles.container}>
      <Camera style={styles.camera} type={Camera.Constants.Type.back}>
        {/* 遮罩层:四个半透明区块围出中间圆角区域 */}
        <View style={styles.maskWrapper}>
          {/* 顶部遮罩 */}
          <View style={styles.topMask} />
          {/* 中间左右遮罩+取景框 */}
          <View style={styles.middleRow}>
            <View style={styles.sideMask} />
            <View style={styles.viewfinder} />
            <View style={styles.sideMask} />
          </View>
          {/* 底部遮罩 */}
          <View style={styles.bottomMask} />
        </View>
      </Camera>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  camera: {
    width: '100%',
    height: '100%',
    position: 'relative',
  },
  maskWrapper: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
  },
  topMask: {
    flex: 1,
    backgroundColor: 'rgba(0,0,0,0.7)',
  },
  middleRow: {
    flexDirection: 'row',
    height: frameSize,
  },
  sideMask: {
    flex: 1,
    backgroundColor: 'rgba(0,0,0,0.7)',
  },
  viewfinder: {
    width: frameSize,
    borderRadius: 20, // 设置内圆角大小
    borderWidth: 2, // 可选:添加取景框边框
    borderColor: '#ffffff',
    overflow: 'hidden',
  },
  bottomMask: {
    flex: 1,
    backgroundColor: 'rgba(0,0,0,0.7)',
  },
});

关键说明

  • 上下左右四个半透明区块组成遮罩,中间的viewfinder通过borderRadius实现圆角,底层相机画面会透过该区域清晰显示。
  • 可通过调整frameSize、borderRadius、遮罩透明度rgba(0,0,0,0.7)匹配设计需求。
  • 若需要取景框内边距,可在viewfinder内嵌套带padding的空视图,或调整viewfinder的margin控制与遮罩的间距。

内容的提问来源于stack exchange,提问作者ahmed hodzic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 15:43:22