Android system programming introduction series interface activity responds to silky traditional animation

The previous article introduced the relevant methods and locations of user operation response in the application. The simple response logic can jump from one interface activity to another, or it can be the relative change of some views. However, it takes time to start an interface to execute the life cycle method of the new interface activity or the relative change of the view, so there is a blank time before the final result of the response is completed. In this long or short period of time, how to show the interface to the user? This uses the animation process recommended by the Android system.

Broadly speaking, the Android system can use relevant animation transition when the content drawn and displayed to users on the screen changes. Consistent with the response of user operation, according to the action objects of animation, the effect of displaying animation can act on interface activity or view. Most articles are based on the introduction of frame animation and gap animation, which will be introduced separately according to the action object of animation.

View animation can act on any view that needs to display animation effects. View animation is the most native animation type of Android system. The definition class of view animation can view android.view.animation.animation. Its subclass is the effect classification of view animation, including gradient effect alphaanimation, rotation effect rotateanimation, scaling effect scaleanimation The movement effect translateanimation, and the combined effect animation set that combines the above effects.

Invalid Sign

For dynamically declared view animation effects, you only need to define the animation classes corresponding to the above five animation effects in the code and set relevant data parameters. among

The gradient effect alpha animation (float fromalpha, float toalpha) must set the fromalpha parameter as the starting transparency of the gradient effect and the toalpha parameter as the end transparency of the gradient effect.

The rotation effect rotateanimation (float from degrees, float pivotyvalue) must set the fromdegrees parameter as the starting rotation angle of the rotation effect and the todegrees parameter as the end rotation angle of the rotation effect; Other parameters are optional, including the last four parameters in the form of screen coordinates. The pivotxtype parameter is the pivotxvalue parameter type, and the pivotxvalue parameter is the angle value of rotation around the X axis. Similarly, the pivotytype parameter is the pivotyvalue parameter type, and the pivotyvalue parameter is the angle value of rotation around the Y axis. The parameter types include the absolute type of animation.absolute, and the parameter value represents the absolute value; Animation.RELATIVE_ TO_ Self is relative to its own view type, and its parameter value is the percentage value relative to its own view before the animation effect; Animation.RELATIVE_ TO_ Parent is relative to the parent view type, and its parameter value is the percentage value of the relative parent view before the animation effect.

The scaling effect scaleanimation (float fromx, float pivotvalue) also records parameters in the form of screen coordinates. You must set the fromx parameter as the scale in the x-axis direction at the beginning of the scaling effect, and the tox parameter as the scale in the x-axis direction at the end of the scaling effect. Similarly, the fromy parameter as the scale in the y-axis direction at the beginning of the scaling effect, The toy parameter is used as the scale in the y-axis direction at the end of the scaling effect; Other parameters are optional, and their meaning is similar to the optional parameters of rotation effect.

Translatanimation (float fromxdelta, float toydelta) and translatanimation (int fromxtype, float toyvalue) record parameters in the form of screen coordinates. You can choose either of these two construction methods. When the parameter value of type series in the eight parameter construction method is animation.relax_ TO_ During self, the data represented by the value series parameters is consistent with the delta series parameters of the four parameter construction method.

The aggregate effect animationset (Boolean shareinterpolator) can determine whether to use the interpolator defined in the current class object when displaying a series of animations in the animation according to the shareinterpolator parameter. Then, you can call addanimation (animation a) of the object to put it into the series view animation object to be executed in turn.

The interpolator mentioned here is a class that implements the android.view.animation.interpolator interface. These interpolator classes calculate the effect display from the beginning to the end of the view animation effect. In the above view animation effect animation object, you can set it by calling the setinterpolator (interpolator I) method, If not set, the android.view.animation.linearinterpolator linear interpolator will be used by default.

Among the construction methods of the above five effect classes, there is also a construction method of the (context, attributeset attrs) parameter. Using this construction method, you can pass in the statically declared animation resource file through the parameter 2 attrs, so as to use the instantiated object in the code.

Statically declared view animation files must be saved in RES / anim / resource directory and conform to XML format. The root label of the file must be one of five view animation effects, including gradient effect < alpha > < / alpha >, rotation effect < scale > < / scale >, scaling effect < rotate > < / rotate >, movement effect < translate > < / translate >, collection effect < set > < / set >. The required and optional parameters also correspond to those in the code dynamic declaration.

Finally, the instantiated objects of the above five view animation classes can be obtained in the code. Setanimationlistener (animation. Animationlistener listener) can be called to set the monitoring of animation execution. In the implemented android.view.animation.animation.animationlistener interface instance, onanimationstart (animation) before the animation starts, onanimationrepeat (animation) when the animation repeats, and onanimationend (animation) can be implemented respectively Callback listening at the end of the animation. At the place where the animation display is started, call the startnow () method to start immediately; Or first call setstarttime (long starttimemillis) to set the start delay time, and then call the start () method to start timing. After the delay time, the animation will be displayed.

Invalid Sign

For dynamically declared picture animation, you can instantiate the frame animation composed of loaded bitmaps using the android.graphics.drawable.animationdrawable class. Then call the addframe (drawable frame, int duration) method of the object in turn to add each frame of pictures to be displayed, where parameter 1 frame specifies the picture resources to be loaded, and parameter 2 duration indicates the time length of the current picture resource frame in the whole animation playback, and the unit is mm. You can also call setoneshot (Boolean oneshot) of the object to set whether the picture animation of the current series of frames is played only once. Finally, the start () of the object is called where the image animation needs to be started, and the stop () of the object is called where the image animation needs to be stopped.

Statically declared picture animation files must be saved in RES / drawable / directory and conform to XML format. The root tag of the file must be < animation list > < / animation list >. In this tag, you can use the Android: oneshot attribute value of true or false to indicate whether the picture animation of the current series of frames is played only once. Each frame of the picture uses the Android: drawable attribute value in < item / > as the drawable resource file reference. At the same time, the display time of the current picture frame should be set through the Android: duration attribute value. According to the recognition speed of human eyes, it is usually set below 1000 (unit: milliseconds). Later, if you want to use it statically, you can set its Android: background attribute in the view to display the animation, and assign the statically declared resource file name as the drawable resource type. If you want to use it dynamically, first find the object corresponding to the view to display the animation in the code, call the setbackgroundresource (int RES) of the object, and also assign the statically declared resource file name as the r.drawable resource type, which can also be used in association. Finally, in the code that needs to start the picture animation, get the interface android.graphics.drawable.animatable object of the picture animation by calling the getbackground () method of the view object, and then call the start () and stop () methods respectively at the position where the picture animation needs to be started and stopped.

In addition, in Android 5.0, i.e. API 21 and above, the project dependency libraries support vector drawable and animated vector drawable are added to the project module to support the definition and drawing of vector graph in the resource file. When Android studio creates a default project, the default application icon is the vector graph object, which has the advantages of undistorted scaling and small volume, Not detailed here. For this vector graph object, you can also add vector graph animation more quickly. Since vector graph can only be statically declared in resource file, corresponding vector graph animation can only be statically declared in resource file. This animation effect mainly depends on the android.graphics.drawable.animatedvectordrawable class or the downward compatible android.vectordrawable.graphics.drawable.animatedvectordrawablecompat class in the latest package.

The declaration of vector graph is to represent ordinary vector graph with a layer wrapped by < vector > < / vector > label in RES / drawable / resource directory. You can add < group > < / group > label inside the label to wrap a group of animation effects, set its Android: name attribute to mark the name of the group of animation, and use the attribute values related to view animation as the display of view animation effects, including gradient, rotation Zoom and pan. You can also continue to use the < Path > < / Path > tag inside the < group > tag to define a series of paths. You also need to set its Android: name attribute to mark the path name of the group.

The declaration of vector animation is the animation effect wrapped with < objectanimator > < / objectanimator > as the root label under RES / Animator / resource directory. By setting its Android: propertyname attribute value, it can describe the effects of gradient, zoom, rotation and translation in the view animation effect. Its internal properties are similar to those in the statically declared view animation. Taking < set > < / set > as the root tag can wrap a series of < objectanimator > < / objectanimator > tags represented by the above four animation effects.

Finally, associate the vector graph with vector animation. You need to create a new XML resource file in the RES / drawable resource directory, take < animated vector > < / animated vector > as the root tag, set its Android: drawable attribute, and reference the above common vector graph resource file to assign a value to the drawable resource type. In the root tag, use < target Android: name = "" Android: animation = "" / > to respectively associate the Android: name attribute value in the vector graph definition with the resource file in the vector graph declaration. Finally, the reference to the common vector graph resource file is changed to the reference to the animation vector graph resource file.

Interface animation is only supported in Android 5.0, API 21 and above. Interface animation acts on interface activities. Interface animation needs to be displayed only when two interface activities start and switch with each other. Therefore, the display objects of interface animation are mainly divided into three types, including the animation effect when the old interface activity exits, the animation effect when the new interface activity enters, and if there are similar views with the same content between the two interfaces, It is called shared view, and its animation effect when the interface is switched. As for the animation effects of these objects, the Android SDK provides some unified animation effects to choose from, including android.transition.fade class for fade in and fade out effects of exit and entry animation, android.transition.slide class for mobile effects, and android.transition.explode class for explosion effects; There are also view animation effects for shared views. Alternatively, you can inherit android.transition.transition to achieve custom animation effects.

Since the definition of interface activity is statically registered in the manifest file, the use of interface animation can also be statically declared during registration. The < activity Android: style / > style attribute is used in static use. The attribute value is a style resource file in XML format with < style > < / style > tags defined. In the < style > style label, the attribute parent = "Android: theme. Material" or its sub style name must be included.

To start the interface animation, first add a switch control to the style< Item name = "Android: windowsactivitytransitions" > true < / item > then rewrite the < item > tag in the style tag, control different animation objects according to the name attribute value, and specify different animation effects according to the < item > < / item > tag value. Here, the corresponding relation series of different animation objects are divided into the following table.

Since the interface animation is statically declared through style definition, it can also be used through code dynamic declaration in the method callback after the interface activity is created. The android.view.window class is used here. Because the style is modified before the interface loads the layout, the window object can be obtained by calling the GetWindow () method before the setcontentview () method.

First, you also need to call the requestfeature (window. Feature_content_transitions) method of the object to turn on the interface animation switch. After that, a series of methods of the object are called to control different animated objects, and different animation effects are specified according to the instance of the incoming Transition object. The corresponding relationships of different animation objects are shown in the following table.

For the animation of the shared view, you also need to mark the shared view in the two layout files respectively, and use the Android: transitionname attribute to specify the same shared view name.

After declaring the animation effect of the interface, it is also necessary to modify the original startactivity (intent, bundle) method to startactivity (intent, bundle) method where the interface is started as a new interface or where the interface starts other interfaces as an old interface. The intent parameter is also the intention information to start the interface before, and the bundle parameter is the newly added data package to control the interface animation. You can obtain the interface animation effect declared by the current interface through activityoptions. Makescenetransitionanimation (this). Tobundle() series methods, and the result value can be passed in as an android.os.bundle object.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>