简体   繁体   English

Flex:修改嵌入的图标并在按钮中使用它?

[英]Flex: Modify an embedded icon and use it in a button?

Just that, if you embed an icon:就是这样,如果你嵌入一个图标:

[Embed(source='icons/checkmark.png')]
private static var CheckMark:Class;

You end up with a dynamic class.您最终会得到一个动态类。 You can pretty easily assign the icon to a button at runtime by calling the setStyle method:您可以在运行时通过调用 setStyle 方法轻松地将图标分配给按钮:

var btn:Button = new Button();
btn.setStyle("icon", CheckMark);

But what if you wanted to alter the icon at runtime, like changing it's alpha value or even redrawing pixels, before assigning it to the button?但是,如果您想在运行时更改图标,例如更改它的 alpha 值甚至重绘像素,然后再将其分配给按钮,该怎么办?

So far I can't find a satisfactory answer...至今没找到满意的答案……

This is the only answer I could find that seemed close: Dynamic Icons (example with View Source)这是我能找到的唯一一个看起来很接近的答案:动态图标(带有查看源代码的示例)

His solution involves a custom "DynamicIcon" class which is used in the button's icon setting, and a custom Button class which adds one method to the Button class to draw dynamic icons.他的解决方案涉及一个用于按钮图标设置的自定义“DynamicIcon”类,以及一个自定义 Button 类,它向 Button 类添加一个方法来绘制动态图标。

The end result is that you are able to send BitmapData to the DynamicIcon class, which will show up in the button.最终结果是您可以将 BitmapData 发送到 DynamicIcon 类,该类将显示在按钮中。 So, embed your image, instantiate your asset class, get the bitmapasset and modify it however you need to and send the bitmapData to the icon.因此,嵌入您的图像,实例化您的资产类,获取位图资产并根据需要修改它并将位图数据发送到图标。

It's an interesting problem and it seems like there should be an easier solution, but this works without a lot of hassle.这是一个有趣的问题,似乎应该有一个更简单的解决方案,但这并不麻烦。

The way I'd solve this is to implement a programmatic skin class that draws the icon itself manually.我解决这个问题的方法是实现一个手动绘制图标本身的程序化皮肤类。 There's probably more work you'll have to do to ensure the button calculates the correct size as if it has an icon even though it doesn't.您可能需要做更多的工作来确保按钮计算正确的大小,就像它有一个图标一样,即使它没有。 You may have to poke through the Button source code to look at how the reference to the icon is stored.您可能需要查看 Button 源代码以查看对图标的引用是如何存储的。

I love just creating programmatic skins that do exactly what I want and then using interesting CSS declarations to modify states - for instance:我喜欢创建完全符合我要求的程序化皮肤,然后使用有趣的 CSS 声明来修改状态 - 例如:

button.setStyle("customIconAlpha", .4);

and then of course the skin or the custom button class would have:然后当然皮肤或自定义按钮类将具有:

var alpha:Number = getStyle("customIconAlpha") as Number;

(not sure if you have to typecast that one) (不确定您是否必须对那个进行类型转换)

The big problem I found with programmatic skins is that the button refuses to measure the width/height.我发现程序化皮肤的一个大问题是按钮拒绝测量宽度/高度。 I easily got around this by overriding the get methods for each:我通过覆盖每个的 get 方法轻松解决了这个问题:

override public function get width():Number { return WIDTH;覆盖公共函数 get width():Number { return WIDTH; } override public function get height():Number { return HEIGHT; } override public function get height():Number { return HEIGHT; } }

In my case I needed to modify buttons in a TabNavigator, hence no easy way to subclass the button.在我的情况下,我需要修改 TabNavigator 中的按钮,因此没有简单的方法来对按钮进行子类化。 Thankfully, the parent of each skin is the button, so using static methods within your skin, you can identify the instance of the Button to which the icon skins belong.值得庆幸的是,每个皮肤的父级都是按钮,因此在您的皮肤中使用静态方法,您可以识别图标皮肤所属的 Button 实例。

If you're using the cover-all "icon" style, a new skin object will be created for each state.如果您使用覆盖所有的“图标”样式,将为每个状态创建一个新的皮肤对象。 So you'll need to keep this in mind when changing the state of the icons.因此,在更改图标状态时,您需要牢记这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM