Spark Tile layout - get renderer from point - apache-flex

is there an built in method in spark tile list to get an item from a given point?
Thanks

As #www.Flextras.com suggested, looking at the source code can be useful. TileLayout has a method named getElementNearestScrollPosition() that will give you the index of the element that is located closest to a Point that you specify. This method is hidden in Flex's mx_internal namespace, however, so it gets excluded from the ASDocs.
Here's a simple example that seems to be working. I'm not showing the itemRenderer.
Main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*">
<fx:Script>
<![CDATA[
import mx.core.mx_internal;
use namespace mx_internal;
protected function list1_clickHandler(event:MouseEvent):void
{
var localPointInList:Point = list.globalToLocal(new Point(event.stageX, event.stageY));
trace(tileLayout.getElementNearestScrollPosition(localPointInList));
}
]]>
</fx:Script>
<fx:Declarations>
<s:ArrayCollection id="foo">
<fx:Object date="Jan 1, 2012"/>
<fx:Object date="Jan 1, 2013"/>
<fx:Object date="Jan 1, 2014"/>
</s:ArrayCollection>
</fx:Declarations>
<s:layout>
<s:HorizontalLayout />
</s:layout>
<s:Button label="I'm a button"/>
<s:List id="list" dataProvider="{foo}" itemRenderer="TestRenderer" click="list1_clickHandler(event)">
<s:layout>
<s:TileLayout id="tileLayout" requestedColumnCount="2" />
</s:layout>
</s:List>
</s:Application>
Note that I'm doing some conversion between local and global coordinates. You need to do this convert the global coordinates in the MouseEvent to the coordinate space of the List. I've also added a Button to the main app, solely to test this conversion ... the presence of the button makes it so the list's local coordinates do not match the global coordinates. If the list was placed at the origin (0,0) this conversion between coordinate spaces would not be necessary ... but that rarely happens in real world apps.

Related

How to change labels in a ButtonBar - simple test case and screenshot attached

In a Flex Mobile app I would like to have a ButtonBar at the bottom and I need to change the labels of its buttons:
(I don't want to use TabbedViewNavigatorApplication here, because all my logic is in a single View with an ActionBar at its top and ButtonBar at its bottom).
So I have prepared a very simple test case to demonstrate my problem.
Please just create a "blank" Flex mobile project in Flash Builder and put my 2 files (below) into it and you will see my problem - touching the buttons at the bottom doesn't change the label of the very right button).
TestAC.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationDPI="160">
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
import spark.skins.mobile.TabbedViewNavigatorTabBarSkin;
private function handleTabs(event:IndexChangeEvent):void {
trace(_tabBar.selectedIndex);
_tabs[2].label = String(1 + _tabBar.selectedIndex);
}
]]>
</fx:Script>
<fx:Declarations>
<s:MultiDPIBitmapSource id="CHAT"
source160dpi="#Embed('chat.png')"
source240dpi="#Embed('chat.png')"
source320dpi="#Embed('chat.png')" />
<s:ArrayCollection id="_tabs">
<fx:Object label="One" />
<fx:Object label="Two" />
<fx:Object label="Three" icon="{CHAT}" />
</s:ArrayCollection>
</fx:Declarations>
<s:ButtonBar id="_tabBar"
requireSelection="true"
width="100%"
bottom="0"
skinClass="spark.skins.mobile.TabbedViewNavigatorTabBarSkin"
dataProvider="{_tabs}"
change="handleTabs(event)">
</s:ButtonBar>
</s:Application>
chat.png:
Allright, adding _tabs.refresh(); to handleTabs has helped.

Dragging in spark List does not work in Flex mobile app? Test case and screenshot attached

I would like to reorder items of a List in a Flex mobile app by dragging them around with a finger.
As a first step I have copied the example from Adobe document Using drag-and-drop with list-based controls - but while their example works fine as web application, nothing happens in the mobile app below:
Why doesn't it work (like is some skin missing in the mobile theme?)
Is there a way to make it work (at least reordering items in a mobile List)?
Below is the simple test code I've tried - just put it into a new blank (i.e. without a nav. bar) Flex mobile project in Flash Builder:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationDPI="160"
creationComplete="initApp()">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private function initApp():void {
srclist.dataProvider =
new ArrayCollection(['Reading', 'Television', 'Movies']);
destlist.dataProvider = new ArrayCollection([]);
}
]]>
</fx:Script>
<s:HGroup>
<s:VGroup>
<s:Label text="Available Activities"/>
<s:List id="srclist"
allowMultipleSelection="true"
dragEnabled="true"
dragMoveEnabled="true"/>
</s:VGroup>
<s:VGroup>
<s:Label text="Activities I Like"/>
<s:List id="destlist"
dropEnabled="true"/>
</s:VGroup>
</s:HGroup>
<s:Button id="b1"
label="Reset"
click="initApp();"/>
</s:Application>
I have found and tried by myself a super workaround on this page.
Just copy classes from his example and add the custom itemRenderer to your source list.
<s:List id="srclist"
allowMultipleSelection="true"
dragEnabled="true"
dragMoveEnabled="true">
<s:itemRenderer>
<fx:Component>
<local:DraggableIconItemRenderer decorator="{DragThumb}" />
</fx:Component>
</s:itemRenderer>
</s:List>
Respect to the author!
Here is the result:

Flex mxml custom component - how to add uicomponents?

how to I pass in a uicomponent to a mxml custom component?
ex: I want to pass in any number of buttons, and I want my custom component to lay them out in a certain order.
MainApp.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" xmlns:local="*"
>
<local:myComp >
<s:Button label='Button1' />
<s:Button label='Button2' />
<!--I want to add anything else here -->
</local:myComp>
myComp.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()"
width="400" height="300"
>
<fx:Script>
<![CDATA[
private function init():void {
// how do I access the added components and place them where I want?
// do I use removeChildren and AddChildren?
// addItemsHere.addchild(nextButton);
]]>
</fx:Script>
<s:HGroup id='addItemsHere' />
As your component extends Group, you shall use addElement instead of addChild (and for all other methods with 'child' in their name it shall be replaced with 'element'. So, access to all elements will be like that:
for(var i:int =0; i < numElements; i++){
var button:Button = Button(getElementAt(i));
doWhatIWantWithMyButton(button);
}
It is also better to override createChildren method of your component if you know what to add at the creation moment.
If you don't need very specific button placement, you can set layout property of your component to any desired layout (like VerticalLayout, for example), and those layouts are tunable.
You seem to be trying to recreate functionality that already exists in the SDK. This is wat SkinnableContainer is for.
Depending on your use case, there are two ways to use it:
You only need to add some custom graphic elements to you custom component, but no additional behaviour
In this scenario, you would simple reuse SkinnableContainer and assign it a custom skin, like so:
<s:SkinnableContainer skinClass="MySkin">
<s:Button label='Button1' />
<s:Button label='Button2' />
</s:SkinnableContainer>
With MySkin.mxml perhaps something like:
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Metadata>
[HostComponent("spark.components.SkinnableContainer")]
</fx:Metadata>
<s:states>
<s:State name="normal" />
<s:State name="disabled" />
</s:states>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label text="I'm a SkinnableContainer"/>
<s:Group id="contentGroup" width="100%" height="100%">
</s:SkinnableContainer>
Your Buttons will now automatically be added to the contentGroup; the SkinnableContainer class handles this for you. Note that this Group must have exactly that id; it's a required SkinPart.
You want to add some behaviour to you component
The procedure is the same, but you would now subclass SkinnableContainer (this is usually done in pure ActionScript), write some behaviour in there, and assign the skin to an instance of this class.
public class MyComp extends SkinnableContainer {
//additional behaviour goes here
}
Usage:
<local:MyComp skinClass="MySkin">
<s:Button label='Button1' />
<s:Button label='Button2' />
</local:MyComp>

Taking a snapshot, scaling it and repeating

I'm trying to scale text from 1 to 5, a sort of zoom in type of look. I'm using the code below:
<s:Scale target="myLabel" autoCenterTransform="true"
duration="2000"
scaleYFrom="1" scaleYTo="5" scaleXFrom="1" scaleXTo="5"
>
The text scales terribly. So my goal is to take a snapshot of the text label at a large font, add it to the display list, set the scale to a 5th and then animate the scale up to 1. After that I have to set the text again and do it all over again.
This is a Flex app and that's where my question comes in. I don't know how to take a snapshot, then what to add it to (group?) and then how to erase it and start over again? Any advice in any of this would be appreciated.
Here's a very simple approach which animates the fontSize style, instead of animating the scale properties:
Depending on what you're doing this may not perform well b/c it is setting a new style so frequently. Also, if you play this animation slowly, it looks like the individual characters "wobble" (they move or shake a teeny bit).
I'll post something on taking a snapshot and scaling that next...
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">
<fx:Declarations>
<s:Animate id="scaler" target="{scaledText}" >
<s:MotionPath property="fontSize">
<s:Keyframe time="0" value="12"/>
<s:Keyframe time="1500" value="48"/>
</s:MotionPath>
</s:Animate>
</fx:Declarations>
<fx:Script>
<![CDATA[
protected function onLabelClick():void
{
scaler.play();
}
]]>
</fx:Script>
<s:Label id="scaledText" text="this is some text" click="onLabelClick()" />
</s:Application>
And here is the approach that takes a snapshot of the text. This has the same problem as your original solution, the scaled text is very jagged. But this does show how to take a snapshot of something and use the snapshot in another Flex object:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">
<fx:Declarations>
<s:Scale id="scaler" target="{drawingTarget}" scaleXFrom="1" scaleXTo="5" scaleYFrom="1" scaleYTo="5"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import spark.core.SpriteVisualElement;
protected function onLabelClick():void
{
var data:BitmapData = new BitmapData(200,200,true, 0xFFFFFF);
data.draw(scaledText);
var g:Graphics = drawingTarget.graphics;
g.beginBitmapFill(data);
g.drawRect(0,0, 200,200);
g.endFill();
}
protected function onBitmapClick(event:MouseEvent):void
{
scaler.play();
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label id="scaledText" text="this is some text" click="onLabelClick()" />
<s:SpriteVisualElement id="drawingTarget" width="200" height="200" click="onBitmapClick(event)"/>
</s:Application>

How to take screenshots of a Flex Spark VideoDisplay?

I want to build a component, where the user can play a video in a Flex Spark VideoDisplay. There will be a button and whenever the button is pressed, I want to save the current time of the VideoDisplay plus a screenshot. The screenshot needs to be someway saved, because I want to display all times and screenshots in a DataGrid (screenshots should appear when the user hovers a time in the DataGrid).
So, how can I take screenshots of the Spark VideoDisplay and save/display them?
You can take snapshots a few ways, this way just uses the ImageSnapshot class, but you could do it by manually drawing the bitmap of the video display if you'd like. Here's a sample:
Renderer
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer
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="100" height="100" creationComplete="trace(data)">
<mx:Image source="{this.data}" width="100%" height="100%"/>
</s:ItemRenderer>
Sample App
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.graphics.ImageSnapshot;
public function takeSnapshot():void
{
var snapshot:BitmapData = ImageSnapshot.captureBitmapData(videoDisplay);
var bitmap:Bitmap = new Bitmap(snapshot);
list.dataProvider.addItem(bitmap);
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout horizontalAlign="center"/>
</s:layout>
<s:VideoDisplay id="videoDisplay"
source="video.mov"
width="400" height="300"/>
<s:Button id="button" click="takeSnapshot()"/>
<s:List id="list" horizontalCenter="0" width="100%" itemRenderer="SnapshotRenderer">
<s:dataProvider>
<mx:ArrayList/>
</s:dataProvider>
<s:layout>
<s:TileLayout/>
</s:layout>
</s:List>
</s:Application>
To accomplish exactly what you were describing (take snapshot and save snapshot), you could store those in an array in that takeSnapshot method, or you could loop through the list.dataProvider do get the bitmaps. Then you'd just need to pass them to a backend language (ruby, python, php...) to save.
Hope that helps,
Lance
use the JPEGEncoder in flex to convert it to bytearray, and then encode the byte array using the b64encoder as follow:
var jpg:JPEGEncoder = new JPEGEncoder();
var ba:ByteArray = jpg.encode(bitmapData);
var b64encoder:Base64Encoder = new Base64Encoder();
b64encoder.encodeBytes(ba);
var b64String:String = b64encoder.flush();
Now you can pass your b64String to your server via HTTP post method :)

Resources