AS 3 |克隆PNG图像数据 [英] AS 3 | Cloning Png image data

查看:141
本文介绍了AS 3 |克隆PNG图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要克隆.png格式从AS3服务器加载图像数据,从而使从服务器一次又一次地装入相同的数据,我不需要。

I need to clone .png image data loaded from server in AS3, so that I am not required to load same data again and again from server.

在上克隆图像数据在AS3搜索我能找到以下code在网上

After a search on cloning image data in AS3 I was able to find following code over internet

clone = new Bitmap(Bitmap(this._loader.content).bitmapData.clone() )

通过使用这种code,我可以克隆位图数据,但我现在面临的是,我的PNG背景是透明的问题。如果我强制转换装PNG数据为位图我的图标背景变为白色。

By using this code I am able to clone bitmap data but the problem which I am facing is that my png background is transparent. If I typecast loaded png data to bitmap my icons background becomes white.

任何帮助将是AP preciated。

Any help would be appreciated.

喜洛朗&放大器; TheDarkIn1978, 非常感谢快速恢复和您的宝贵建议,

Hi Laurent & TheDarkIn1978, Thanks a lot for quick revert back and your valuable suggestions,

让我把完整的画面在你们的面前,

Let me put complete picture in front of you guys,

我想从服务器,有几个地方同一个影片剪辑,以显示加载图标。

I am trying to load icons from server which has to be displayed at several places in same movieclip.

要显示我创建了一个影片剪辑中,我直接调用addChild()函数添加加载的内容作为其子的图像数据。

To display image data I have created a movieclip in which I directly add loaded content as its child by calling addChild() function.

现在,当我尝试添加的另一个影片剪辑作为它的鸡IL相同的图像数据出现问题,数据的新父被设置和 该图像由previously添加的内容删除。因此,在年底这个图标在影片剪辑上的一个位置只显示 (由于事实上,每个孩子可以有一个单亲)。

Now the problem occurs when I try to add same image data on another movieclip as its chil, new parent of data gets set and this image is removed from previously added content. So at the end this icon is displayed only at one position on movieclip (due to fact that each child can have a single parent).

所以我想克隆图像数据并显示在屏幕上, 当从服务器不断加载图像我打电话下面的函数保存的图像数据在本地,

Therefore I thought to clone image data and display it on screen, when ever Image from server is loaded I call following function to save image data in local,

   private var _bmapData : BitmapData;
    _bmapData = new BitmapData(_loader.width, _loader.height );
    _bmapData.draw( this );
    isImageLoaded=true;'

现在无论我必须表明这个数据,我用它来调用下面code克隆PNG数据

Now whereever I have to show this data , I use to call following code to clone png data

var dup : Image = new Image();
 dup.addChild(new Bitmap(_bmapData.clone()));
 return dup;

我beleive什么透光性好只支持PNG格式,而不是位图或JPEG,因此我的背景图标越来越白。

What I beleive transpareny is only supported in png format, not in bitmap or jpeg, therefore my background icons are getting white.

请帮助,因为我stucked和无法理解我的下一个步骤。

Kindly help as I am stucked and not able to understand my next steps.

推荐答案

公布c您的$ C $在您的更新几乎是在那里,你只需要在你的BitmapData调用一对夫妇更多的参数,以获得透明的工作:

The code you posted in your update is nearly there, you just need a couple more parameters in your BitmapData call to get the transparency working:

private var _bmapData : BitmapData;
_bmapData = new BitmapData(_loader.width, _loader.height, true, 0);
_bmapData.draw( this );

注意真,0 我增加。 是设置透明度为真,而 0 称无背景颜色应该由Flash添加。

Note the true, 0 I've added. true is setting transparency to true, and the 0 saying no background color should be added by Flash.

修改由于这已经获得了一些争论,我将详细阐述为什么这是必要的(为什么我的一些意见稍微不正确的)。

Edit Since this has garnered some debate, I will elaborate on why this is necessary (and why some of my comments were slightly incorrect).

首先,这里的<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData%28%29"相对=nofollow>为的BitmapData构造文档。如果你喜欢阅读这一点,那么这个答案的其余部分将是毫无意义的,因为在大多数情况下,我只是去重复。

First here's the documentation for the BitmapData constructor. If you fancy reading that, then the rest of this answer will be pointless, because for the most part I'm just going to repeat it.

所以,这两个BitmapData构造函数4个参数,宽度,高度,透明度和背景颜色。让我们忽略的宽度和高度,而直走上透明度,默认设置为true。美中不足的是,为了使BitmapData对象,以真正实现透明背景,它的背景颜色必须设置为黑色。

So, the BitmapData constructor takes 4 parameters, width, height, transparency and background color. Let's ignore width and height, and move straight onto transparency, which by default is set to true. The catch is that in order for the BitmapData object to actually implement a transparent background, it's background color must be set to black.

透明:布尔(默认值= true) - 指定位图图像是否支持每个像素的透明度。默认值为true(透明)。要创建完全透明的位图,请将transparent参数设置为true值,将fillColor参数为0x00000000的值(或为0)。

transparent:Boolean (default = true) — Specifies whether the bitmap image supports per-pixel transparency. The default value is true (transparent). To create a fully transparent bitmap, set the value of the transparent parameter to true and the value of the fillColor parameter to 0x00000000 (or to 0).

那么令人惊讶,即使的BitmapData设置为透明默认情况下,背景色默认为 0xFFFFFFFF的,其中prevents透明标志产生任何影响。这意味着,为了有一个正确thansparent背景BitmapData对象,您必须声明一个纯黑色的背景色。

So amazingly, even though BitmapData is set to transparent by default, the background color defaults to 0xFFFFFFFF, which prevents the transparent flag from having any effect. This means that in order to have a properly thansparent background to a BitmapData object, you must declare a pure black background color.

最后,如果你仍想证明,运行此享​​受:

Finally, if you still want proof, run this and enjoy:

package {

    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;

    public class Main extends MovieClip
    {
        private var bg          :Shape;

        private var bitmap1     :Bitmap;
        private var bitmap2     :Bitmap;

        public function Main():void
        {
            stage ? init() : addEventListener(Event.ADDED_TO_STAGE,init);
        }

        private function init(event:Event = null):void
        {
            if(event) removeEventListener(Event.ADDED_TO_STAGE,init);

            drawBackground();
            drawBitmaps();
        }

        private function drawBackground():void
        {
            //create a light colored background
            bg = addChild(new Shape()) as Shape;
            bg.graphics.beginFill(0xD0D0D0);
            bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
            bg.graphics.endFill();
        }

        private function drawBitmaps():void
        {
            //create a bitmap in the documented fashion
            var bmd1:BitmapData = new BitmapData(200,200,true);
            bitmap1 = addChild(new Bitmap(bmd1)) as Bitmap;
            //this time, give a background color
            var bmd2:BitmapData = new BitmapData(200,200,true,0x000000);
            bitmap2 = addChild(new Bitmap(bmd2)) as Bitmap;
            bitmap2.x = 200;
        }
    }
}

这篇关于AS 3 |克隆PNG图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆