在Flex4 Spark Application中设置图片背景解决方案汇总

问题:如何在 Flex4 Spark Application 中添加图片背景?

方案1:自定义含有BitmapGraphic的皮肤类,然后再MXML,CSS,AS中设置skinClass的皮肤样式

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

<s:Application name="Spark_Application_skinClass_test"

        xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        xmlns:mx="library://ns.adobe.com/flex/halo"

        skinClass="skins.CustomApplicationSkin">

    <s:layout>

        <s:BasicLayout/>

    </s:layout>

</s:Application>

自定义皮肤类, skins/CustomApplicationSkin.mxml代码如下:

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

<s:Skin name="CustomApplicationSkin"

        xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        alpha.disabled="0.5">

    <s:states>

        <s:State name="normal"/>

        <s:State name="disabled"/>

    </s:states>

    <fx:Metadata>

    <![CDATA[

        [HostComponent("spark.components.Application")]

    ]]>

    </fx:Metadata>

    <!-- fill -->

    <s:BitmapImage id="img"

            source="@Embed('image1.jpg')"

            smooth="true"

            left="0" right="0"

            top="0" bottom="0"/>

    <s:Group id="contentGroup" left="0" right="0" top="0" bottom="0" minWidth="0" minHeight="0"/>

</s:Skin>

你也可以通过外部.css样式表文件或者使用<Style/>标签来设置使用skinClass样式

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

<s:Application name="Spark_Application_skinClass_test"

        xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        xmlns:mx="library://ns.adobe.com/flex/halo">

    <s:layout>

        <s:BasicLayout/>

    </s:layout>

    <fx:Style>

        @namespace s "library://ns.adobe.com/flex/spark";

        s|Application {

            skinClass: ClassReference("skins.CustomApplicationSkin");

        }

    </fx:Style>

</s:Application>

或者你可以使用ActionScript来设置skinClass样式,代码如下:

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

<s:Application name="Spark_Application_skinClass_test"

        xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        xmlns:mx="library://ns.adobe.com/flex/halo"creationComplete=”init();”>

    <fx:Script>

        <![CDATA[

            import skins.CustomApplicationSkin;

            protected function init():void {

                setStyle("skinClass", CustomApplicationSkin);

            }

        ]]>

    </fx:Script>

</s:Application>

方案2在运行时embedded图片到BitmapFill对象中,修改Application皮肤的backgroundRect皮肤部分的填充属性。代码如下:

 

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

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

              xmlns:s="library://ns.adobe.com/flex/spark"

              xmlns:ho="library://ns.adobe.com/flex/halo"

              xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"creationComplete="init();">

    <fx:Script>

       <![CDATA[

           import mx.graphics.BitmapFill;

           import mx.graphics.BitmapFillMode;

           import spark.skins.spark.ApplicationSkin;

           [Embed("style/background.jpg")]

           protected const BgImg:Class;

           protected function init():void{

              var bmpFill : BitmapFill = new BitmapFill();

              bmpFill.source = BgImg;

              bmpFill.fillMode = BitmapFillMode.SCALE;

              ApplicationSkin(skin).backgroundRect.fill=bmpFill;

       ]]>

    </fx:Script>

</s:Application>

 

方案3扩展默认的Spark包 中Application 皮肤,并通过embedded图片来覆盖backgroundRect皮肤部分的填充属性,代码如下:

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

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

              xmlns:s="library://ns.adobe.com/flex/spark"

              xmlns:ho="library://ns.adobe.com/flex/halo"

              xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"skinClass="skins.CustomBitmapApplicationSkin">

</s:Application>

扩展的自定义的应用程序皮肤类,skins/CustomBitmapApplicationSkin.as如下:

package skins{

    import mx.graphics.BitmapFill;

    import mx.graphics.BitmapFillMode;

    import spark.skins.spark.ApplicationSkin;

    publicclass CustomBitmapApplicationSkin extends ApplicationSkin{

       [Embed("style/background.jpg")]

       protectedconst BgImg:Class;

       publicfunction CustomBitmapApplicationSkin(){

           super();

           var bmpFill:BitmapFill = new BitmapFill();

           bmpFill.source=BgImg;

           bmpFill.fillMode = BitmapFillMode.SCALE;

           backgroundRect.fill = bmpFill;

       }

    }

}

 

方案4:自定义皮肤类(有别于方案一,不需要BitmapGraphic)实现代码如下:

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

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

              xmlns:s="library://ns.adobe.com/flex/spark"

              xmlns:ho="library://ns.adobe.com/flex/halo"

              xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"skinClass="skins.CustomBitmapApplicationSkin2">

    <fx:Style>

       @namespace s "library://ns.adobe.com/flex/spark";

       s|Application {

           backgroundImageEmbed("style/background.jpg");

       }

    </fx:Style>

</s:Application>

自定义的应用程序皮肤类,skins/CustomBitmapApplicationSkin2.as如下:

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

<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"

       xmlns:s="library://ns.adobe.com/flex/spark"

       xmlns:fb="http://ns.adobe.com/flashbuilder/2009"

       alpha.disabledStates="0.5">

    <s:states>

       <s:State name="normal/>

       <s:State name="disabled" stateGroups="disabledStates/>

       <s:State name="normalWithControlBar" stateGroups="controlBarStates/>

       <s:State name="disabledWithControlBar" stateGroups="disabledStates,controlBarStates/>

    </s:states>

    <fx:Metadata>

       [HostComponent("spark.components.Application")]

    </fx:Metadata>

   

    <fx:Script fb:purpose="styling">

       <![CDATA[

           overrideprotectedfunction updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {

              backgroundRect.setStyle("backgroundAlpha", getStyle("backgroundAlpha"));

              backgroundRect.setStyle("backgroundColor", getStyle("backgroundColor"));

             

              if (getStyle("backgroundImage")) {

                  backgroundRect.setStyle("backgroundImage", getStyle("backgroundImage"));

                  backgroundRect.setStyle("backgroundImageFillMode""repeat");

              }

             

              super.updateDisplayList(unscaledWidth, unscaledHeight);

           }

       ]]>

    </fx:Script>

   

    <!-- fill -->

    <!---

    A rectangle with a solid color fill that forms the background of the application.

    The color of the fill is set to the Application's backgroundColor property.

    -->

    <s:BorderContainer id="backgroundRect"

                     backgroundColor="#FFFFFF"

                     borderVisible="false"

                     left="0" right="0" top="0" bottom="0/>

    <s:Group left="0" right="0" top="0" bottom="0">

       <s:layout>

           <s:VerticalLayout gap="0" horizontalAlign="justify/>

       </s:layout>

       <!--- Application Control Bar -->

       <s:Group id="topGroup"

               minWidth="0" minHeight="0"

               includeIn="controlBarStates>

           <!-- layer 0: control bar highlight -->

           <s:Rect left="0" right="0" top="0" bottom="1>

              <s:stroke>

                  <s:LinearGradientStroke rotation="90" weight="1">

                     <s:GradientEntry color="0xFFFFFF/>

                     <s:GradientEntry color="0xD8D8D8/>

                  </s:LinearGradientStroke>

              </s:stroke>

           </s:Rect>

           <!-- layer 1: control bar fill -->

           <s:Rect left="1" right="1" top="1" bottom="2>

              <s:fill>

                  <s:LinearGradient rotation="90">

                     <s:GradientEntry color="0xEDEDED/>

                     <s:GradientEntry color="0xCDCDCD/>

                  </s:LinearGradient>

              </s:fill>

           </s:Rect>

           <!-- layer 2: control bar divider line -->

           <s:Rect left="0" right="0" bottom="0" height="1" alpha="0.55">

              <s:fill>

                  <s:SolidColor color="0x000000/>

              </s:fill>

           </s:Rect>

           <!-- layer 3: control bar -->

           <s:Group id="controlBarGroup"

                   left="0" right="0" top="1" bottom="1"

                   minWidth="0" minHeight="0">

              <s:layout>

                  <s:HorizontalLayout paddingLeft="10" paddingRight="10" paddingTop="7" paddingBottom="7"gap="10/>

              </s:layout>

           </s:Group>

       </s:Group>

       <s:Group id="contentGroup"

               width="100%" height="100%"

               minWidth="0" minHeight="0/>

    </s:Group>

</s:Skin>

 

本人关于Flex4 Spark Application 背景图片设置的解决方案就到此,如有其它更高效,更可行的方案可以分享出来,互相学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值