动态加载的图像在某些部分看起来很糟糕 - 闪存as3

时间:2014-09-12 05:09:01

标签: actionscript-3 flash

我们在图块可视化工具应用程序中通过xml加载图像,图像在某些部分看起来不平滑,图块图像内部的线看起来呈锯齿状。这里是图像链接[1]:http://postimg.org/image/h4schoiub/“![在此输入图像描述] [1]

请注意,我不是flash动作专家

这是我的图像处理程序代码

package com.listplugin
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.utils.getDefinitionByName;


    //Use of class
    //l2.img.setSource("gear.png");
    //l2.img.setDimension(199,87,true);
    //l2.img.startLoad();

    /**
     * ...
     * @author Mike 2011
     */
    public class ImageHandler extends MovieClip
    {
        private var url:String;
        private var isExternal:Boolean;
        private var img;
        private var w:Number;
        private var h:Number;
        private var maintainRatio:Boolean;
        private var m_owner:MovieClip;
        private var m_func:MovieClip;
        private var center:Boolean;
        private var childs:Array;
        private var isSmooth:Boolean;
        public function ImageHandler()
        {

        }
        public function setSource(p_url:String)
        {
            isSmooth = false;
            if (childs)
            {
                for (var i = 0; i < childs.length; i++)
                {
                    removeChild(childs[i]);
                }
            }
            childs = new Array();
            center = true;
            url = p_url;
            isExternal = true;
            if (url.indexOf(".jpg") == -1 && url.indexOf(".gif") == -1 && url.indexOf(".png") == -1 && url.indexOf(".jpeg") == -1 && url.indexOf(".jpeg") == -1)
            {
                isExternal = false;
            }
        }
        public function startLoad()
        {
            if (isExternal == true)
            {
                loadExternalImage();
            }
            else
            {
                var ClassReference:Class = getDefinitionByName( url ) as Class;
                img = new ClassReference();             

                addImage();         
                if (m_func)
                    m_func.imageloaded();
            }
        }
        public function setDimension(p_width:Number, p_height:Number, p_maintainratio)
        {
            w = p_width;
            h = p_height;
            maintainRatio = p_maintainratio;
        }
        public function makeSmooth(p_flag:Boolean)
        {
            isSmooth = p_flag;
        }
        private function loadExternalImage()
        {
            var mLoader:Loader = new Loader();
            var mRequest:URLRequest = new URLRequest(url);

            mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
            mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
            mLoader.load(mRequest);

        }
        private function onCompleteHandler(loadEvent:Event)
        {
            img = loadEvent.currentTarget.content;

            addImage();

        }
        public function callback(p_owner:MovieClip)
        {           
            m_func = p_owner;
        }
        private function addImage()
        {
            if (w == 0)     w = img.width;
            if (h == 0)     h = img.height;

            if (maintainRatio == true)
            {
                var hgap = img.width - w;
                var vgap = img.height - h;
                if (img.height > img.width)
                {
                    img.height = h;
                    img.scaleX = img.scaleY;
                    if (img.width > w)
                    {
                        img.width = w;
                        img.scaleY = img.scaleX;
                    }

                }
                else
                {
                    img.width = w;
                    img.scaleY = img.scaleX;
                    if (img.height > h)
                    {
                        img.height = h;
                        img.scaleX = img.scaleY;
                    }

                }
                if (center==true)
                {
                    img.x = (w - img.width) / 2;
                    img.y = (h - img.height) / 2;
                }
            }
            else
            {
                img.width = w;
                img.height = h;
            }

            addChildAt(img, 0);
            childs.push(img);
            if (m_func)
                m_func.imageloaded();

        }
        public function makeCenter(p_flag:Boolean)
        {
            center = p_flag;
        }
        private function onProgressHandler(mProgress:ProgressEvent)
        {
            var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;

        }

    }   
}

1 个答案:

答案 0 :(得分:1)

那是因为你正在缩放图像。

Loader将返回一个Bitmap Object,它具有一个名为smoothing的属性。

基本上你应该这样做

var img:Bitmap;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.Complete, onComplete);
loader.load(new URLRequest(pathToTheImage));

function onComplete(e:Event):void{
    img = e.content as Bitmap;
    img.smoothing = true;
    //then scale it
}

查看Bitmap

的AS3文档