打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
FLEX | Flex
userphoto

2012.05.29

关注
Flex - Create a Dashboard That Allows Drag and Resize
October 21st, 2009
NOTE: I recently discovered that the resize functionality of these widgets is poor on a Mac, so I'm working on an improved code sample.  Stay tuned...
Here's the very beginnings of a dashboard application that will allow you to drag widgets around and resize them.  This could be the beginning of a great project tutorial.  I've already got lots of ideas for adding on to this simple protoype.  It could also serve as a good introduction to object oriented programming concepts because the main class in the project serves as a base class for all widgets that are added to teh dashboard.  It's basically a Flex container that has a button labled 'M' (for move) and another one labeled 'R' (for resize).  So you could extend the WidgetBase class to make your own widgets and they will inherit the move and resize functionality from the base class.
Here's the application mxml file, which serves as the dashboard that contains our widgets:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:dashboard="dashboard.*"
layout="absolute" width="520" height="400"
borderColor="black"
backgroundColor="white"
borderStyle="solid"
borderThickness="1"
creationComplete="init()">
<mx:Script>
<![CDATA[
private function init():void{
var widget1:WidgetBase = new Widget1();
addChild(widget1);
var widget2:WidgetBase = new Widget2();
widget2.x = 400;
addChild(widget2);
}
]]>
</mx:Script>
</mx:Application>
Here's the base class, note that it's in a package named 'dashboard':
package dashboard
{
import flash.events.MouseEvent;
import mx.containers.Canvas;
import mx.controls.LinkButton;
//I tried to make this an mxml component, rather than a class,
//but I got an error.
//here's a link to an explanation...
//http://www.munkiihouse.com/?p=37
public class WidgetBase extends Canvas
{
private var lbMove:LinkButton;
private var lbResize:LinkButton;
public function WidgetBase()
{
super();
//create the control to allow the widget to be moved...
lbMove = new LinkButton();
lbMove.label = "M";
lbMove.setConstraintValue("top",0);
lbMove.setConstraintValue("right",0);
lbMove.addEventListener(MouseEvent.MOUSE_DOWN,startMove);
lbMove.addEventListener(MouseEvent.MOUSE_UP,stopMove);
addChild(lbMove);
//create the control to allow the widget to be resized..
lbResize = new LinkButton();
lbResize.label = "R";
lbResize.setConstraintValue("bottom",0);
lbResize.setConstraintValue("right",0);
lbResize.addEventListener(MouseEvent.MOUSE_DOWN,                                                                             startResize);
lbResize.addEventListener(MouseEvent.MOUSE_OUT, stopResize);
lbResize.addEventListener(MouseEvent.CLICK, stopResize);
addChild(lbResize);
//
this.addEventListener(MouseEvent.MOUSE_MOVE,                                                                    checkMovePosition);
}
private function startMove(evt:MouseEvent):void{
//note: startDrag() and stopDrag() are methods of Sprite,                         //which is a super class of Canvas
this.startDrag();
}
private function stopMove(evt:MouseEvent):void{
this.stopDrag();
}
private function startResize(evt:MouseEvent):void{
lbResize.addEventListener(MouseEvent.MOUSE_MOVE,resize);
}
private function stopResize(evt:MouseEvent):void{
lbResize.removeEventListener(MouseEvent.MOUSE_MOVE,resize);
}
private var prevX:Number = 0;
private var prevY:Number = 0;
private function resize(evt:MouseEvent):void{
if(prevX > 0 && prevY > 0){
this.width += (mouseX - prevX);
this.height += (mouseY - prevY);
}
prevX = mouseX;
prevY = mouseY;
}
private function checkMovePosition(evt:MouseEvent):void{
if(this.x < -10 || this.y < -10){
this.stopDrag();
this.x = 0;
this.y = 0;
}
}
}
}
Here's the first incarnation of a widget:
<?xml version="1.0" encoding="utf-8"?>
<WidgetBase xmlns="dashboard.*" xmlns:mx="http://www.adobe.com/2006/mxml"
width="300" height="300"
backgroundColor="white"
borderStyle="solid"
borderColor="black"
borderThickness="4" >
<mx:Label text="Widget1" />
</WidgetBase>
As I mentioned earlier, I've got lots of ideas for exanding on this prototype, so if you find this sample code useful, check back for updates in the future.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
自己写的模拟combobox的flex4实现,带分页,支持键盘操作
flash常用代码!(精) - flash源文件 - Flash教程与作品 - 水世界素材...
flash as3.0制作流式视频(flv)播放器的方法_Flash_紫苹果电脑网
as3加载 外部txt文件
纯AS3.0代码写的一个简易音乐播放器
Flash as3 一种减速运动的算法(有惯性的小球)用作惯性菜单
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服