अभिक्रिया मूल में पारदर्शी रंग का पृष्ठभूमि रंग कैसे सेट करें


139

यह उस दृश्य की शैली है जिसका मैंने उपयोग किया है

backCover: {
  position: 'absolute',
  marginTop: 20,
  top: 0,
  bottom: 0,
  left: 0,
  right: 0,
}

वर्तमान में इसकी एक सफेद पृष्ठभूमि है। मैं बैकग्राउंड चेंज कर सकता हूं जैसा मैं चाहता हूं, '#343434'लेकिन यह केवल रंग के लिए अधिकतम 6 हेक्सवल्यू स्वीकार करता है, इसलिए मैं उस तरह से अस्पष्टता नहीं दे सकता '#00ffffff'। मैंने इस तरह अपारदर्शिता का उपयोग करने की कोशिश की

backCover: {
  position: 'absolute',
  marginTop: 20,
  top: 0,
  bottom: 0,
  left: 0,
  right: 0,
  opacity: 0.5,
}

लेकिन यह दृश्य की सामग्री की दृश्यता कम कर देता है। तो कोई जवाब?

जवाबों:


288

के लिए rgbaमान का उपयोग करें backgroundColor

उदाहरण के लिए,

backgroundColor: 'rgba(52, 52, 52, 0.8)'

यह 80% अस्पष्टता है, जो अस्पष्टता दशमलव से प्राप्त होता है के साथ एक ग्रे रंग के लिए यह सेट 0.8। यह मान कुछ भी हो सकता 0.0है 1.0


क्यों पृथ्वी पर कलर वैल्स 8 बिट और अल्फा वैल्स फ्लोट हैं?
द्वैमासिक

@duhaime, निश्चित रूप से विशेष रूप से क्यों नहीं, लेकिन 8-बिट्स एक मेमोरी सेंस (विशेषकर ऐतिहासिक रूप से) से समझ में आता है। अल्फा मान 0 और 1 को न्यूनतम और पूरी तरह से पारदर्शी या पूरी तरह से अपारदर्शी के रूप में अधिकतम होने के लिए अधिक समझ में आता है। उदाहरण के लिए, यदि आप 25% पारदर्शी होना चाहते हैं, तो आप यह पता लगाना नहीं चाहते हैं कि 255 में से 1 / 4th क्या है।
kojow7

104

निम्नलिखित ठीक काम करता है:

backgroundColor: 'rgba(52, 52, 52, alpha)'

आप भी कोशिश कर सकते हैं:

backgroundColor: 'transparent'

2
पृष्ठभूमि का रंग: 'पारदर्शी' अब तक का सबसे आसान उपाय है।
नाथनएल

27

backgroundColor: '#00000000' इसे आज़माएं , यह पृष्ठभूमि के रंग को पारदर्शी बना देगा, यह #rggbbaa हेक्स कोड्स का अनुसरण करता है


किसी कारण से, यह संस्करण गलत तरीके से अस्पष्टता के साथ परिणाम रंग प्रदर्शित करता है। अगर मैं गलत नहीं हूँ तो यह RN में एक बग है। इसलिए बेहतर है कि rgbaरास्ते का इस्तेमाल करें ।
श्येनगिस कासिमोव

1
@ShyngysKassymov gist.github.com/lopspower/03fb1cc0ac9f32ef38f4 इसे चेक करें
Oo

@ कोई दिलचस्प, जो समझ में आता है। इशारा करने के लिए धन्यवाद! लेकिन IMO यह आसान rgbaतरीका है :)
Shyngys Kassymov

इसका मतलब यह है कि प्रारूप के बजाय #aarrggbb होना चाहिए?
श्येनगिस कासिमोव

मेरा मतलब है कि आप हेक्सावल्यू का उपयोग कर सकते हैं rrggbbaa
ओओ

3

आपको वर्तमान संघर्षों के बारे में पता होना चाहिए जो iOS और RGBA पृष्ठभूमि के साथ मौजूद हैं।

सारांश: सार्वजनिक प्रतिक्रिया मूल निवासी वर्तमान में iOS परत छाया गुणों को अधिक या कम सीधे उजागर करता है, हालांकि इसके साथ कई समस्याएं हैं:

1) इन गुणों का उपयोग करते समय प्रदर्शन डिफ़ॉल्ट रूप से खराब होता है। ऐसा इसलिए है क्योंकि iOS दृश्य के सटीक पिक्सेल मास्क को प्राप्त करके छाया की गणना करता है, जिसमें किसी भी ट्रांसल्यूसेंट सामग्री और इसके सभी साक्षात्कार शामिल हैं, जो बहुत सीपीयू और जीपीयू-गहन है। 2) iOS छाया गुण CSS बॉक्स-छाया मानक के सिंटैक्स या शब्दार्थ से मेल नहीं खाते हैं, और Android पर लागू होने की संभावना नहीं है। 3) हम layer.shadowPathसंपत्ति को उजागर नहीं करते हैं, जो परत छाया से अच्छा प्रदर्शन प्राप्त करने के लिए महत्वपूर्ण है।

यह एक समस्या को हल करता है नंबर 1) एक डिफ़ॉल्ट को लागू करने से जो shadowPathएक अपारदर्शी पृष्ठभूमि के साथ विचारों के लिए दृश्य सीमा से मेल खाता है। यह सामान्य उपयोग के मामले के लिए अनुकूलन करके छाया के प्रदर्शन में सुधार करता है। मैंने उन दृश्यों के लिए पृष्ठभूमि रंग प्रचार को भी बहाल किया है जिनमें छाया प्रॉप्स हैं - यह सुनिश्चित करने में मदद करनी चाहिए कि यह सबसे अच्छी स्थिति अधिक बार होती है।

एक स्पष्ट पारदर्शी पृष्ठभूमि वाले दृश्यों के लिए, छाया काम करना जारी रखेगी जैसा कि उसने पहले किया था (इसे shadowPathपरेशान नहीं छोड़ा जाएगा, और छाया बिल्कुल दृश्य और उसके साक्षात्कार के पिक्सेल से प्राप्त की जाएगी)। हालांकि, प्रदर्शन के लिए यह सबसे खराब स्थिति है, इसलिए आपको इससे बचना चाहिए जब तक कि बिल्कुल आवश्यक न हो। इसके लिए समर्थन भविष्य में डिफ़ॉल्ट रूप से अक्षम हो सकता है, या पूरी तरह से गिरा दिया जा सकता है।

पारभासी छवियों के लिए, यह सुझाव दिया जाता है कि आप छाया को छवि में ही बेक करें, या छाया को पूर्व-उत्पन्न करने के लिए किसी अन्य तंत्र का उपयोग करें। टेक्स्ट शैडो के लिए, आपको टेक्स्टशैडो प्रॉपर्टीज़ का उपयोग करना चाहिए, जो क्रॉस-प्लेटफ़ॉर्म पर काम करती हैं और जिनमें बेहतर प्रदर्शन होता है।

समस्या संख्या 2) भविष्य में एक अलग रूप में हल हो जाएगी, संभवतः iOS शैडोएक्सएक्सएक्स गुणों को बॉक्सशैडोएक्सएक्सएक्सएक्सएक्सएक्स का नाम बदलकर, और सीएसएस मानकों से मिलान करने के लिए सिंटैक्स और शब्दार्थ को बदलकर।

समस्या नंबर 3) अब ज्यादातर मूट है, क्योंकि हम शैडोपाथ को स्वचालित रूप से जेनरेट करते हैं। भविष्य में, यदि हम छाया के अधिक सटीक नियंत्रण की मांग करते हैं, तो हम स्पष्ट रूप से पथ को सेट करने के लिए iOS-विशिष्ट प्रस्ताव प्रदान कर सकते हैं।

समीक्षित: वीकोल

प्रतिबद्ध: https://github.com/facebook/react-native/commit/e4c53c28aea7e067e48f5c8c0100c7cafc031b06


2

आश्चर्यजनक रूप से किसी ने इस बारे में नहीं बताया, जो कुछ स्पष्टता प्रदान करता है:

style={{
backgroundColor: 'white',
opacity: 0.7
}}

6
यह समाधान पूरे दृश्य को अस्पष्टता को परिभाषित करता है, न कि केवल इसकी पृष्ठभूमि को, जिसके परिणामस्वरूप इसके सभी बच्चे अर्ध-अपारदर्शी बन जाते हैं (यह वास्तव में मूल प्रश्न में बताया गया है)
कूल शीतल

-1

यहाँ एक मोडल का मेरा समाधान है जिसे किसी भी स्क्रीन पर प्रस्तुत किया जा सकता है और App.tsx में इनिशियलाइज़ किया जा सकता है

ModalComponent.tsx

import React, { Component } from 'react';
import { Modal, Text, TouchableHighlight, View, StyleSheet, Platform } from 'react-native';
import EventEmitter from 'events';
// I keep localization files for strings and device metrics like height and width which are used for styling 
import strings from '../../config/strings';
import metrics from '../../config/metrics';

const emitter = new EventEmitter();
export const _modalEmitter = emitter

export class ModalView extends Component {
    state: {
        modalVisible: boolean,
        text: string, 
        callbackSubmit: any, 
        callbackCancel: any,
        animation: any
    }

    constructor(props) {
        super(props)
        this.state = {
            modalVisible: false,
            text: "", 
            callbackSubmit: (() => {}), 
            callbackCancel: (() => {}),
            animation: new Animated.Value(0)
        } 
    }

    componentDidMount() {
        _modalEmitter.addListener(strings.modalOpen, (event) => {
            var state = {
                modalVisible: true,
                text: event.text, 
                callbackSubmit: event.onSubmit, 
                callbackCancel: event.onClose,
                animation: new Animated.Value(0)
            } 
            this.setState(state)
        })
        _modalEmitter.addListener(strings.modalClose, (event) => {
            var state = {
                modalVisible: false,
                text: "", 
                callbackSubmit: (() => {}), 
                callbackCancel: (() => {}),
                animation: new Animated.Value(0)
            } 
            this.setState(state)
        })
    }

    componentWillUnmount() {
        var state = {
            modalVisible: false,
            text: "", 
            callbackSubmit: (() => {}), 
            callbackCancel: (() => {})
        } 
        this.setState(state)
    }

    closeModal = () => {
        _modalEmitter.emit(strings.modalClose)
    }

    startAnimation=()=>{
        Animated.timing(this.state.animation, {
            toValue : 0.5,
            duration : 500
        }).start()
    }

    body = () => {
        const animatedOpacity ={
            opacity : this.state.animation
        }
        this.startAnimation()
        return (
            <View style={{ height: 0 }}>
                <Modal
                    animationType="fade"
                    transparent={true}
                    visible={this.state.modalVisible}>

                    // render a transparent gray background over the whole screen and animate it to fade in, touchable opacity to close modal on click out

                    <Animated.View style={[styles.modalBackground, animatedOpacity]} > 
                        <TouchableOpacity onPress={() => this.closeModal()} activeOpacity={1} style={[styles.modalBackground, {opacity: 1} ]} > 
                        </TouchableOpacity>
                    </Animated.View>

                    // render an absolutely positioned modal component over that background
                    <View style={styles.modalContent}>

                        <View key="text_container">
                            <Text>{this.state.text}?</Text>
                        </View>
                        <View key="options_container">
                            // keep in mind the content styling is very minimal for this example, you can put in your own component here or style and make it behave as you wish
                            <TouchableOpacity
                                onPress={() => {
                                    this.state.callbackSubmit();
                                }}>
                                <Text>Confirm</Text>
                            </TouchableOpacity>

                            <TouchableOpacity
                                onPress={() => {
                                    this.state.callbackCancel();
                                }}>
                                <Text>Cancel</Text>
                            </TouchableOpacity>

                        </View>
                    </View>
                </Modal>
            </View> 
        );
    }

    render() {
        return this.body()
    }
}

// to center the modal on your screen 
// top: metrics.DEVICE_HEIGHT/2 positions the top of the modal at the center of your screen
// however you wanna consider your modal's height and subtract half of that so that the 
// center of the modal is centered not the top, additionally for 'ios' taking into consideration
// the 20px top bunny ears offset hence - (Platform.OS == 'ios'? 120 : 100)
// where 100 is half of the modal's height of 200
const styles = StyleSheet.create({
    modalBackground: {
        height: '100%', 
        width: '100%', 
        backgroundColor: 'gray', 
        zIndex: -1 
    },
    modalContent: { 
        position: 'absolute', 
        alignSelf: 'center', 
        zIndex: 1, 
        top: metrics.DEVICE_HEIGHT/2 - (Platform.OS == 'ios'? 120 : 100), 
        justifyContent: 'center', 
        alignItems: 'center', 
        display: 'flex', 
        height: 200, 
        width: '80%', 
        borderRadius: 27,
        backgroundColor: 'white', 
        opacity: 1 
    },
})

App.tsx प्रस्तुत करना और आयात करना

import { ModalView } from './{your_path}/ModalComponent';

render() {
    return (
        <React.Fragment>
            <StatusBar barStyle={'dark-content'} />
            <AppRouter />
            <ModalView />
        </React.Fragment>
    )
}

और किसी भी घटक से इसका उपयोग करने के लिए

SomeComponent.tsx

import { _modalEmitter } from './{your_path}/ModalComponent'

// Some functions within your component

showModal(modalText, callbackOnSubmit, callbackOnClose) {
    _modalEmitter.emit(strings.modalOpen, { text: modalText, onSubmit: callbackOnSubmit.bind(this), onClose: callbackOnClose.bind(this) })
}

closeModal() {
    _modalEmitter.emit(strings.modalClose)
}

आशा है कि मैं आपकी कुछ मदद करने में सक्षम था, मैंने इन-ऐप सूचनाओं के लिए एक समान संरचना का उपयोग किया

खुश कोडिंग

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.