البرمجة

تحسين تظليل الصور في تطبيق React Native باستخدام الشفافية

في محاولتي لتطبيق تراكب عنوان فوق صورة مع تظليل الصورة بشفافية منخفضة، وجدت أن التأثير الشفاف يؤثر أيضًا على النص المتراكب، جعله باهتًا. هل هناك حلاً لهذا؟ إليك كيف يظهر الكود الخاص بي للمكون المخصص (معاينة المقالة – حيث تكون الصورة أحد مكونات معاينة المقالة):

jsx
import React from 'react'; import { View, StyleSheet, Text, Image, TouchableHighlight } from 'react-native'; import Dimensions from 'Dimensions'; import ImageButton from '../../common/imageButton'; import KeywordBox from '../../authentication/onboarding/keyword-box'; export default class ArticlePreview extends React.Component { render() { return ( <TouchableHighlight underlayColor={'transparent'} onPress={this.props.onPress}> <Image source={this.props.source} style={[styles.articlePreview, this.border('red')]}> <View style={[styles.container, this.border('orange')]}> <View style={[styles.header, this.border('blue')]}> <Text style={[styles.previewText]}>{this.props.text}Text> View> <View style={[styles.footer, this.border('white')]}> <View style={[styles.heartRow, this.border('black')]}> <ImageButton style={[styles.heartBtn, , this.border('red')]} resizeMode={'contain'} onPress={this.onHeartPress} source={require('../../img/heart_btn.png')} /> <Text style={[styles.heartText]}>{this.props.heartText + ' المفضلة'}Text> View> <KeywordBox style={[styles.category, this.border('blue')]} key={this.props.key} text={this.props.category} onPress={this.props.categoryPress} selected={this.props.selected} /> View> View> Image> TouchableHighlight> ); } onHeartPress() { // سيتم نقل هذه الوظيفة إلى وحدة عامة } border(color) { return { //borderColor: color, //borderWidth: 4, }; } } const styles = StyleSheet.create({ heartText: { color: 'white', fontSize: 12, fontWeight: 'bold', alignSelf: 'center', marginLeft: 5, fontFamily: 'SFCompactText-Medium' }, heartRow: { flexDirection: 'row', justifyContent: 'space-around', alignSelf: 'center', justifyContent: 'center', }, heartBtn: { height: (92 / 97) * (Dimensions.get('window').width / 13), width: Dimensions.get('window').width / 13, alignSelf: 'center', }, category: { fontFamily: 'Bebas Neue', fontSize: 10, fontWeight: 'bold' }, header: { flex: 3, alignItems: 'center', justifyContent: 'space-around', marginTop: Dimensions.get('window').height / 30, }, footer: { flex: 1, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', margin: Dimensions.get('window').height / 50, }, container: { flex: 1, backgroundColor: 'black', opacity: 0.6, }, articlePreview: { flex: 1, height: Dimensions.get('window').height / 3.2, width: Dimensions.get('window').width, flexDirection: 'column' }, previewText: { fontFamily: 'Bebas Neue', fontSize: 23, color: 'white', alignSelf: 'center', textAlign: 'center', margin: 5, position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 }, });

للتغلب على هذه المشكلة، يمكنك تطبيق الشفافية على الصورة نفسها بدلاً من عنصر العنوان. يمكنك تحقيق ذلك عبر تغيير الشفافية في styles.container إلى 1 (أو أي قيمة تتناسب مع متطلباتك)، ومن ثم إضافة خلفية مظلمة باستخدام عنصر إضافي لتحقيق التظليل.

jsx
container: { flex: 1, backgroundColor: 'rgba(0,0,0,0.6)', },

بهذا، يمكنك تحقيق تظليل الصورة بشكل مستقل عن النص، وبالتالي لن يتأثر النص بتأثير الشفافية.

المزيد من المعلومات

بالتأكيد، دعني أوسع معرضك للمزيد من المعلومات حول التحدي الذي تواجهه في تطوير تطبيق React Native والذي يتعلق بتراكب النص على الصورة مع تظليل الصورة بشفافية منخفضة.

في الوقت الحالي، قدمت لك حلاً لتجنب تأثير الشفافية على النص. ومع ذلك، إذا كان لديك أي تفاصيل إضافية حول المتطلبات الخاصة بالتطبيق، مثل الوظائف الإضافية التي تتوقع تحقيقها، أو أي تعديلات أخرى قد تحتاجها في تصميم واجهة المستخدم، يرجى مشاركتها لكي أتمكن من تقديم دعم أفضل وتوجيهات أكثر دقة.

هل هناك نقاط محددة ترغب في مناقشتها أو تعديلات إضافية تحتاج إليها في الكود الحالي؟ أو هل تحتاج إلى نصائح حول أفضل الممارسات في تطوير تطبيقات React Native بشكل عام؟ سأكون سعيدًا بمساعدتك بما يتناسب مع احتياجاتك الخاصة.

زر الذهاب إلى الأعلى