魏晋遗疯

人不能选择出身,所以不能忘记过去。我是白陀山新一代疯子,所以注定我是个烂人
数据加载中……
Flex图片浏览器,可以很黄、很暴力
  最近一个星期准备专门学一下Flex编程,昨天捣鼓了一个图片浏览器,下面是效果图:

上图是为了提醒大家一定要注意预防乳腺癌,借以体现我深刻的人文主义情怀。

 

作用是浏览本机的图片,而且获得本机文件夹中所有的图片的方法并不是通过flash本身获得,因为安全的原因,目前我尚不知道有方法遍历客户端的文件夹。由于Flex生成的最终程序是swf格式的,所以既可以在网页上使用,也可以用Flash Player播放,所以单纯来讲,这也挺有意思。刚才说到,我不能用flash获得本地所有文件,所以我采用的方法是用.net写一个WebServices,然后flash调用这个方法得到传入文件夹路径中的所有的图片文件的路径,最后在flash中显示出图片。用它来观看自己电脑里的人体艺术图片,难道不是一件很有成就很黄很暴力的事情么?

架构分为两部分,您可以借此了解到Flex与其他编程语言结合的方法

1.       WebServices

2.       Flash前台页面

下面先说一下WebServices的代码:
namespace FlexWebServices
{
    
/// <summary>
    
/// Summary description for Service1
    
/// </summary>

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false)]
    
public class FlexImageBrowser : System.Web.Services.WebService
    
{
        [WebMethod]
        
public string GetFileList(string path)
        
{
            
try
            
{
                DirectoryInfo imageFolder 
= new DirectoryInfo(path);
                
//得到路径下所有的jpg图片文件
                FileInfo[] fils = imageFolder.GetFiles("*.jpg");

                StringBuilder strReturn 
= new StringBuilder();;
                
foreach (FileInfo file in fils)
                
{
                    
//用\n分隔各图片路径
                    strReturn.Append(file.FullName + "\n"); 
                }


                
return strReturn.ToString();
            }

            
catch (Exception e)
            
{
                
//如果产生异常,则返回空字符串
                
//一般说来,产生异常的情况是路径错误
                return "";
            }

        }

    }

}
 

下面是Flex前台的代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application applicationComplete="onAppInit()" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" borderColor="#ff8080" backgroundGradientColors="[#c0c0c0, #ffffff]" height="100%" width="100%">
    
<mx:Script>
        
<![CDATA[
            import mx.controls.List;
        
            import mx.events.ItemClickEvent;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            
            private var imageFolderPath:String = "F:\\Pic\\NoThinking\\firl\\";
            private var stringArray:Array;
            private var currentIndex:int = -1; 
            internal function onButtonClick(event:ItemClickEvent):void
            {
                switch (event.index)
                {
                    //当路径改变时,点击刷新重新获得路径下所有图片列表
                    case 0:
                        imageFolderPath = TextInputPath.text;
                        //调用webservices
                        ws.GetFileList(imageFolderPath);
                        break;
                    //第一张图片
                    case 1:
                        if (stringArray.length > 0 )
                        {
                            ImageShowingPic.source = stringArray[0];
                            currentIndex = 0;
                        }
                        break;
                    //上一张图片
                    case 2:
                        if (currentIndex - 1 >= 0 )
                        {
                            ImageShowingPic.source = stringArray[currentIndex - 1];
                            currentIndex -= 1;
                        }
                        else
                        {
                            Alert.show("Be the first image");
                        }
                        break;
                    //下一张图片
                    case 3:
                        if (currentIndex + 1 <=  stringArray.length -1)
                        {
                            ImageShowingPic.source = stringArray[currentIndex + 1];
                            currentIndex += 1;
                        }
                        else
                        {
                            Alert.show("Be the Last image");
                        }
                        break;
                    //最后一张图片
                    case 4:
                        if (stringArray.length > 0)
                        {
                            ImageShowingPic.source = stringArray[stringArray.length - 1];
                            currentIndex = stringArray.length - 1;
                        }
                        else
                        {
                            Alert.show("This Path has no jpg image.");
                        }
                        break;
                    default:
                    break;
                }
            }
            
            //当webservices返回时,会调用此方法
            internal function resultHandle(event:ResultEvent):void
            {
                var stringList:String = event.result.toString();
                //webservices中是以\n为分隔符将每个图片路径分隔
                stringArray = stringList.split("\n");
                
                //下面循环是去掉为空或为\n的无效路径
                 for (var i:int = 0; i <stringArray.length; i++)
                {
                    if (stringArray[i].toString() == "" || stringArray[i].toString() == "\n")
                    {
                        stringArray.splice(i, 1);
                    }
                } 
                
                //当返回的路径列表不为0时,立即显示第一个图片
                if (stringArray.length > 0)
                {
                    currentIndex = 0;
                    ImageShowingPic.source = stringArray[0];
                }
            }
            
            //程序初始化会调用此方法
            internal function onAppInit():void
            {
                ws.GetFileList.send();
            }
            
        
]]>
    
</mx:Script>
    
<mx:WebService id="ws"
         wsdl
="http://localhost/FlexWebServices/FlexImageBrowser.asmx?wsdl"
         result
="resultHandle(event)" showBusyCursor="true">
         
<mx:operation name="GetFileList">
             
<mx:request>
                 
<path>{TextInputPath.text}</path>
             
</mx:request>
         
</mx:operation>
    
</mx:WebService>            
    
    
<mx:ButtonBar width="343" fontSize="12" horizontalCenter="138" bottom="10" itemClick="onButtonClick(event)">
    
<mx:Array>
        
<mx:String>刷新</mx:String>
        
<mx:String>第一张</mx:String>
        
<mx:String>上一张</mx:String>
        
<mx:String>下一张</mx:String>
        
<mx:String>最后一张</mx:String>
    
</mx:Array>
    
</mx:ButtonBar>
    
<mx:Image id="ImageShowingPic"   right="10" left="10" top="10" bottom="40" source="file:///C|/Documents and Settings/hunq/Desktop/E[1].jpg"/>
    
<mx:TextInput id="TextInputPath" horizontalCenter="-121.5" bottom="10" text="F:\pic"/>
    
<mx:Label text="在这里输入路径:" fontSize="12" horizontalCenter="-260" bottom="10"/>
</mx:Application>
 

代码中已经有注释了,以后还要不断完善代码,古语曰:以战养兵。我这是以实例来学新东西,欢迎您能跟我讨论切磋共同学习,如果有更黄,更暴力的想法,也同样欢迎您赐教。

 

代码中已经有注释了,以后还要不断完善代码,古语曰:以战养兵。我这是以实例来学新东西,欢迎您能跟我讨论切磋共同学习,如果有更黄,更暴力的想法,也同样欢迎您赐教。

posted on 2008-02-02 21:40 养猪防老 阅读(620) 评论(0)  编辑 收藏 网摘


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-02-02 22:09 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: