Basic animation implementation on android studio (article 1), androidstudio

Source: Internet
Author: User

Basic animation implementation on android studio (article 1), androidstudio

Hello, ladies and gentlemen, when many of my friends are learning android, there are often some projects that need to be inserted with basic animations. How can they be implemented? Next we will analyze several basic animations in android.

(Here we use Android Studio, which is more intelligent than eclipse by default. Ps: The method is the same. There is no difference. Don't be surprised if you like eclipse .)

Before learning about android animation, we need to know that the animation in android is generally related to event operation processing, for example, triggered by an event. Now we will officially start:

First, we need to know that there are four types of basic animations in android:

1: Tween Animation. Transform Animation

2: Layout Animation

3: Frame Animation. Frame Animation

4: Property Animation

In this article, first learn the first two methods.

Second: animation Learning

I. Tween Animation transform Animation:

There are four basic types of transform Animation:

1: Alpha: gradient transparency animation type

2: Scale: gradient size animation type

3: Translate: animation type of position Movement

4: Rotate: rotation animation type

The four methods have the same attributes. The attributes are as follows:

1. Duration: Specifies the animation Duration in milliseconds. Generally, in java, the animation Duration is represented by a button. setDuration (1000); (the button is a reference, 1000 is a millisecond, that is, the animation duration .)

2. fillAfter: indicates that the animation is applied after the animation ends. For example, if the animation may run as a translation in the Translate, The fillAfter determines the position of the animation object after the animation ends. The return value is a boolean value. If it is true, it indicates that the animation ends at the end position. If it is false, it indicates that the animation ends and jumps back to the position before the animation starts.

3. fillBefore: the position before the animation starts. It can be understood by fillAfter.

4. interpolator: animation insertor (used to accelerate an animation and slow down the insertor)

5: repeatcount: specifies the number of animation repetitions.

6: repeatmode: determines whether the animation repetition is sequential or reverse.

7. startoffset: interval between an animation and an animation. It is generally used for continuous playback of two or more animations. You can also set a listener to replace this function)

In addition to java, the animation can also be implemented by calling layout (as described below)

In java, there are several types:

1. AlphaAnimation has the fromAlpha parameter: transparency at the start. toAlpha: Transparency after the end is expressed by f (0.0f to 1.0f.

2. ScaleAniamtion includes fromX, toX, fromY, toY, pirotX, priotY, and other parameters, indicating the coordinates of start and end, and x, y coordinate.

3. TranslateAnimation has the fromxDelta parameter. fromyDelta is the starting coordinate of x and y before moving. toxDelta and toyDelta are the x and y coordinates at the end, respectively.

4. RotateAnimation includes the fromDegrees start angle, toDegrees end angle, and pirotX and priotY parameters.

Well, let's use an example to explain it:

First, we use Java to implement a simple Alpha animation,

 

1 Animation zhangch = new AlphaAnimation(0.1f,1.0f);2 zhangch.setDuration(5000);3 image.startAnimation(zhangch);

 

Because the parameters in the first line use floating point numbers, the f letter must be added to each number, the first one means that the animation object starts from the transparency of 0.1 to the transparency of the second parameter 1.0, 1.0 is the normal photo, and 0.0 is all transparent.

The second line is to set the animation runtime to 2 seconds, 2000 milliseconds.

The third line refers to the animation of the object image.

However, in android, besides java pure code, you can also use the res file to create a layout file for implementation,

1 Animation zhangch = AnimationUtils.loadAnimation(this, R.anim.scale);2 image.startAnimation(zhangch);

The first line of AnimationUtils is a tool class that comes with the system. loadAnimation is the loading animation. The parameters in it refer to the scale file in the anim folder in the res folder called from the current interface, the first is the res File Settings, and the second is the layout in the scale:

 

The example is as follows:

Perform an animation demonstration of the icons above as shown in the UI,

Here, continue_btn and continue_btn2 are the combination of two basic animations. Let's take a look at the required structure.

 

A total of two java files and two layout files are required. Here we only use java code to implement animations without calling layout Because java code animations can be more adapted to dynamic transformation and management, as a code person, ensure that the Code is preferred.

Layout

Then you can set two relativelayout parameters. Remember to set the id name.

The following code is used:

 

Here we need a simple explanation for the implementation of this continuous Animation: we use a listener setting process, and write two animations in the content executed in case, start an Animation and set an Animation.

Listener, the system will automatically generate the following three States. We can literally see that as long as you add a description to the execution command of this ending state, you can start another animation to play two consecutive animations. If there are more than one animation, you can set more.

 

Of course, there are also commonly used animations when one page jumps to another, as shown below:

In overridePendingTransition, the first parameter indicates the animation of the first activity exiting, and the second parameter indicates the animation of the second activity entering. layout call is used here,

Its rotate layout is as follows:

Interpolator is the animation plug-in the previously mentioned attributes.

 

Well, this is the first basic animation implementation. Next we will talk about the second basic animation, FrameAnimation.

The principle of frame-by-frame animation is to play a very picture object at a certain speed, which makes people feel like continuous animation. Here is an example.

Create an xml file under drawable, write multiple objects in the list in the xml file, and add the specified values to it,

Put the code part and the third basic animation together, LayoutAnimation

The layout animation has the setOrder control sequence and LayoutAnimationCotroller layout controller.

For details, see the route in the example java as the layout animation name: and move as the frame-by-frame animation name.

 

Write down in a new Main3Activity:

 

And write the list file in the layout file.

To run.

All the code packages will be analyzed and downloaded below. The property animation is complicated. I will explain it in detail in the next lecture. I hope it will help you. Thank you, O (character _ blank) O.

Source android file address is https://pan.baidu.com/s/1jHEi9Hs

If you have any shortcomings, you are welcome to criticize and correct them.

 

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.