Publish a lightweight slider Control

Source: Internet
Author: User

It is much smaller than the system's built-in components, and supports progress bar display (it is useful to show the buffer progress when making a player). It also supports triangle volume adjustment display.

Example:

Package {import flash. display. simplebutton; import flash. display. sprite; import flash. events. event; import flash. text. textfield; [SWF (width = 150, Height = 130)] public class slidertest extends sprite {private var TXT: textfield; private var slider_progress: simpleslider; Public Function slidertest () {var slider_v: simpleslider = new simpleslider (100,200,180); addchild (slider_v); slider_v.backwidth = 0; slider_v.backheight = 100; Limit = 6; Limit = 10; slider_v.x = slider_v.y = 10; slider_v.value = 120; // test the value of VaR slider_h: simpleslider = new simpleslider (0.0, 1.0, 0.5); addchild (slider_h); slider_h.backwidth = 100; slider_h.backheight = 0; Limit = 10; slider_h.y = 40; slider_h.x = 30; slider_h.value = 0.3; // test the value assignment // slider_progress = new simpleslider (0,100, 33, true); addchild (slider_progress ); slider_progress.backwidth = 100; slider_progress.backheight = 8; Height = 18; slider_progress.y = 62; slider_progress.x = 30; Progress = false; Progress = true; Progress = 0x666666; Progress = 0; this. addeventlistener (event. enter_frame, onenterframe); // triangle (volume) slider var slider_sound: simpleslider = new simpleslider (0, 1.0, 0.5); trim = true; slider_sound.backwidth = 100; slider_sound.backheight = 18; slider_sound.handleheight = slider_sound.backheight + 2; slider_sound.y = 90; slider_sound.x = 30; addchild (slider_sound); slider_sound.value = 0.3; events. change, onchange); slider_h.addeventlistener (event. change, onchange); slider_progress.addeventlistener (event. change, onchange); slider_sound.addeventlistener (event. change, onchange); TXT = new textfield (); TXT. TEXT = slider_v.value.tostring () + "," + slider_h.value.tostring () + "," + slider_progress.value.tostring () + "," + slider_sound.value.tostring (); TXT. width = 100; TXT. height = 20; TXT. X = 30; TXT. y = 10; TXT. selectable = false; addchild (txt);} private function onenterframe (E: Event): void {This. slider_progress.progressvalue + = 1; if (this. slider_progress.progressvalue >=100) {This. slider_progress.progressvalue = 0;} private function onchange (E: Event): void {var S: simpleslider = E. currenttarget as simpleslider; TXT. TEXT = S. value. tostring ();}}}

Simpleslider.

Package {import flash. display. sprite; import flash. events. event; import flash. events. mouseevent; import flash. geom. rectangle; // lightweight slider control (by yjmyzz@126.com, http://yjmyzz.cnblogs.com/) public class simpleslider extends sprite {private VaR _ value: Number; private VaR _ max: Number = 100; private VaR _ min: Number = 0; private VaR _ HANDLE: SPRITE; private VaR _ Back: SPRITE; private VaR _ progress: SPRITE; private VaR _ TRI: SPRITE; private VaR _ backwidth: Number = 0; private VaR _ backheight: Number = 100; private VaR _ backcolor: uint = 0 xcccccc; private VaR _ backbordercolor: uint = 0x999999; private VaR _ handlewidth: Number = 6; private VaR _ handleheight: Number = 20; private VaR _ handlecolor: uint = 0x000000; private VaR _ handlebordercolor: uint = 0 xcccccccc; private VaR _ progresscolor: uint = 0xffff99; private VaR _ showprogress: Boolean = false; private VaR _ progressvalue: uint = 0; private VaR _ showtri: Boolean = false; private VaR _ trihighlightcolor: uint = 0xffff99; Public Function simpleslider (Min: Number = 0, Max: Number = 100, value: Number = 100, showprogress: Boolean = false) {_ min = min; _ max = max; this. _ showprogress = showprogress; _ value = math. min (math. max (value, min), Max); Init ();} private function Init (): void {_ back = new sprite (); addchild (_ Back); this. _ progress = new sprite (); addchild (_ progress); this. _ tri = new sprite (); addchild (_ TRI); _ HANDLE = new sprite (); _ HANDLE. buttonmode = true; addchild (_ HANDLE); _ HANDLE. addeventlistener (mouseevent. mouse_down, mousedownhandler); Draw (); updateposition ();} private function draw (): void {Drawback (); drawhandle (); drawprogress (); drawtriback ();} // draw the triangle background private function drawtriback (): void {_ tri. graphics. clear (); If (this. _ showtri & _ backwidth> _ backheight) {_ back. graphics. clear (); _ progress. graphics. clear (); // draw the overall background _ tri. graphics. beginfill (_ backcolor); _ tri. graphics. linestyle (0, _ backbordercolor); _ tri. graphics. moveTo (0, _ backheight); _ tri. graphics. lineto (_ backwidth, _ backheight); _ tri. graphics. lineto (_ backwidth, 0); _ tri. graphics. lineto (0, _ backheight); _ tri. graphics. endfill (); _ tri. y = _ handleheight-_ backheight-1; // highlight part _ tri. graphics. beginfill (this. _ trihighlightcolor); _ tri. graphics. moveTo (0, _ backheight); _ tri. graphics. lineto (_ HANDLE. x, _ backheight); _ tri. graphics. lineto (_ HANDLE. x, _ backheight-_ HANDLE. x * _ backheight/_ backwidth); _ tri. graphics. lineto (0, _ backheight); _ tri. graphics. endfill () ;}} private function Drawback (): void {_ back. graphics. clear (); _ back. graphics. beginfill (_ backcolor); _ back. graphics. linestyle (0, _ backbordercolor); _ back. graphics. drawrect (0, 0, _ backwidth, _ backheight); _ back. graphics. endfill ();} private function drawprogress (): void {_ progress. graphics. clear (); If (this. _ showprogress) {_ progress. graphics. beginfill (this. _ progresscolor); _ progress. graphics. drawrect (1, 1, _ backwidth-2, _ backheight-2); _ progress. graphics. endfill () ;}} private function drawhandle (): void {_ HANDLE. graphics. clear (); _ HANDLE. graphics. beginfill (_ handlecolor); _ HANDLE. graphics. linestyle (0, _ handlebordercolor); _ HANDLE. graphics. drawrect (0, 0, _ handlewidth, _ handleheight); _ HANDLE. graphics. endfill (); If (_ backwidth> _ backheight) {// horizontal _ HANDLE. X = 0; _ back. y = _ handleheight/2-_ backheight/2; If (this. _ showprogress) {This. _ progress. y = _ back. Y ;}} else {// vertical _ HANDLE. X = _ back. width/2-_ HANDLE. width/2; _ HANDLE. y = 0 ;}} private function updateposition (): void {var handlerange: Number = _ backheight-_ handleheight; var valuerange: Number = _ max-_ min; if (_ backwidth> _ backheight) {// horizontal handlerange = _ backwidth-_ handlewidth; _ HANDLE. X = (_ value-_ min)/valuerange) * handlerange; If (this. _ showtri) {This. drawtriback () ;}} else {// vertical _ HANDLE. y = handlerange-(_ value-_ min)/valuerange) * handlerange;} private function updatevalue (): void {var handlerange: Number = _ backheight-_ handleheight; vaR valuerange: Number = _ max-_ min; If (_ backwidth> _ backheight) {// horizontal handlerange = _ backwidth-_ handlewidth; _ value = (_ HANDLE. x/handlerange) * valuerange + _ min; If (this. _ showtri) {This. drawtriback ();} // trace (_ HANDLE. x);} else {// vertical _ value = (handlerange-_ HANDLE. y)/handlerange * valuerange + _ min;} dispatchevent (new event (event. change);} private function mouseuphandler (E: mouseevent): void {stage. removeeventlistener (mouseevent. mouse_move, mousemovehandler); stage. removeeventlistener (mouseevent. mouse_up, mouseuphandler); _ HANDLE. stopdrag ();} private function mousedownhandler (E: mouseevent): void {stage. addeventlistener (mouseevent. mouse_move, mousemovehandler); stage. addeventlistener (mouseevent. mouse_up, mouseuphandler); // vertical slider if (_ backwidth <_ backheight) {_ HANDLE. startdrag (false, new rectangle (_ HANDLE. x, 0, 0, _ backheight-_ handleheight);} else {// horizontal slider _ HANDLE. startdrag (false, new rectangle (0, 0, _ backwidth-_ handlewidth, 0) ;}} private function mousemovehandler (E: mouseevent): void {updatevalue ();} public Function move (X: Number, Y: Number): void {This. X = x; this. y = y;} public function set backbordercolor (N: uint): void {_ backbordercolor = N; Draw ();} public function get backbordercolor (): uint {return _ backbordercolor;} public function set backcolor (N: uint): void {_ backcolor = N; Draw ();} public function get backcolor (): uint {return _ backcolor;} public function set handlebordercolor (N: uint): void {_ handlebordercolor = N; Draw ();} public function get handlebordercolor (): uint {return _ handlebordercolor;} public function set handlecolor (N: uint): void {_ handlecolor = N; Draw ();} public function get handlecolor (): uint {return _ handlecolor;} // sets the maximum value public function Set max (N: Number): void {_ max = N; updateposition ();} // obtain the maximum public function get Max (): number {return _ max;} // set the minimum public function set min (N: Number): void {_ min = N; updateposition () ;}// obtain the maximum value public function get min (): number {return _ min;} // set the current value of the slider public function set value (N: number ): void {_ value = N; _ value = math. min (_ max, math. max (_ value, _ min); updateposition () ;}// obtain the current value of the slider public function get value (): number {return _ value ;} // set the slider width public function set handlewidth (N: Number): void {_ handlewidth = N; Draw () ;}// obtain the slider width public function get handlewidth (): number {return _ handlewidth;} // sets the background height. Public Function set backheight (N: Number): void {_ backheight = N; Draw ();} // obtain the background height public function get backheight (): number {return _ backheight;} // set the slider height public function set handleheight (N: number ): void {_ handleheight = N; Draw (); updateposition () ;}// obtain the slider height public function get handleheight (): number {return _ handleheight ;} // set the background width public function set backwidth (N: Number): void {_ backwidth = N; Draw () ;}// set the background width public function get backwidth (): number {return _ backwidth;} public function set progressvalue (V: uint): void {If (V> = 100) {v = 100;} else if (v <= 0) {v = 0;} This. _ progressvalue = V; If (this. _ showprogress) {This. _ progress. width = (_ backwidth-2) * _ progressvalue/100;} public function get progressvalue (): uint {return this. _ progressvalue;} public function set progresscolor (V: uint): void {This. _ progresscolor = V; drawprogress ();} public function get progresscolor (): uint {return this. _ progresscolor;} public function set showprogress (V: Boolean): void {This. _ showprogress = V; drawprogress ();} public function get showprogress (): Boolean {return this. _ showprogress;} public function set showtri (V: Boolean): void {This. _ showtri = V; this. drawtriback ();} public function get showtri (): Boolean {return this. _ showtri ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.