【问题标题】:In Flex, how do I create, populate, and display a bitmap?在 Flex 中,如何创建、填充和显示位图?
【发布时间】:2010-10-21 06:28:20
【问题描述】:

从一个完全空的 MXML 应用程序开始:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" >

</mx:Application>

什么是脚本:

  • 创建位图(这应该是BitmapData 对象吗?)
  • 以编程方式用数据填充它(例如画一个圆圈)(有没有比重复调用 BitmapData.SetPixel 更有效的方法?)
  • 在应用程序中渲染它(例如在中间居中)(如何将位图显示到屏幕上?在线 Flash 示例显示调用myBitmap=new Bitmap(myBitmapData),然后调用myBitmap.AddChild,但这会在 Flex 中引发错误)。

??

注意:目标不仅仅是在屏幕上显示一个圆圈,而是使用 SetPixel(或 ???)填充 Bitmap(或 BitmapData?或 ???)对象,然后将其内容显示到屏幕上,就像在图像处理或视觉效果应用程序中需要做的那样。

【问题讨论】:

    标签: actionscript-3 apache-flex bitmap flex3


    【解决方案1】:

    您有几个选择。首先是检查Degrafa framework,它有一些非常棒的绘图工具,否则你需要添加一个脚本块并在函数中使用AS3绘图api(在这种情况下可能会调用creationComplete:

    private function handleCreationComplete():void
    {
        var component:UIComponent = new UIComponent(); //needed to add to the flex display list
        var myCircle:Sprite = new Sprite();
        myCircle.graphics.beginFill(0xFFFFFF,1)
        myCircle.graphics.drawCircle(100,100,50);
        component.addChild(myCircle);
        this.addChild(component);
    }
    

    这不会使圆圈居中,但您可以弄清楚这一点。

    你可以使用这个函数从上面的 UIComponent 中真正得到位图:

    private function getBitmapData( target : UIComponent ) : BitmapData
       {
        var bd : BitmapData = new BitmapData( target.width, target.height );
        var m : Matrix = new Matrix();
        bd.draw( target, m );
        return bd;
       }
    

    来自here

    【讨论】:

    • 这会画一个圆,但它不是通过显式创建位图来实现的。我将编辑我的问题以使其更清楚。
    • 谢谢,这些额外的部分帮助很大。
    【解决方案2】:

    这是我根据 Joel Hooks 的建议创建的一个完整的工作示例:

    MXML 文件 (ards.mxml):

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
           creationComplete="onCreationComplete()">
    
    <mx:Script source="ards_script.as" />
    
    </mx:Application>
    

    脚本文件 (ards_script.as):

    import mx.core.UIComponent;
    
    private function onCreationComplete():void
    {
        // create a UIComponent and add it to the Flex display list
        var component:UIComponent = new UIComponent();
        component.width = this.width;
        component.height = this.height;
        component.x = 0;
        component.y = 0;
        this.addChild(component);
    
        // create a BitmapData object (basically an array of pixels) the same size as the screen    
        var bd : BitmapData = new BitmapData( component.width, component.height );
    
        // draw a green circle in the bitmap data
        var xCenter:int = component.width/2;
        var yCenter:int = component.height/2;
        var r:int = Math.min(xCenter,yCenter);
        var rSquare:int = r*r;
        var color:Number = 0x00ff00;
        for( var i:int=0; i<component.width; i++ )
        {
            for( var j:int=0; j<component.width; j++ )
            {
                var dX:int = i - xCenter;
                var dY:int = j - yCenter;
                var q:int = dX*dX + dY*dY;
                if( q < rSquare )
                {
                        bd.setPixel( i, j, color );
                }       
            }
        }
    
        // create a bitmap based on the data, and add it as a child to the UIComponent to be displayed
        // (if you try to add it directly to the Application, you get a runtime error)
        var bt:Bitmap = new Bitmap(bd);
        component.addChild(bt);    
    }
    

    【讨论】:

    • 哦,所以你可以实例化一个通用的 UIComponent 并直接向它添加一个位图(因为它是一个 DisplayObject)。不错!
    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多