Transparent animation for Android

Time:2021-7-28

This example shares the specific code of Android transparent animation for your reference. The specific contents are as follows

There is an activity on the home page

public class AlphaAnimationActivity extends AppCompatActivity {
 
 private ImageView mImageView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_frame_animation);
  //It's just an ordinary picture
  mImageView = findViewById(R.id.imageview);
  
  ... here are some buttons to start the animation
 
  
 }

1. Make up animation mode

1.1 XML mode

Directory folder RES / anim / alpha.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="3000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0">

    </alpha>
</set>

Then use the following in Java code:

//Patch animation method - XML
public void start1() {
 //Load animation XML
 Animation lAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
 //Set and turn on animation
 mImageView.startAnimation(lAnimation);
 
}

1.2 java code mode

//Make up animation mode - Java
private void start2() {
 //Create transparent animation
 Animation lAnimation = new AlphaAnimation(0.0f, 1.0f);
 //Set animation time
 lAnimation.setDuration(3000);
 //Animate
 mImageView.startAnimation(lAnimation);
}

2 attribute animation mode

1.1 valueanimator XML mode

Directory folder RES / Animator / alpha_ animator.xml


<animator xmlns:android="http://schemas.android.com/apk/res/android"
    android:valueFrom="0"
    android:valueTo="255"
    android:duration="2000"
    android:valueType="intType"/>

Then in the code

//Attribute animation mode - valueanimator - XML
public void start3() {
 //Load XML animation
 ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.alpha_animator);
 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
   int currentValue = (int) animation.getAnimatedValue();
   Log. D ("attribute animation", "onanimationupdate:" + animation. Getanimatedvalue());
   //Assign the changed value to the attribute value of the object, which will be described in detail below
   mImageView.setImageAlpha(currentValue);
   //Refresh the view, that is, redraw, for animation
   mImageView.requestLayout();
  }
 });
 //Start animation
 animator.start();
}

1.2 valueanimator java code mode

//Attribute animation mode - valueanimator - Java
public void start4() {
 //Step 1: set the initial and end values of animation attributes
 //Ofint() has two functions
 //1. Create an animation instance
 //2. Smooth transition of multiple int parameters passed in: pass in 0 and 1 here, which means smooth transition of values from 0 to 255
 //If three int parameters a, B and C are passed in, the transition from a to B, then from B to C, and so on
 ValueAnimator anim = ValueAnimator.ofInt(0, 255);
 //Sets how long the animation runs
 anim.setDuration(500);
 //Set animation delay playback time
 anim.setStartDelay(500);
 //Set the number of repetitions of the animation = number of repetitions + 1
 //When the number of animation playback = infinite, the animation repeats indefinitely
 anim.setRepeatCount(0);
 //Set repeat animation mode
 //Valueanimator.restart (default): playback in positive order
 //Valueanimator.reverse: reverse playback
 anim.setRepeatMode(ValueAnimator.RESTART);
 

 //Step 2: manually assign the changed value to the attribute value of the object: update the listener through animation
 //Update listener for setting values
 //That is, the method will be called once every time the value changes
 anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
   
   int currentValue = (Integer) animation.getAnimatedValue();
   //Get the changed value
   System.out.println(currentValue);
   //Output changed value
   
   //Step 4: assign the changed value to the attribute value of the object, which will be described in detail below
   mImageView.setImageAlpha(currentValue);
   
   //Step 5: refresh the view, that is, redraw to achieve the animation effect
   mImageView.requestLayout();
   
  }
 });
 //Step 3 start animation
 anim.start();
 //Start animation
 //Valueanimator class is to change the value first, and then manually assign it to the attribute of the object to realize animation; It is an indirect operation on object properties
 //The valueanimator class is essentially an operating mechanism for changing values
}

1.3 objectanimator XML mode

Directory folder animator / alpha_ object_ animator.xml


<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="alpha"
    android:valueFrom="1"
    android:valueTo="0"
    android:duration="2000"
    android:valueType="floatType"
    >
</objectAnimator>
//Attribute animation mode - objectanimator - XML
public void start5() {
 //Load XML animation
 Animator animator = AnimatorInflater.loadAnimator(this, R.animator.alpha_object_animator);
 
 //Animate objects
 animator.setTarget(mImageView);
 
 //Start animation
 animator.start();
 
 Log. D ("animation", "objectanimator - XML");
 
}

1.4 objectanimator java code mode

//Attribute animation mode - objectanimator - Java
public void start6() {
 ObjectAnimator anim = ObjectAnimator.ofFloat(mImageView, "alpha", 1f, 0f, 1f);
 //Means:
 //The animation object is mbutton
 //The attribute of the animated object is transparency alpha
 //The animation effect is: General - fully transparent - General
 //There are two functions of float()
 anim.setDuration(500);
 //Sets how long the animation runs
 
 anim.setStartDelay(500);
 //Set animation delay playback time
 
 anim.setRepeatCount(0);
 //Set the number of repetitions of the animation = number of repetitions + 1
 //When the number of animation playback = infinite, the animation repeats indefinitely
 
 anim.setRepeatMode(ValueAnimator.RESTART);
 //Set repeat animation mode
 //Valueanimator.restart (default): playback in positive order
 //Valueanimator.reverse: reverse playback
 anim.start();
 
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]