Q: How can I scale an externally loaded image?
Step 1: Load External images. Second, use its scalex and scaley attributes to scale their proportions, and third, scale proportionally by clicking an event.
- Package
- {
- Import flash. display. movieclip;
- Import flash. Events .*;
- Import flash. display. loader;
- Import flash.net. URLRequest;
- Import flash. display. Bitmap;
- Public class example extends movieclip
- {
- Public Function example ()
- {
- Init ();
- }
- Private function Init (): void
- {
- VaR _ Loader: loader = new loader ();
- _ Loader. Load (New URLRequest ("1.jpg "));
- _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
- _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
- }
- Private function loadhander (E: Event): void
- {
- Trace ("loaded successfully ");
- Trace(e.tar get. content );
- VaR image: bitmap = E. currenttarget. content as bitmap;
- // Var bitmap: bitmapdata = image. bitmapdata;
- VaR MC: movieclip = new movieclip ();
- MC. addchild (image );
- Addchild (MC );
- MC. scalex = 0.2; // set the percentage after loading to 0.2.
- MC. scaley = 0.2; // set the proportion of the uploaded image to 0.2.
- MC. Rotation =-30; // convert its angle to-30 degrees.
- MC. x = stage. stagewidth/2-100; // set the X coordinate
- MC. Y = stage. stageheight/2-100; // set Y coordinates
- MC. addeventlistener (mouseevent. Click, onclick );
- }
- Private function iohander (E: ioerrorevent): void
- {
- Trace ("loading error ");
- }
- Private function onclick (E: mouseevent): void
- {
- Trace ("You clicked on me ");
- E. currenttarget. scalex = 1; // restore the original proportion
- E. currenttarget. scaley = 1;
- }
- }
- }
Add a proportional scaling code based on the Code of multimedia application base 8. Such an image can be scaled, but the effect is not monotonous, not dynamic. To achieve its dynamic effect, we add a buffer to make the interaction more exciting.
Output result:
Question 2: How to buffer images?
Step 1: Use the buffer function to import two packages
Import Fl. transitions. Tween;
Import Fl. transitions. Easing .*;
Official Document reference:
The tween class allows you to move, resize, and fade in and out a video clip by specifying the attributes of the target video clip to play an animated effect in several frames or seconds.
The tween class also allows you to specify various easing methods. "Easing" is the progressive acceleration and progressive deceleration effect during the animation operation, which helps make the animation more realistic. The fl. transitions. Easing package provides many easing methods (including the equations for acceleration and deceleration) that change the easing animation accordingly.
The fl. transitions. Easing package contains classes that can be used with the fl. transitions class to create easing effects. "Easing" refers to the gradual acceleration or deceleration in the animation process, which will make your animation look more realistic. The classes in this package support multiple easing effects to enhance the animation effect.
- Package
- {
- Import flash. display. movieclip;
- Import flash. Events .*;
- Import flash. display. loader;
- Import flash.net. URLRequest;
- Import flash. display. Bitmap;
- Import Fl. transitions. Tween;
- Import Fl. transitions. Easing .*;
- Public class example extends movieclip
- {
- Private var key: Boolean = true;
- Public Function example ()
- {
- Init ();
- }
- Private function Init (): void
- {
- VaR _ Loader: loader = new loader ();
- _ Loader. Load (New URLRequest ("1.jpg "));
- _ Loader. contentloaderinfo. addeventlistener (event. Complete, loadhander );
- _ Loader. contentloaderinfo. addeventlistener (ioerrorevent. io_error, iohander );
- }
- Private function loadhander (E: Event): void
- {
- Trace ("loaded successfully ");
- Trace(e.tar get. content );
- VaR image: bitmap = E. currenttarget. content as bitmap;
- // Var bitmap: bitmapdata = image. bitmapdata;
- VaR MC: movieclip = new movieclip ();
- MC. addchild (image );
- Addchild (MC );
- MC. scalex = 0.2;
- MC. Scaling = 0.2;
- MC. Rotation =-30;
- MC. x = stage. stagewidth/2-100;
- MC. Y = stage. stageheight/2-100;
- MC. addeventlistener (mouseevent. Click, onclick );
- }
- Private function iohander (E: ioerrorevent): void
- {
- Trace ("loading error ");
- }
- Private function onclick (E: mouseevent): void
- {
- Trace ("You clicked on me ");
- If (key)
- {
- VaR mytween: tween = new tween (E. currenttarget, "scalex", elastic. easeout, 0.2, 1, 3, true );
- VaR mytween2: tween = new tween (E. currenttarget, "scaley", elastic. easeout, 0.2, 1, 3, true );
- Key = false;
- } Else
- {
- VaR mytween3: tween = new tween (E. currenttarget, "scalex", elastic. easeout, 1, 0.2, 3, true );
- VaR mytween4: tween = new tween (E. currenttarget, "scaley", elastic. easeout, 1, 0.2, 3, true );
- Key = true;
- }
- }
- }
- }
The tween class is used to achieve the image buffering effect. After you set the initial scaling ratio to 0.2 clicks, the ratio can be restored to achieve some effect.
You can also modify elastic. easeout to make it more effective. In this way, we can use it to achieve the effect.
Output result: