Animation can be easily realized using Image and Timer controls in Visual Basic. This article will introduce the basic principles of Visual Basic animation programming and the programming techniques for realizing three different types of animation.
1. Basic principles
----Animation is a simulation of movement, which is achieved by quickly displaying a set of related images on the screen. Therefore, the basis for realizing animation is the display of images and the rapid and regular movement or change of images.----In Visual Basic, use the LoadPicture function to load image files in BMP, ICO and WMF formats into memory, and assign the function return value to the Picture property of the Image object to display the image in the Image object. There are three basic methods to make images move or change, namely:
----The Enabled property of the Timer object determines whether the Timer event is valid. Setting the Enabled property to True will start the Timer event (such as starting the animation); to False will invalidate the Timer event (such as stopping the animation).
2. Motionless animation
----No-position animation means that the animated object does not move, but the image keeps changing. A typical example is turning a book. The method to achieve motion-free animation is to set up the Image object and Timer object, call the LoadPicture function during the Timer event to load different images, and assign the Picture attribute of the Image object to display different images in the object, that is Implement image changes.----The following is an example of a flip book animation. When the program starts, an open book is displayed in the form. Click the book with the left mouse button to start flipping the book; click the book with the left mouse button again to stop flipping the book. The bitmap files book1.bmp~book4.bmp respectively represent different positions of the page being turned when turning the book. They are stored in the directory where the current project is located.
----Set the Image object Image1 and Timer object Timer1 in the form (Form1) that needs to display animation, and set their properties as shown in the following table. Use default values for unlisted properties.
.
.
.
3. Single frame displacement animation
----Single-frame displacement animation refers to an animation formed by constantly changing the position of the same image. A typical example of this is when clouds are blown by the wind. The method of programming a single-frame displacement animation is to call the Move method of the Image object during the Timer event to move the image.----Below is an example of cloud movement. When the program starts, a cloud is displayed in the form. Click the cloud with the left button of the mouse, and the cloud will begin to flutter. If it touches the boundary of the form, the cloud will change its direction of movement; click the cloud with the left button of the mouse again, and the cloud will begin to move. will stop moving. The bitmap file cloud.bmp is stored in the directory where the current project is located.
----Set the Image object Image1 and Timer object Timer1 in the form (Form1) that needs to display animation, and set their properties as shown in the following table. Use default values for unlisted properties.
.
.
.
----Note that when the clouds hit the boundary and move in the opposite direction, this is achieved by changing the signs of DetaX and DetaY.4. Multi-frame displacement animation
----Multi-frame displacement animation is the most complex animation, which combines the characteristics of non-displacement animation and single-frame displacement animation. Most movements in nature have the characteristics of multi-frame displacement, such as the flight of a bird. While the bird's position moves, its wings are also flapping. To implement multi-frame displacement animation, the image replacement and position movement of the Image object need to be processed simultaneously during the Timer event process.----The following is an example of a bird flying. When the program starts, a bird is displayed in the form. Click it with the left mouse button, and the bird will start to flap its wings. If it hits the boundary of the form, the bird will change its flying direction; click the left mouse button again. Click on the bird and the bird will stop flying. The bitmap files bird1.bmp~bird4.bmp respectively represent the different positions of the bird's wings when it flies. They are stored in the directory where the current project is located.
----Set the Image object Image1 and Timer object Timer1 in the form (Form1) that needs to display animation, and set their properties as shown in the following table. Use default values for unlisted properties.
.
.
.
5. Zoom animation
----The inflation or deflation of a balloon is a classic example of a zoom animation. Zoom animation can be implemented by modifying the Width and/or Height properties of the Image object during the Timer event. But if you want to show the concentric scaling of the object, you should also move the Image object at the same time.----The following is an example of simulating balloon scaling in the air (concentric). When the program starts, a balloon is displayed in the form. Click it with the left mouse button, and the balloon will begin to expand. If it hits the boundary of the form, the balloon will shrink. When it shrinks to its original size, it will expand again; then use Click the left mouse button on the balloon, and the balloon will stop scaling. The bitmap file Balloon.bmp is stored in the directory where the current project is located.
----Set the Image object Image1 and Timer object Timer1 in the form (Form1) that needs to display animation, and set their properties as shown in the following table. Use default values for unlisted properties.
object
property
Set value
Image1
Picture
...(path to the project file)/Balloon.bmp
.
.
.
->