这似乎是LinearGradient
班级的问题。我最初的假设是它BoxItemRenderer
在图表中绘制填充。但是看着那里让我看到begin()
了渲染器用来开始填充的渐变方法。
问题可能是一种特殊情况,当在宽高比很大的矩形上使用非 90 度角时,这一点很明显。您可以使用简单的矩形重新创建此问题:
<s:Rect width="200" height="40">
<s:fill>
<s:LinearGradient rotation="45">
<s:GradientEntry color="#ff0000"/>
<s:GradientEntry color="#0000ff"/>
</s:LinearGradient>
</s:fill>
</s:Rect>
我的 hacky 解决方案是扩展LinearGradient
和覆盖它的begin()
方法。我复制了原始代码,并注释掉了使其确定错误宽度的几行。我用不同的角度测试了它,它似乎渲染得很好。
诚然,我不明白我注释掉的行的目的,并且可能有一个有效的用例。
package
{
import flash.display.GradientType;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import mx.core.mx_internal;
import mx.graphics.LinearGradient;
use namespace mx_internal;
public class CustomGradient extends LinearGradient
{
private static var commonMatrix:Matrix = new Matrix();
public function CustomGradient()
{
super();
}
override public function begin(target:Graphics, targetBounds:Rectangle, targetOrigin:Point):void
{
commonMatrix.identity();
if (!compoundTransform)
{
var tx:Number = x;
var ty:Number = y;
var length:Number = scaleX;
if (isNaN(length))
{
// Figure out the two sides
if (rotation % 90 != 0)
{
// // Normalize angles with absolute value > 360
// var normalizedAngle:Number = rotation % 360;
// // Normalize negative angles
// if (normalizedAngle < 0)
// normalizedAngle += 360;
//
// // Angles wrap at 180
// normalizedAngle %= 180;
//
// // Angles > 90 get mirrored
// if (normalizedAngle > 90)
// normalizedAngle = 180 - normalizedAngle;
//
// var side:Number = targetBounds.width;
// // Get the hypotenuse of the largest triangle that can fit in the bounds
// var hypotenuse:Number = Math.sqrt(targetBounds.width * targetBounds.width + targetBounds.height * targetBounds.height);
// // Get the angle of that largest triangle
// var hypotenuseAngle:Number = Math.acos(targetBounds.width / hypotenuse) * 180 / Math.PI;
//
// // If the angle is larger than the hypotenuse angle, then use the height
// // as the adjacent side of the triangle
// if (normalizedAngle > hypotenuseAngle)
// {
// normalizedAngle = 90 - normalizedAngle;
// side = targetBounds.height;
// }
//
// // Solve for the hypotenuse given an adjacent side and an angle.
// length = side / Math.cos(normalizedAngle / 180 * Math.PI);
length=Math.max(targetBounds.width, targetBounds.height);
}
else
{
// Use either width or height based on the rotation
length = (rotation % 180) == 0 ? targetBounds.width : targetBounds.height;
}
}
// If only x or y is defined, force the other to be set to 0
if (!isNaN(tx) && isNaN(ty))
ty = 0;
else if (isNaN(tx) && !isNaN(ty))
tx = 0;
// If x and y are specified, then move the gradient so that the
// top left corner is at 0,0
if (!isNaN(tx) && !isNaN(ty))
commonMatrix.translate(GRADIENT_DIMENSION / 2, GRADIENT_DIMENSION / 2); // 1638.4 / 2
// Force the length to a absolute minimum of 2. Values of 0, 1, or -1 have undesired behavior
if (length >= 0 && length < 2)
length = 2;
else if (length < 0 && length > -2)
length = -2;
// Scale the gradient in the x direction. The natural size is 1638.4px. No need
// to scale the y direction because it is infinite
commonMatrix.scale (length / GRADIENT_DIMENSION, 1 / GRADIENT_DIMENSION);
commonMatrix.rotate (!isNaN(_angle) ? _angle : rotationInRadians);
if (isNaN(tx))
tx = targetBounds.left + targetBounds.width / 2;
else
tx += targetOrigin.x;
if (isNaN(ty))
ty = targetBounds.top + targetBounds.height / 2;
else
ty += targetOrigin.y;
commonMatrix.translate(tx, ty);
}
else
{
commonMatrix.translate(GRADIENT_DIMENSION / 2, GRADIENT_DIMENSION / 2);
commonMatrix.scale(1 / GRADIENT_DIMENSION, 1 / GRADIENT_DIMENSION);
commonMatrix.concat(compoundTransform.matrix);
commonMatrix.translate(targetOrigin.x, targetOrigin.y);
}
target.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios,
commonMatrix, spreadMethod, interpolationMethod);
}
}
}
[编辑]注释掉整个 if 语句并使用最大维度length
代替。原始黑客仅注释掉了该if (normalizedAngle > hypotenuseAngle)
条款。可能仍然有问题,但解决了这两个问题。