Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: Change opacity colour of ImageBackground

Tags:

I have been trying to develop screen mentioned below:

For that I have created below component:

import React, {Component} from 'react';
import {View, Text, StyleSheet, ImageBackground, Image} from 'react-native';
import Balance from './Balance.js'

class AccountHeader extends React.Component{
    render(){
        return(
            <ImageBackground
                source={require('../images/lawrance.jpg')}
                style={styles.container}>
                    <View style={styles.overlay}></View>
                    <Text style = {[styles.textStyle, {paddingTop: 10}]} >My Account</Text>
                    <Image source= {require('../images/lawrance.jpg')}
                        style={styles.avatarStyle}/>
                    <Text style = {styles.textStyle} > Jenifer Lawrance</Text>
                    <Text style = {styles.textStyle} > +14155552671</Text>
                    <Balance style= {styles.balanceContainer}/>
            </ImageBackground>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor:'red',
        opacity: 0.6
    },
    overlay: {
        backgroundColor:'transparent',
        opacity: 0.6
    },
    avatarStyle: {
        width:100, 
        height: 100,
        marginTop: 10,
        borderRadius: 50,
        alignSelf: 'center',
    },
    textStyle: {
        marginTop: 10,
        fontSize: 18,
        color: "#FFFFFF",
        fontWeight: 'bold',
        alignSelf: 'center',
    },
    balanceContainer:{
        padding:10,
    }
  });

export default AccountHeader;

Now here are two issues:

  1. Changing the opacity of ImageBackground also change the opacity of its children
  2. Not able to change the color of opacity

Any help appreciated!

Design screen:

enter image description here

Developed Screen

enter image description here

like image 393
AndiGeeky Avatar asked Mar 21 '18 05:03

AndiGeeky


People also ask

How do I change the color of opacity in React Native?

Specify Opacity of a Color in React If you want to specify the opacity of background color, you should use the rgba() , which is slightly different from rgb() function. The decimal value can be anything from 0 to 1 . In this case, our background will be 30% colored and 70% transparent .

How do you reduce the opacity of background image in React Native?

create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'white', }, image: { height: 250, width: 250, opacity: 0.5, }, }); export default Home; The output will be as given in the screenshot below. That's how you increase or reduce transparency of image in react native.

How do I change the color of an image in React Native?

Well, you can also change the color of an image or icon in React Native. Precisely saying, you can change the color of all the non-transparent pixels of the image. To achieve this, all you need is to add the style prop tintColor to your image component.


1 Answers

Try this :

<ImageBackground source={require('./images/backgroundBlue.jpg')} imageStyle= 
{{opacity:0.5}}/> 

it works

like image 179
L.G Avatar answered Sep 22 '22 04:09

L.G