Flex 实现可以拖动的毛玻璃效果

复制代码 代码如下:


width="100%" height="100%" horizontalAlign="center" verticalAlign="middle" backgroundColor="#FFFFFF"
creationComplete="onCreationComplete(event)" mouseUp="onMouseUp(event)" mouseMove="onMouseMove(event)">

clipContent="true" horizontalScrollPolicy="off" verticalScrollPolicy="off">
source="@Embed(source='images/bg.jpg')"
/>

borderColor="#000000" render="onBoxRender(event)"
x="200" y="200" height="200" width="400" id="ctlBox"
filters="{[new DropShadowFilter(5, 45, 0x000000,0.5)]}"
mouseDown="onMouseDown(event)" />


此场景对应的类为:
复制代码 代码如下:

// ActionScript file
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
private var m_mouseDown : Boolean = false;
private var m_offsetPt : Point;
private function onCreationComplete(evt:Event) : void
{
onBoxRender(null);
}
private function onBoxRender(evt:Event) : void
{
if( ctlContainer.width > 0 && ctlContainer.height > 0 )
{
var bmpData : BitmapData = new BitmapData( ctlContainer.width, ctlContainer.height, false);
var matrix : Matrix = new Matrix();

// 对矩阵进行平移变换
matrix.translate( -1 * ctlBox.x, -1 * ctlBox.y);

// 将背景填充到移动元件上来
bmpData.draw( ctlContainer
, matrix
, null
, null
, new Rectangle( 0, 0, ctlBox.width + 4, ctlBox.height + 4) // 裁剪区域
);
bmpData.applyFilter( bmpData
, new Rectangle( 0, 0, bmpData.width, bmpData.height)
, new Point( 0, 0)
, new BlurFilter( 5, 5, 5) // BlurFilter的参数越大计算量越大 移动的时候越卡
);
ctlBox.graphics.beginBitmapFill(bmpData, null, false, false);
ctlBox.graphics.drawRect( 0, 0, ctlBox.width, ctlBox.height);
ctlBox.graphics.endFill();
}
}
private function onMouseDown(evt:MouseEvent) : void
{
m_mouseDown = true;
m_offsetPt = new Point( ctlBox.mouseX, ctlBox.mouseY);
}
private function onMouseUp(evt:MouseEvent) : void
{
m_mouseDown = false;
evt.updateAfterEvent();
}
private function onMouseMove(evt:MouseEvent) : void
{
if( m_mouseDown )
{
ctlBox.x = evt.stageX - m_offsetPt.x;
ctlBox.y = evt.stageY - m_offsetPt.y;
evt.updateAfterEvent();
}
}
// ActionScript file
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
private var m_mouseDown : Boolean = false;
private var m_offsetPt : Point;
private function onCreationComplete(evt:Event) : void
{
    onBoxRender(null);
}
private function onBoxRender(evt:Event) : void
{
    if( ctlContainer.width > 0 && ctlContainer.height > 0 )
    {
        var bmpData : BitmapData = new BitmapData( ctlContainer.width, ctlContainer.height, false);
        var matrix : Matrix = new Matrix();

        // 对矩阵进行平移变换
        matrix.translate( -1 * ctlBox.x, -1 * ctlBox.y);

        // 将背景填充到移动元件上来
        bmpData.draw( ctlContainer
        , matrix
        , null
        , null
        , new Rectangle( 0, 0, ctlBox.width + 4, ctlBox.height + 4) // 裁剪区域
        );
        bmpData.applyFilter( bmpData
            , new Rectangle( 0, 0, bmpData.width, bmpData.height)
            , new Point( 0, 0)
            , new BlurFilter( 5, 5, 5) // BlurFilter的参数越大计算量越大 移动的时候越卡
            );
        ctlBox.graphics.beginBitmapFill(bmpData, null, false, false);
        ctlBox.graphics.drawRect( 0, 0, ctlBox.width, ctlBox.height);
        ctlBox.graphics.endFill();
    }
}
private function onMouseDown(evt:MouseEvent) : void
{
    m_mouseDown = true;
    m_offsetPt = new Point( ctlBox.mouseX, ctlBox.mouseY);
}
private function onMouseUp(evt:MouseEvent) : void
{
    m_mouseDown = false;
    evt.updateAfterEvent();
}
private function onMouseMove(evt:MouseEvent) : void
{
    if( m_mouseDown )
    {
        ctlBox.x = evt.stageX - m_offsetPt.x;
        ctlBox.y = evt.stageY - m_offsetPt.y;
        evt.updateAfterEvent();
    }
}

对于Flex/Silverlight开发者来说很不方便。只能用第三方站点才能放Flash/Silverlight
Flex 实现可以拖动的毛玻璃效果_第1张图片

你可能感兴趣的:(Flex 实现可以拖动的毛玻璃效果)