如何从spark.LinearGradient到beginGradientFill

发布于 2024-11-03 14:54:23 字数 606 浏览 0 评论 0原文

我正在尝试将此 LinearGradient 转换

<s:LinearGradient rotation="90" scaleX="44.2931" x="10.294" y="-0.276" >
    <s:GradientEntry color="#FFD500" ratio="0"/>
    <s:GradientEntry color="#F5A106" ratio="1"/>
</s:LinearGradient>

为等效的 beginGradientFill 调用。我对翻译的最佳猜测是这样的

var matr:Matrix = new Matrix();
matr.createGradientBox(20, 20, Math.PI/2, 10.294, -0.276);
g.beginGradientFill(GradientType.LINEAR, [0xFFD500, 0xF5A106], [1,1], [0,1], matr); 

,但我不确定这是否正确,也不知道盒子的大小应该是多少(上面代码中的两个 20 只是我尝试的东西)。希望任何专家专家的意见!

谢谢你!

I'm trying to translate this LinearGradient

<s:LinearGradient rotation="90" scaleX="44.2931" x="10.294" y="-0.276" >
    <s:GradientEntry color="#FFD500" ratio="0"/>
    <s:GradientEntry color="#F5A106" ratio="1"/>
</s:LinearGradient>

into an equivalent beginGradientFill call. My best guess of the translation is this

var matr:Matrix = new Matrix();
matr.createGradientBox(20, 20, Math.PI/2, 10.294, -0.276);
g.beginGradientFill(GradientType.LINEAR, [0xFFD500, 0xF5A106], [1,1], [0,1], matr); 

but I'm not sure if this is right, nor I know what the size of the box should be (the two 20s in the code above are just me trying stuff). Would love any expert expert opinions!

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2024-11-10 14:54:23

此示例可以说明如何绘制与 s:LinearGradient 中相同的填充:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        public static const GRADIENT_DIMENSION:Number = 1638.4;

        private static const GRADIENT_SCALE_X:Number = 44.2931;
        private static const GRADIENT_X:Number = 10.294;
        private static const GRADIENT_Y:Number = 0.276;
        private static const ROTATION_DEGREES:Number = 90;

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);

            var g:Graphics = drawingTarget.graphics;
            g.clear();
            var matrix:Matrix = getMatrix(ROTATION_DEGREES, GRADIENT_X, -GRADIENT_Y, GRADIENT_SCALE_X);
            g.beginGradientFill(GradientType.LINEAR, [ 0xFFD500, 0xF5A106 ], [ 1, 1 ], [ 0, 255 ], matrix);
            g.drawRect(0, 0, drawingTarget.width, drawingTarget.height);
            g.endFill();
        }

        private function getMatrix(rotationDegrees:Number, x:Number, y:Number, scaleX:Number = 1):Matrix
        {
            var commonMatrix:Matrix = new Matrix();
            commonMatrix.translate(GRADIENT_DIMENSION / 2, GRADIENT_DIMENSION / 2);
            commonMatrix.scale(1 / GRADIENT_DIMENSION, 1 / GRADIENT_DIMENSION);
            var compoundTransform:Matrix = new Matrix();
            compoundTransform.scale(scaleX, 1);
            compoundTransform.rotate(rotationDegrees * Math.PI / 180);
            compoundTransform.translate(x, y);
            commonMatrix.concat(compoundTransform);
            return commonMatrix;
        }

        private function init():void
        {
            invalidateDisplayList();
        }
    ]]>
    </fx:Script>
    <s:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
    <s:Rect height="50%" width="50%">
        <s:fill>
            <s:LinearGradient rotation="{ROTATION_DEGREES}" scaleX="{GRADIENT_SCALE_X}" x="{GRADIENT_X}"
                y="{GRADIENT_Y}">
                <s:GradientEntry color="#FFD500" ratio="0" />
                <s:GradientEntry color="#F5A106" ratio="1" />
            </s:LinearGradient>
        </s:fill>
    </s:Rect>
    <mx:UIComponent height="50%" id="drawingTarget" width="50%" />
</s:Application>

看一下执行所有计算的 getMatrix() 方法。

This sample can illustrate how to draw the same fill as in s:LinearGradient:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        public static const GRADIENT_DIMENSION:Number = 1638.4;

        private static const GRADIENT_SCALE_X:Number = 44.2931;
        private static const GRADIENT_X:Number = 10.294;
        private static const GRADIENT_Y:Number = 0.276;
        private static const ROTATION_DEGREES:Number = 90;

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);

            var g:Graphics = drawingTarget.graphics;
            g.clear();
            var matrix:Matrix = getMatrix(ROTATION_DEGREES, GRADIENT_X, -GRADIENT_Y, GRADIENT_SCALE_X);
            g.beginGradientFill(GradientType.LINEAR, [ 0xFFD500, 0xF5A106 ], [ 1, 1 ], [ 0, 255 ], matrix);
            g.drawRect(0, 0, drawingTarget.width, drawingTarget.height);
            g.endFill();
        }

        private function getMatrix(rotationDegrees:Number, x:Number, y:Number, scaleX:Number = 1):Matrix
        {
            var commonMatrix:Matrix = new Matrix();
            commonMatrix.translate(GRADIENT_DIMENSION / 2, GRADIENT_DIMENSION / 2);
            commonMatrix.scale(1 / GRADIENT_DIMENSION, 1 / GRADIENT_DIMENSION);
            var compoundTransform:Matrix = new Matrix();
            compoundTransform.scale(scaleX, 1);
            compoundTransform.rotate(rotationDegrees * Math.PI / 180);
            compoundTransform.translate(x, y);
            commonMatrix.concat(compoundTransform);
            return commonMatrix;
        }

        private function init():void
        {
            invalidateDisplayList();
        }
    ]]>
    </fx:Script>
    <s:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
    <s:Rect height="50%" width="50%">
        <s:fill>
            <s:LinearGradient rotation="{ROTATION_DEGREES}" scaleX="{GRADIENT_SCALE_X}" x="{GRADIENT_X}"
                y="{GRADIENT_Y}">
                <s:GradientEntry color="#FFD500" ratio="0" />
                <s:GradientEntry color="#F5A106" ratio="1" />
            </s:LinearGradient>
        </s:fill>
    </s:Rect>
    <mx:UIComponent height="50%" id="drawingTarget" width="50%" />
</s:Application>

Take a look at getMatrix() method which performs all the calculations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文