简体   繁体   English

在as3中加载时图像平滑

[英]Smooth image on load in as3

I am new here and a complete noob when it comes to as3. 我在这里是新手,而对于as3则是一个完全菜鸟。 Somehow I have managed to put this together with some help from different places. 不知何故,我设法在不同地方提供了一些帮助。 And now I turn to you guys :) 现在我转向你们:)

I need to put smoothing on my images and thumbs that Im loading from an XML file. 我需要对我从XML文件加载的图像和拇指进行平滑处理。 I have tried a lot of things but can't get any of it to work and I get this error: Scene 1, Layer 'as3', Frame 1, Line 27 1120: Access of undefined property e. 我已经尝试了很多方法,但是无法使它正常工作,并且出现以下错误:场景1,图层“ as3”,第1帧,第27行1120:访问未定义的属性e。 -> So I know var bitmapContent:Bitmap = Bitmap( e.target.content ); ->所以我知道var bitmapContent:Bitmap = Bitmap( e.target.content ); is the problem. 是问题。 but I have no idea what to use instead of e. 但我不知道用什么代替e。 I this i what I have so far: 我这就是我到目前为止所拥有的:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.None;
import flash.display.Bitmap;


// Loads the first image//
var i =new Loader();
i.load(new URLRequest("images/1.jpg"));
mainLoader.addChild(i)


//Loads the XML file//
var picsXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE ,  xmlLoaded);
xmlLoader.load(new URLRequest("imagelist.xml"));


//Loads images into thumbs//
function xmlLoaded(event:Event):void{
    picsXML = new XML(xmlLoader.data);
    //trace(picsXML);
    var bitmapContent:Bitmap = Bitmap( e.target.content );
    bitmapContent.smoothing = true;
    var thumbLoader:UILoader;
    for (var i:uint=0; i<picsXML.image.length(); i++)

    {
        thumbLoader=UILoader(getChildByName("thumb"+i));
        thumbLoader.load(new URLRequest("thumbs/"+picsXML.image[i].@file));
        thumbLoader.buttonMode = true;
        thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
        thumbLoader.addEventListener(MouseEvent.CLICK, tester); 
    }
}

//Loads large image when thumb is clicked//
function thumbClicked(event:MouseEvent){
    //var bitmapImage:Bitmap = event.target.content;
    //bitmapImage.smoothing = true;
    var thumbName:String = event.currentTarget.name;
    var thumbIndex:uint = uint(thumbName.substr(5));
    var fullPath:String = "images/"+picsXML.image[thumbIndex].@file;
    mainLoader.load(new URLRequest(fullPath));
    var myTween:Tween = new Tween(mainLoader,"alpha", None.easeNone, .3,1,18,false);

}

//Removes the first image when thumbs is clicked//
function tester(event:MouseEvent){
    if (mainLoader.contains(i)) {
        trace("hej")
     mainLoader.removeChild(i);
    }
    }

If i get your code true problem is here: u import an external xml file which is probably contain your url's and name of pic's. 如果我得到您的代码,真正的问题在这里:u导入一个外部xml文件,该文件可能包含您的url和pic的名称。 U can't turn ur xml file into a bitmap. 您无法将您的xml文件转换为位图。

If u want smoothing on your pictures u need to it after load ur pics or thumb's. 如果您想对图片进行平滑处理,则需要在加载图片或拇指后再进行平滑处理。 Probably ur problem will fix like this :) Hope this will help 您的问题可能会像这样解决:)希望这会有所帮助

Functions with arguments work like this: function Name ( input arg Name : input arg Type ) 具有参数的函数的工作方式如下: function Name输入arg名称input arg Type

Firstly, your function xmlLoaded(event:Event) cannot have an input called event but then you try to do a Bitmap( e.target.content ); 首先,您的函数xmlLoaded(event:Event)不能有一个名为event的输入,但是您尝试做一个Bitmap( e.target.content ); so for that bitmap line to work you'd have to change your input name to e like so... xmlLoaded( e : Event ); 因此,要使该位图线正常工作,您必须将输入名称更改为e,如下所示: xmlLoaded( e : Event );

Secondly, var bitmapContent:Bitmap = Bitmap( e.target.content ); 其次, var bitmapContent:Bitmap = Bitmap( e.target.content ); is incorrect. 是不正确的。

This is more correct var bitmapContent:Bitmap = img_Bytes_Loader.content as Bitmap; 这是更正确的var bitmapContent:Bitmap = img_Bytes_Loader.content as Bitmap; set a Loader 's content as Bitmap not XML content (text) as Bitmap (pixels). Loader的内容设置为位图,而不是XML内容(文本)设置为位图(像素)。

Anyways assuming your XML file looks like this: 无论如何,假设您的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<Items>

    <mp3>
        <url> track1.mp3 </url>     
        <image> image1.jpg </image>
    </mp3>

    <mp3>
        <url> track2.mp3 </url>     
        <image> image2.jpg </image>
    </mp3>

</Items>

Then your code should look something like this below : 然后,您的代码应如下所示:

var imgLoader_XML : URLRequest;
var picLoader : Loader = new Loader();

function xmlLoaded(event:Event):void
{
    picsXML = new XML(xmlLoader.data);
    //trace(picsXML);

    imgLoader_XML = new URLRequest( picsXML.mp3[ index ].image ); //[index] could be replaced with [i] if FOR loop
    picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, picloadComplete);
    picLoader.load (imgLoader_2);
}

public function picloadComplete(evt:Event)
{
    yourContainer.addChild(picLoader);
    yourContainer.width = 100;
    yourContainer.height = 100;
    yourContainer.alpha = 1;

}

Thanks for the inputs guys. 感谢您的投入。 It is greatly appreciated. 非常感谢。 I figured it out. 我想到了。 You were right, of course I can't put smoothing on the UILoader, so I had to target the content of the UILoader and run thumbLoader.addEventListener(Event.COMPLETE, smoothing); 您是对的,当然我不能对UILoader进行平滑处理,因此我必须定位UILoader的内容并运行thumbLoader.addEventListener(Event.COMPLETE,smoothing);。 each time an image is importet. 每次导入图像时。

Here is the entire working code (maybe it can help someone else :) : 这是完整的工作代码(也许可以帮助其他人:):

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.None;
import flash.display.Bitmap;
import flash.display.Loader;


// Loads the first image//
var i =new Loader();
i.load(new URLRequest("images/1.jpg"));
mainLoader.addChild(i)


//Loads the XML file//
var picsXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE ,  xmlLoaded);
xmlLoader.load(new URLRequest("imagelist.xml"));


//Loads images into thumbs//
function xmlLoaded(event:Event):void{
    picsXML = new XML(xmlLoader.data);
    var thumbLoader :UILoader = new UILoader(); 

    for (var i:uint=0; i<picsXML.image.length(); i++)

    {
        thumbLoader=UILoader(getChildByName("thumb"+i));
        thumbLoader.load(new URLRequest("thumbs/"+picsXML.image[i].@file));
        thumbLoader.buttonMode = true;
        thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
        thumbLoader.addEventListener(MouseEvent.CLICK, tester);
        thumbLoader.addEventListener(Event.COMPLETE, smoothing);
    }

}

function smoothing(e:Event):void{
            if(e.currentTarget.content is Bitmap)
            {
                Bitmap(e.currentTarget.content).smoothing = true;
            }
}

//Loads large image when thumb is clicked//
function thumbClicked(event:MouseEvent){

    var thumbName:String = event.currentTarget.name;
    var thumbIndex:uint = uint(thumbName.substr(5));
    var fullPath:String = "images/"+picsXML.image[thumbIndex].@file;
    mainLoader.load(new URLRequest(fullPath));
    mainLoader.addEventListener(Event.COMPLETE, smoothing);
    var myTween:Tween = new Tween(mainLoader,"alpha", None.easeNone, .3,1,18,false);


}

//Removes the first image when thumbs is clicked//
function tester(event:MouseEvent){
    if (mainLoader.contains(i)) {
        trace("hej")
     mainLoader.removeChild(i);
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM