How to scale correctly Titanium ImageView inside its parent? - titanium-mobile

I'm working on Windows 10 starting to learn Titanium SDK 6.0.1.GA and Alloy 1.9.8, I want to display an ImageView filled with an image chosen from the gallery, this component is placed inside a View in my .xml like this:
<Alloy>
<Window class="container">
<ActionBar id="actionBar" platform="android" title="Contact Details"></ActionBar>
<Menu>
<MenuItem id="saveContactMenu" title="Save" onClick="saveContact"/>
</Menu>
<View class="contact-details-holder">
<View class="contact-photo-holder" onClick="openOptions">
<ImageView id="photo" class="contact-photo"/>
</View>
<View class="contact-info-holder">
<Label class="label">Nombre</Label>
<Label id="fullname" class="text"/>
</View>
<View class="address-info-holder">
<View class="address-holder">
<Label class="label">Direccion</Label>
<Label id="address" class="text"/>
</View>
<View class="map-holder">
</View>
</View>
<View class="contact-info-holder">
<Label class="label">T</Label>
<Label id="phone" class="text"/>
</View>
</View>
<OptionDialog id="photoDialog" title="Select source" onClick="handleSelectedOption" cancel="3">
<Options>
<Option>Camera</Option>
<Option>Photo Gallery</Option>
<Option>From the Web</Option>
<Option>Cancel</Option>
</Options>
</OptionDialog>
</Window>
</Alloy>
For which the .tss is:
'.container' : {
width : Ti.UI.FILL,
height : Ti.UI.FILL,
backgroundColor:'white'
}
'.contact-details-holder' : {
width : Ti.UI.FILL,
height : Ti.UI.SIZE,
layout: "vertical"
}
'.contact-photo-holder': {
width : Ti.UI.FILL,
height : '44 dp',
top: 0,
backgroundColor: "pink"
}
'.contact-photo': {
width : Ti.UI.SIZE,
height: Ti.UI.SIZE
}
'.contact-info-holder' : {
width : Ti.UI.FILL,
height: Ti.UI.SIZE,
top: 0,
layout: "vertical",
backgroundColor: "lime"
}
'.label': {
width : Ti.UI.SIZE,
height: Ti.UI.FILL,
left : 0,
font: {
fontFamily:'Helvetica',
fontSize: '12dp',
fontStyle: 'normal',
fontWeight: 'normal'
},
color: '#343434'
}
'.text': {
width : Ti.UI.SIZE,
height: Ti.UI.FILL,
left : 0,
font: {
fontFamily:'Helvetica',
fontSize: '12dp',
fontStyle: 'normal',
fontWeight: 'normal'
},
color: 'black'
}
The View has its width set to Ti.UI.FILL and the height to 44 dps, while the ImageView has its width and height set to Ti.UI.SIZE, with this I would expect the image size to be that of the original image and the View would clip on top of it showing only the center of the image, but it's shown like this following image:
Am I applying the wrong styles in the tss?

try this
.xml
<View class="contact-photo-holder" onClick="openOptions">
<View id="photoContainer">
<ImageView id="photo" class="contact-photo"/>
</View>
</View>
.tss
"#photoContainer":{
width : Ti.UI.FILL,
height : Ti.UI.SIZE
}
"#photo":{
left: 0,
right : 0,
height : "auto"
}

Related

How to Set background image with navigation bar in react-native using react-navigation

I have to achieve Below output :
What I achieved
Here is My source code :
return (
<ImageBackground source={require('../../assets/images/home_bg.png')} style={{
height: 185,
width: '100%',
}}>
<Header style={{
backgroundColor: 'transparent'
}}
androidStatusBarColor={"#EB7D16"} >
<View style={{ flex: 0.5, alignItems: 'center', alignContent: 'center', alignSelf: 'center' }}>
<Left>{LeftIcon}</Left>
</View>
<View style={{ flex: 4, justifyContent: 'center', alignItems: 'center' }}>
<CustomSearchBar />
</View>
<View style={{ flex: 0.5, alignContent: 'center', alignItems: 'center' }}>
<Right>{RightIcon}</Right>
</View>
</Header>
<View style={{ alignItems: 'center', justifyContent: 'center' }}>
<Image resizeMode="contain" source={require('../../assets/icons/logo.png')} style={{ height: '50%', width: '50%', marginTop: 20 }}>
</Image>
</View>
</ImageBackground >
);
I used Image Background and then Set Header component on that module.
Here, I set ImageBackground height: 185, width: '100%', which is wrong way and it's not responsive for all devices. I also use windows.height and windows.width but it's also not working, Kindly share proper way for setting this background Image and How to achieve may targeted output.
Thank you in advance!
you need to make status bar "translucent" so that background image starts from top.
try this and add in your main render of screen
import { StatusBar } from "react-native";
...
<StatusBar translucent backgroundColor="transparent" />
I resolved it by my slef.. actually issue with background image.
Thank you all for time!

facing problems with styling on react native and achieving a better responsive layout

I tried to run my code on various screen sized emulators and the layout of my design varies from device to device. I need help to make the layout more responsive and fixed on different sized screens such as tablets, 3"mobile and 6"mobile.
-----------or connect with us on----------- I want to style this line in better way and that remains the same look on every device
I tried using the dimensions of the screen by defining the width and height using variables, and by describing the values in percentages, but nothing worked.
import React, { Component } from 'react';
import { View,TouchableOpacity, Text, StyleSheet, Image, Dimensions } from 'react-native';
import BacgroundImage from './BackgroundImage';
import Buttons from './Reusable/Button';
const { width: WIDTH } = Dimensions.get('window');
class LaunchScreen extends Component {
render() {
return (
<BacgroundImage>
<View style={styles.logoContainer}>
<Image
source={require('./images/logo.png')}
style={styles.PlaceLogo}
/>
</View>
<View style={styles.Buttons}>
<Buttons style={styles.signupButton}
onPress={() => navigate('Login')}>
<Text style={styles.buttonText}> SIGN UP</Text>
</Buttons>
<Buttons style={styles.loginButton}>
<Text style={styles.buttonText}> SIGN IN</Text>
</Buttons>
</View >
<View style={styles.sepratorView}>
<Text style={styles.sepratorText}>--------- or connect with us on ---------</Text>
</View >
<View style={styles.socialButtonStyle}>
<TouchableOpacity
style={styles.fbstyle}
activeOpacity={0.5}
>
<Image
source={require('./images/facebookicon.png')}
style={styles.iconstyle}
/>
<View
style={styles.sepratorLine}
/>
<Text
style={styles.socialButtonText}
>FACEBOOK</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.googlestyle}
onPress={this.signIn}
activeOpacity={0.5}
>
<Image
source={require('./images/google.png')}
style={styles.iconstyle}
/>
<View
style={styles.sepratorLine}
/>
<Text style={styles.socialButtonText} >GOOGLE</Text>
</TouchableOpacity>
</View>
</BacgroundImage>
);
};
}
const styles = StyleSheet.create({
logoContainer: {
flex: 1
},
PlaceLogo: {
width: WIDTH - 140,
margin: 75,
resizeMode: 'center',
justifyContent: 'center',
alignItems: 'center',
},
yosoButtons: {
width: WIDTH - 80,
justifyContent:'center',
marginTop:350
},
signupButton: {
height:40,
paddingTop:7,
marginBottom: 15,
},
loginButton: {
height:40,
paddingTop:7,
marginBottom: 15
},
buttonText: {
},
sepratorText: {
textAlign:'center',
fontSize:20,
color: '#b6b7ba',
},
sepratorView: {
flexDirection: 'row',
justifyContent:'center',
alignItems:'center'
},
socialButtonStyle:{
flex:1,
flexDirection:'row',
justifyContent:'center',
alignItems:'center',
margin:5
},
fbstyle:{
flex:1,
flexDirection:'row',
borderColor:'white',
alignItems:'center',
backgroundColor:'#485a96',
borderWidth:1,
borderRadius:35,
height: 40,
marginLeft:15,
marginRight:15
},
googlestyle:{
flex:1,
flexDirection:'row',
alignItems:'center',
backgroundColor:'#dc4e41',
borderWidth:1,
borderRadius:35,
borderColor:'white',
height: 40,
marginRight:15
},
iconstyle:{
resizeMode:'stretch',
height:25,
width:25
},
sepratorLine:{
backgroundColor:'white',
width:2,
height: 40
},
socialButtonText:{
color: '#fff',
flex:1,
textAlign: 'center',
fontSize: 15,
fontWeight: "bold"
}
});
export default LaunchScreen;
I want the layout to remain the same. Both the logo and buttons remain at the same position on every device display.
And I want a better dynamic solution for separator I have used in code
--------------or connect with us on-----------
I got a workaround solution for this problem. I am assigning values using the height and width of the screen to the styling.
import { StyleSheet, Dimensions } from 'react-native';
const { width: WIDTH, height: HEIGHT } = Dimensions.get('window');
SomeText: {
fontSize: HEIGHT / 38,
marginTop: WIDTH / 20,
}

How to fit an image inside the Text component?

I Need to insert an image inside the text so, I do it with the most simple way:
const avatarurl = 'https://upload.wikimedia.org/wikipedia/commons/6/6e/Golde33443.jpg'
<ScrollView>
<View style={styles.articleContent}>
<View style={styles.p}>
<Text style={{backgroundColor:'pink', width: 300, height: 100 }}>
<Image style={{width: 300, height: 50}} source={{uri: avatarurl}}/>
</Text>
</View>
</View>
</ScrollView>
const styles = {
articleContent: {
paddingTop: 50,
paddingLeft: 20,
paddingRight: 20,
borderColor: 'red',
},
p: {
paddingBottom: 15,
flex: 1,
},
textCaption: {
fontSize: 13,
color: 'black',
},
}
Here is the result:
I Don't know why the entire image is not inside the Text. So, I tried one more time with text:
<ScrollView>
<View style={styles.articleContent}>
<View style={styles.p}>
<Text style={{backgroundColor:'pink', width: 300, height: 200}}>
<Text style={styles.textCaption}>Well-known discrete probability distributions used in statistical modeling include</Text>
<Image style={{width: 30, height: 30}} source={{uri: avatarurl}}/>
<Text style={styles.textCaption}> the Poisson distribution, the Bernoulli distribution, the binomial distribution, </Text>
</Text>
<Text style={styles.text}>asdasd asd asd asd asd asdas das das d</Text>
</View>
</View>
</ScrollView>
Ok, the image is not fitting inside the line of the text. I tried some styles like: textAlign, lineHeight. But nothing helps. Is there a way to do this? The image needs to be inside the text:
Like this:
The image may not invade the space of the other line. And the words need to stay in the bottom of the line.
In order to put the image inside the text, you'll have to put the image itself in like this;
<View>
<Text> BIG TITLE </Text>
<Text><Text>Caption text bla bla ajdjj </Text><Image style={{width: 30, height: 30}} source={{uri: avatarurl}}/> More Text bla blah blah blah blahhhhh </Text>
</View>

React Native LayoutAnimation scrollView container size

I have some components wrapped in a ScrollView and for some condition the bottom most component shouldn't render. When that happens, I use LayoutAnimation to hide it. The problem is that when the component disappear, the ScrollView jumps to the new content height directly, without any animation at all.
I want to use LayoutAnimation since I have screens where the contents height is unknown.
Example image
If you look at the image, when the button is pressed, the screen will instantly jump to the blue box without any animation.
state = { showGreenBox: false };
renderBottomBox() {
if (this.state.showGreenBox) {
return (
<View style={{ height: 300, width: 100, backgroundColor: 'green' }} />
);
}
}
render() {
return (
<ScrollView>
<View style={{ height: 300, width: 100, backgroundColor: 'red' }} />
<View style={{ height: 300, width: 100, backgroundColor: 'blue' }} />
{this.renderBottomBox()}
<TouchableOpacity
onPress={() => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
this.setState({ showGreenBox: !this.state.showGreenBox });
}}
>
<Text>Press to collapse green box</Text>
</TouchableOpacity>
</ScrollView>
);
}

Nest inside <Image> but behind the image

The problem I'm having is that the image inside the image, is on top of the outside image, the outside image is a half transparent image so I want it to be ON TOP of the nested image, as an overlay image, what can I do?
<Image
style={it}
source={require('../../img/Rarities/red.png')}
>
<View>
{
item[2] != null ?
<View style={{ width: 15, height: 15, backgroundColor: item[2].Color }} />
: null
}
<Image
style={{height:'70%',width:'70%'}}
source={{ uri: item[1].base64 }}
/>
</View>
</Image>
I tried:
changing the zIndex of the images didn't work..
In React Native, components are rendered in the order they are defined - therefore it can be tricky to reverse the order and render a parent on top of a child.
Instead, you can render the images as siblings, and use a parent container component with a little position: absolute trickery to get the images to align on top of each other.
For the following view structure:
<View style={styles.imageContainer}>
<Image source={{uri: image1}} style={styles.bottom} />
<Image source={{uri: image2}} style={styles.top} />
</View>
You can achieve this effect with following styles. See inline comments for explanation:
const styles = StyleSheet.create({
// The container controls the top image size
imageContainer: {
width: 200,
height: 200
},
bottom: {
// horizontal margin: (100% -width) / 2
marginHorizontal: '15%',
// vertical margin: (100% - height) / 2
marginVertical: '15%',
width: '70%',
height: '70%'
},
top: {
// positioned absolutely
position: 'absolute',
opacity: 0.5,
// full width/height of imageContainer
width: '100%',
height: '100%'
},
});
You can see it in action in this Snack demo.
I'm not sure that i understand what are you meaning correctly but you can use the Image components like this if you want to have them inside each other:
<View>
<Image
style={{flex:1}}
source={require('./img/favicon.png')}
/>
<Image
style={{height:'30',width:'30', position:'absolute'}}
source={require('./img/favicon.png')}
/>
</View>
Note: the Image sources and the dimensions should be replaced.

Resources