Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize rotation Animation with Android

Shulou Source: shulou.com Published: 2022-06-02 04:08:03 10月04日 Update

This article mainly talks about "how to achieve rotation animation in Android". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve rotation animation with Android".

1. Preparatory work

First, you need to have a picture to rotate.

You need to think about how to start, end, accelerate, and slow down.

2. Principle of acceleration and deceleration

This animation uses RotateAnimation, and the parameters required for initialization are as follows

Public RotateAnimation (float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType, float pivotYValue) {mFromDegrees = fromDegrees;// start angle mToDegrees = toDegrees;// end angle mPivotXValue = pivotXValue;// determine the type of x-axis coordinates mPivotXType = pivotXType;//x axis value, 0.5f indicates that the type of y-axis coordinates is determined by half the length of the control itself as x-axis mPivotYValue = pivotYValue;// mPivotYType = pivotYType / / the value of the y-axis, 0.5f indicates that half the length of the control itself is the y-axis initializePivotPoint ();}

The so-called rotation animation, in essence, is to specify rotation within a certain period after the above object is initialized.

The so-called acceleration essentially means changing the rotation angle within a set period.

Or modify the period to rotate a certain angle within the preset period.

In short, the angle and cycle must change, which can determine the speed of the animation.

For example, from 360 degrees in 2 seconds to 360 degrees in 1 second is a kind of acceleration, and from 360 degrees in 2 seconds to 720 degrees in 2 seconds is also a kind of acceleration.

On the contrary, it is to slow down.

3. Initialize RotateAnimation rotate = new RotateAnimation (0f, 360 f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotate.setInterpolator (new LinearInterpolator ()); rotate.setDuration (2000); / / set the animation duration period rotate.setRepeatCount (- 1); / / set the number of repeats / / rotate.setFillAfter (true) / / whether the animation remains in the finished state after execution rotate.setStartOffset (10); / / waiting time before execution 4, start start.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {fan.startAnimation (rotate);}}); 5. Acceleration

First, you need to create a global variable

Private int duration=2000

Accelerated exampl

Accelerate.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (duration > 10) {duration/=2; / / period divided by 2 angles invariant acceleration (need to consider extremes, so add a judgment)} rotate.setDuration (duration) / / set cycle fan.startAnimation (rotate); / / start rotation}}); 6. Decelerate decelerate.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (duration10) {duration/=2;} rotate.setDuration (duration); fan.startAnimation (rotate)) ); decelerate.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (duration)

Tags: Period animation angle that is content picture coordinate control time essence extreme number of times state type part length fan learning practical ordinary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Shulou Technology Shulou Tech Info vpn