SoFunction
Updated on 2025-04-09

Android implements transparent animation

This article shares the specific code for Android to implement transparent animation for your reference. The specific content is as follows

There is an Activity on the home page

public class AlphaAnimationActivity extends AppCompatActivity {
 
 private ImageView mImageView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_frame_animation);
  // Just a normal picture  mImageView = findViewById();
  
  ... These are several buttons Used to start animation
 
  
 }

1 Tween animation method

1.1 xml method

Directory folder res/anim/

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:andro>
    <alpha
        android:duration="3000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0">

    </alpha>
</set>

Then use it in Java code as follows:

//Tween animation method - xmlpublic void start1() {
 //Load the animation xml Animation lAnimation = (this, );
 //Set and start the startup picture (lAnimation);
 
}

1.2 Java code method

//Tween animation method - javaprivate void start2() {
 //Create transparent animation Animation lAnimation = new AlphaAnimation(0.0f, 1.0f);
 //Set animation time (3000);
 //Set animation (lAnimation);
}

2 attribute animation method

1.1 ValueAnimator xml method

Directory folder res/animator/alpha_animator.xml

<animator xmlns:andro
    android:valueFrom="0"
    android:valueTo="255"
    android:duration="2000"
    android:valueType="intType"/>

Then in the code

//Attribute animation method - ValueAnimator - xmlpublic void start3() {
 // Load XML animation ValueAnimator animator = (ValueAnimator) (this, .alpha_animator);
 (new () {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
   int currentValue = (int) ();
   ("Attribute Animation", "onAnimationUpdate: " + ());
   // Assign the changed value to the object's attribute value, the following will explain in detail   (currentValue);
   //Refresh the view, that is, repaint, thereby achieving animation effect   ();
  }
 });
 // Start the animation ();
}

1.2 ValueAnimator java code method

//Attribute animation method - ValueAnimator - javapublic void start4() {
 // Step 1: Set the initial value & end value of the animation attribute // ofInt() has two functions // 1. Create an animation instance // 2. Smoothly transition multiple Int parameters: 0 and 1 are passed here, indicating that the value is smoothly transitioned from 0 to 255 // If 3 Int parameters a, b, c are passed in, then smoothly transition from a to b, then smoothly transition from b to c, and so on ValueAnimator anim = (0, 255);
 // Set the duration of the animation (500);
 // Set animation delay playback time (500);
 // Set the number of repeated playbacks of animation = number of replays +1 // Number of animation playback times = infinite, animation is repeated infinitely (0);
 // Set the repeating animation mode // (Default): Replay in the positive order // :Reverse playback ();
 

 // Step 2: Manually assign the changed value to the object's attribute value: update the listener through the animation // Set the value update listener // That is: the method will be called once every time the value changes or changes once. (new () {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
   
   int currentValue = (Integer) ();
   // Get the changed value   (currentValue);
   // Output the changed value   
   // Step 4: Assign the changed value to the object's attribute value, the following will explain in detail   (currentValue);
   
   // Step 5: Refresh the view, that is, repaint, so as to achieve animation effect   ();
   
  }
 });
 //Step 3 Start the animation ();
 // Start the animation // The ValueAnimator class changes the value first, and then manually assigns the value to the object's properties to realize animation; it is indirectly operates on the object's properties // The ValueAnimator class is essentially an operation mechanism that changes values}

1.3 ObjectAnimator xml method

Directory folder animator/alpha_object_animator.xml

<objectAnimator xmlns:andro
    android:propertyName="alpha"
    android:valueFrom="1"
    android:valueTo="0"
    android:duration="2000"
    android:valueType="floatType"
    >
</objectAnimator>
//Attribute animation method - ObjectAnimator - xmlpublic void start5() {
 // Load XML animation Animator animator = (this, .alpha_object_animator);
 
 // Set an animation object (mImageView);
 
 // Start the animation ();
 
 ("Animation","ObjectAnimator - xml");
 
}

1.4 ObjectAnimator java code method

//Attribute animation method - ObjectAnimator - javapublic void start6() {
 ObjectAnimator anim = (mImageView, "alpha", 1f, 0f, 1f);
 // means: // The animation object is mButton // The property of the object used by the animation is transparency alpha // The animation effect is: regular - fully transparent - regular // ofFloat() has two functions (500);
 // Set the duration of the animation 
 (500);
 // Set animation delay playback time 
 (0);
 // Set the number of repeated playbacks of animation = number of replays +1 // Number of animation playback times = infinite, animation is repeated infinitely 
 ();
 // Set the repeating animation mode // (Default): Replay in the positive order // :Reverse playback ();
 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.