音量指示器技术

时间:2012-09-03 07:03:04

标签: volume indicator

我绝望地试图建立音量指标

刚刚找到一些音量滑块示例。 我想要的是如下图所示:

有哪些好的音量指示器技术可以处理?

2 个答案:

答案 0 :(得分:2)

我试图创造你需要的东西。请看一下here

如果您正在寻找,请输入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="group1_creationCompleteHandler(event)">

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        private static const BAR_HEIGHT:Number = 44;
        private static const BAR_WIDTH:Number = 5;
        private static const BAR_GAP:Number = 2;
        private static const BAR_COUNT:Number = 50;
        private static const BAR_COLOR_ACTIVE:uint = 0x00ff00;

        [Bindable]
        public var initialPercent:Number;

        private var totalBarHeight:Number;

        protected function group1_creationCompleteHandler(event:FlexEvent):void
        {
            totalBarHeight = BAR_HEIGHT * BAR_COUNT;
            drawBars(blackBars);
            update(initialPercent);
        }

        public function update(percent:Number):void {
            drawBars(greenBars, BAR_COLOR_ACTIVE, percent);
        }

        public function drawBars(where2draw:Group, color:uint = 0x000000, percent:Number = 100):void {
            //complete bars
            var completeBars:Number = Math.floor(BAR_COUNT * percent / 100); 
            where2draw.graphics.clear();
            for (var i:int = 0; i < completeBars; i++) 
            {
                var barX:Number = (BAR_WIDTH + BAR_GAP) * i;
                where2draw.graphics.beginFill(color);
                where2draw.graphics.drawRect(barX, 0, BAR_WIDTH, BAR_HEIGHT);
                where2draw.graphics.endFill();
            }
            //partial bars, if needed
            var reminderBarHeight:Number = totalBarHeight * percent / 100 - completeBars * BAR_HEIGHT;
            var barY:Number = BAR_HEIGHT - reminderBarHeight;
            var barX:Number = (BAR_WIDTH + BAR_GAP) * completeBars;
            where2draw.graphics.beginFill(color);
            where2draw.graphics.drawRect(barX, barY, BAR_WIDTH, reminderBarHeight);
            where2draw.graphics.endFill();
        }

    ]]>
</fx:Script>

<s:Group id="blackBars"/>
<s:Group id="greenBars"/>

</s:Group>

使用ActionScript构建它可能更好。我创建它只是为了传达这个想法。

说到你需要的第二个组件,应该是事件更容易。您可以通过在组件上绘制渐变并在值发生变化时为蒙版设置动画来实现该效果。

谢谢!

答案 1 :(得分:1)

以下是您的需求:

<?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:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)" backgroundColor="0x000000">

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private static const SLIDER_HEIGHT:Number = 237;
            private static const SQUARE_HEIGHT:Number = 9;
            private static const SQUARE_GAP:Number = 3;

            protected function hslider1_changeHandler(event:Event):void
            {
                updateSlider(volumeSlider.value);
            }

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                var squaresNumber:Number = Math.floor((SLIDER_HEIGHT + SQUARE_GAP) / (SQUARE_HEIGHT + SQUARE_GAP));
                for (var i:int = 0; i < squaresNumber ; i++) 
                {
                    sliderMask.graphics.beginFill(0x000000);
                    sliderMask.graphics.drawRect(0, i * (SQUARE_HEIGHT + SQUARE_GAP), SQUARE_HEIGHT, SQUARE_HEIGHT);
                    sliderMask.graphics.endFill();
                }
                updateSlider(volumeSlider.value);
            }

            private function updateSlider(value:Number):void
            {
                redEntry.color = value >= 1 ? 0xbf0000 : 0x009c00;
                yellowEntry.color = value >= 1 ? 0xe0ec0b : 0x009c00;
                greenEntry.ratio = value > 50 ? 1 : value / 50;
                redEntry.ratio = value > 50 ? (value/50 - 1) * .67 + .33 : value / 50 * .33;;
                yellowEntry.ratio = value > 50 ? (value/50 - 1) * .33 + .67 : value / 50 * .67;
            }

        ]]>
    </fx:Script>

    <s:Group verticalCenter="0" horizontalCenter="0" height="{SLIDER_HEIGHT}" width="9">
        <s:Rect height="100%" width="100%" mask="{sliderMask}">
            <s:fill>
                <s:LinearGradient rotation="90">
                    <s:GradientEntry id="redEntry" ratio="0" color="0xbf0000"/>
                    <s:GradientEntry id="yellowEntry" ratio="0" color="0xe0ec0b"/>
                    <s:GradientEntry id="greenEntry" ratio="0" color="0x009c00"/>
                </s:LinearGradient>
            </s:fill>
        </s:Rect>
        <s:Group id="sliderMask"/>
    </s:Group>

    <s:HSlider verticalCenter="{SLIDER_HEIGHT / 2 + 30}" horizontalCenter="0" 
               id="volumeSlider" maximum="100" minimum="0" value="20" change="hslider1_changeHandler(event)"/>

</s:Application>

有一点需要澄清。 当您逐个添加GradientEntry并将每个比率设置为0时,前两个条目不会消失。因此,当滑块为0或1或介于两者之间时,我不得不将黄色和红色条目更改为绿色。

干杯!