how to replace the images in flex 3? - apache-flex

In my flex application, i am maintaining 5 images. When user clicks on 'next' button, it should display one image say 'image1'. If that button clicked again, then image1 should replace with image2 and so on. I am basically following 'image.visible' method. But images are displaying side by side. I think it is not the correct procedure. Any alternative? Thanks in advance
here is my code. I kept all my images and buttons in mx:panel. Even i used x and y positions which are not working.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Panel
title = 'Learn and Test your Knowledge'
height = '80%'
paddingBottom = '10' paddingTop = '10'
paddingLeft = '10' paddingRight = '10'
borderAlpha='0.20' fontFamily="Verdana" fontSize="15" color="#F30C32" backgroundImage="#Embed(source='../images/lad.jpg')" width="413" x="139">
<mx:Script>
<![CDATA[
public function nextEvent():void
{
// here i should write next button code
}
]]>
</mx:Script>
<mx:Image source="../images/image1.jpg" visible="true" id="image1" />
<mx:Image source="../images/image3.jpg" visible="true" id="image2"/>
<mx:Image source="../images/image3.jpg" visible="true" id="image3"/>
<mx:Button id="next" visible="false" click="nextEvent()">
</mx:Button>

The best way is to use only one image component if you are only ever showing one image. You can create an array or vector with the embedded images and reference that to change the source property on the image component. Here is an example: (the code below will work with any layout/container)
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Image id="image" click="imageClick()" source="{new images[0]()}" />
<mx:Script>
<![CDATA[
[Embed(source="assets/1.png")]
private var image1:Class;
[Embed(source="assets/2.png")]
private var image2:Class;
[Embed(source="assets/3.png")]
private var image3:Class;
private var images:Array = [image1, image2, image3];
private var imageIndex:uint = 0;
protected function imageClick():void
{
imageIndex++;
if(imageIndex == images.length) imageIndex = 0;
image.source = new images[imageIndex]();
}
]]>
</mx:Script>
</mx:Canvas>

Specify the x and y position of images as same and play around with their visibility.It ll definitely work.

ViewStack is my option it fits very well in this occasion.
At a time it shows only one component, for next action it will automatically override previous content by its new component.
<mx:ViewStack id="myViewStack" borderStyle="solid" width="100%" height="80%">
<mx:Canvas id="one" click="myViewStack.selectedChild=two;">
<mx:Image source="assets/1.png" />
</mx:Canvas>
<mx:Canvas id="two" click="myViewStack.selectedChild=three;">
<mx:Image source="assets/2.png" />
</mx:Canvas>
<mx:Canvas id="three" click="myViewStack.selectedChild=four;">
<mx:Image source="assets/3.png" />
</mx:Canvas>
<mx:Canvas id="four" click="myViewStack.selectedChild=five;">
<mx:Image source="assets/4.png" />
</mx:Canvas>
<mx:Canvas id="five" click="myViewStack.selectedChild=one;">
<mx:Image source="assets/5.png" />
</mx:Canvas>
</mx:ViewStack>

Related

Frustrated Animation Issue

I have spent 2 hours on this simple state to state transition with simply resize and move animation.... I can get the element to move and fade...but there is no resize animation at all. The panel element stay the same width whole time. If I copy the code to a brand new test mxml file, it works, but not in my main application file...I was wondering if someone here can help me out. Thanks a lot.
//This is a custom component inside the main application...Not sure if it relates to my issue.....
<mx:Canvas xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="1000" height="600" xmlns:getcomplist="services.getcomplist.*" xmlns:components="components.*"
currentState="defaultState">
<fx:Script>
<![CDATA[
protected function compList_changeHandler(event:IndexChangeEvent):void
{
currentState="whenClick";
}
]]>
</fx:Script>
<mx:states>
<s:State name="defaultState"/>
<s:State name="whenClick" />
</mx:states>
<mx:transitions>
<s:Transition toState="whenClick">
<s:Parallel target="{animationIsAnnoying}">
<!--<s:Fade duration="1000"/>--> // Fade is working fine...
<s:Resize heightFrom="600" duration="5000" /> //resize is not working
<s:Move xFrom="500" duration="5000" /> //Move is working fine
</s:Parallel>
</s:Transition>
</mx:transitions>
<s:HGroup>
<s:List id="compList"
width="280"
height="500"
change="compList_changeHandler(event)">
</s:List>
<s:Panel id="animationIsAnnoying" height="150" includeIn="whenClick" /> //The panel I want to animated...
<components:compDisplayDetail includeIn="whenClick" id="compDisplay" width="600" height="500" userPic="{userPicResult.lastResult}" dataFromClick ="{compDetailinfoResult.lastResult}" />
</s:HGroup>
</mx:Canvas>
You must have a click event to command the resize component
im using here mx:Resize component
<mx:Resize id="resizeThis" target="{toolBoxContainer}"/>
<mx:VBox id="toolBoxContainer" height="200" width="300" >
<mx:Textid="txt" text="HELLO"/>
</mx:VBox>
<mx:Button id="menuImage"toolTip="Show/Hidden Menu" click="menuClickHandler(event)" toggle="true"/>
<mx:Script>
<![CDATA[
private var toggle:int =1;
private function menuClickHandler(e:MouseEvent):void {
if(toggle == 0 ) {
//MoveThis.end();
resizeThis.widthFrom = 0
resizeThis.widthTo = 46;
resizeThis.play();
}
if(toggle == 1) {
resizeThis.end();
resizeThis.widthFrom = 46
resizeThis.widthTo = 0;
resizeThis.play();
}
if(toggle == 0) toggle = 1;
else toggle = 0;
}
]]>
</mx:Script>
This thing has errors in imports or the likes.. but i assume you got my point..

Flex: Double click event propagation on datagrid dependent on component order?

I'd like to have a double click event on a datagrid in Flex3. The following example only works if the Accordion (id = "mustBeSecond") container comes after the DataGrid. Why is the order of the components important and what can I do to prevent this behavior?
(The example does not work. If you change the order of "mustBeSecond" and "gridReportConversions" the example works fine)
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.ArrayCollection;
[Bindable] private var dp:ArrayCollection = new ArrayCollection([
{qty:1,referer:'http://google.com'},
{qty:25,referer:'http://cnn.com'},
{qty:4,referer:'http:stackoverflow.com'}]);
private function refererRowDoubleClicked(e:Event):void {
Alert.show("double click");
}
]]>
</mx:Script>
<mx:HBox width="100%" height="100%">
<mx:Accordion width="200" height="200" id="mustBeSecond">
<mx:Canvas label="Navigation Box" width="100%" height="100%">
<mx:VBox>
<mx:LinkButton label="First Link" />
<mx:LinkButton label="Second Link" />
</mx:VBox>
</mx:Canvas>
</mx:Accordion>
<mx:DataGrid id="gridReportConversions" height="100%" width="100%" dataProvider="{this.dp}"
mouseEnabled="true" doubleClickEnabled="true" itemDoubleClick="refererRowDoubleClicked(event)">
<mx:columns>
<mx:DataGridColumn width="75" dataField="qty" headerText="Qty" />
<mx:DataGridColumn dataField="referer" headerText="URL" />
</mx:columns>
</mx:DataGrid>
</mx:HBox>
I tested your code in Flex and it didn't make any difference which order they were in. The double click event fired either way. This was in a fresh project with no other code except the default stuff that a Flex application sets you up with.
Sometimes when a Flex project starts acting weird it helps to go to click Project -> Clean.
Do you get any errors or notices showing up in the Problems pane?

Flex show/hide on mouse over/out

i have 2 containers one above the other,
i need to show the second one when
there is a mouse over the first one
and
hide it when it goes out of the first one
also i want the second container content to be usable (mouse clicks/moves)
how can i do that?
Biroka has the right idea but there is another way. Put the containers in a viewstack and change the selectedChild on rollOver and rollOut. This should eliminate your flicker. Here's a fully functional sample
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
private function vsRollOver():void{
viewstack1.selectedChild = container2;
}
private function vsRollOut():void{
viewstack1.selectedChild = container1;
}
]]>
</mx:Script>
<mx:ViewStack x="10" y="10" id="viewstack1" width="200" height="200" rollOver="vsRollOver();" rollOut="vsRollOut();">
<mx:Canvas id="container1" label="View 1" width="100%" height="100%" backgroundColor="0x0000ff">
</mx:Canvas>
<mx:Canvas id="container2" label="View 2" width="100%" height="100%" backgroundColor="0xff0000">
</mx:Canvas>
</mx:ViewStack>
</mx:Application>
The second container has to be above the first one and:
private function init():void // call this on creationComplete event
{
container2.visible = false;
container1.addEventListener(MouseEvent.ROLL_OVER,overFunction);
container1.addEventListener(MouseEvent.ROLL_OUT,outFunction);
}
private function overFunction(e:MouseEvent):void
{
container2.visible = true;
}
private function outFunction(e:MouseEvent):void
{
container2.visible = false;
}
I would suggest that container1 should be 1-2 pixel larger than container2

Flex fixed and variable height - can it be set in markup?

I've got the following Flex application markup:
<app:MyApplicationClass
xmlns:app="*"
width="100%"
height="100%"
layout="vertical"
horizontalScrollPolicy="off"
verticalScrollPolicy="off">
<mx:VBox id="idPageContainer" width="100%" height="100%" verticalGap="0"
horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:HBox id="idTopContainer" width="100%" height="28" horizontalGap="2">
(top menu stuff goes here)
</mx:HBox>
<mx:HBox id="idBottomContainer" width="100%" height="100%" verticalScrollPolicy="off" clipContent="false">
(page stuff goes here)
</mx:HBox>
</mx:VBox>
</app:MyApplicationClass>
When I run it, it displays top panel with a fixed height, and a bottom panel with variable height. I expect the bottom panel's height to contain the remaining height, but it somehow overflows off-page.
The only way I found to fix this height issue (so far) is to programmatically set the height to be fixed instead of variable:
<mx:HBox id="idBottomContainer" width="100%" height="700" verticalScrollPolicy="off" clipContent="false">
(page stuff goes here)
</mx:HBox>
And code-behind:
package {
import mx.containers.HBox;
import mx.core.Application;
import mx.events.ResizeEvent;
// (...)
public class MyApplicationClass extends Application {
public var idBottomContainer:HBox;
// (...)
private function ON_CreationComplete (event:FlexEvent) : void {
// (...)
addEventListener(ResizeEvent.RESIZE, ON_Resize);
}
private function ON_Resize (event:Event) : void {
idBottomContainer.height = this.height - idTopContainer.height;
}
}
}
But this solution is too "dirty" and I'm looking for a more elegant way. Does anyone know an alternative?
Seems to me what you have ought to work. I have something similar working in countless screens. But if you're really stuck you could try to replace the VBox with a Canvas and do something like this:
<mx:Canvasid="idPageContainer" width="100%" height="100%" verticalGap="0"
horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:HBox id="idTopContainer" width="100%" height="28" horizontalGap="2">
(top menu stuff goes here)
</mx:HBox>
<mx:HBox id="idBottomContainer" width="100%" top="{idTopContainer.height}"
height="{idPageContainer.height - idTopContainer.height}"
verticalScrollPolicy="off" clipContent="false">
(page stuff goes here)
</mx:HBox>
</mx:Canvas>

Adding components to CoverFlow in Flex

I'm working with the CoverFlow library by Doug McCune.
In the example included, he has created some panels inside a CoverFlowContainer. There are other examples of him populating the coverflow from an RSS feed on the page linked above but he doesn't include the source for these :(
I will eventually be retrieving data from a web service to add to the coverflow however I'm starting with this simple example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center" verticalAlign="middle" xmlns:containers="com.dougmccune.containers.*" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.TextArea;
public var articlePanel:Panel = new Panel();
public var articleTextArea:TextArea = new TextArea();
private function init() : void
{
articlePanel.addChild(articleTextArea);
articlePanel.title = "test tile"
articleTextArea.text = "article1" + "\n" + "www.article.com" + "\n" + "hello this is article 1";
coverflow2.addChild(articlePanel);
}
]]>
</mx:Script>
<mx:VBox id="box" verticalGap="0" height="306" width="100%" maxWidth="600" maxHeight="300" >
<containers:CoverFlowContainer id="coverflow2" width="100%" height="244"
horizontalGap="40" borderStyle="inset" backgroundColor="0x000000"
segments="6" reflectionEnabled="true">
<mx:Panel id="testpanel" width="200" height="200" title="Mxml title">
<mx:DataGrid width="100%" height="100%">
<mx:columns>
<mx:DataGridColumn headerText="Column 1" dataField="col1"/>
<mx:DataGridColumn headerText="Column 2" dataField="col2"/>
<mx:DataGridColumn headerText="Column 3" dataField="col3"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
<mx:Panel id="buttonpanel" width="200" height="200" title="Mxml title">
<mx:Button id="myButton" label="Change title" click="buttonpanel.title = ('hello') "/>
</mx:Panel>
</containers:CoverFlowContainer>
</mx:VBox>
</mx:Application>
I've defined some panels in the mxml and set creationComplete=init() so that the new panel I've created in ActionScript will be added to the CoverFlow container.
When I launch the application it displays the two predefined panels but not the one I have created in ActionScript.
Any ideas? Is there a better way of doing this?
you can try mx:Repeater component
<mx:HTTPService id="srv" url="pics.xml"/>
<ns2:CarouselContainer id="cf" width="100%" height="303" horizontalGap="40"
segments="6" reflectionEnabled="true" angle="10" >
<mx:Repeater id="r" dataProvider="{srv.lastResult.images.image}">
<mx:Canvas width="200" height="200">
<mx:Image source="{r.currentItem.src}"/>
</mx:Canvas>
</mx:Repeater>
</ns2:CarouselContainer>
Fixed this by first defining the coverflow container with nothing in it:
<containers:CoverFlowContainer id="coverflow" width="100%" height="244"
horizontalGap="40" borderStyle="inset" backgroundColor="0x000000"
reflectionEnabled="true"/>
In the actionscript section I retrieve an array from a web service and for each item in the array I create a panel. It is important to give each panel a width and height otherwise it will only show one element in the coverflow or no elements at all:
articlePanel.width = 200;
articlePanel.height = 200;

Resources