Android Realizes Transparent Animation

  • 2021-12-13 09:09:07
  • OfStack

In this paper, we share the specific code of Android to realize transparent animation for your reference. The specific contents are as follows

Home page has 1 Activity


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 1 An ordinary picture 
  mImageView = findViewById(R.id.imageview);
  
  ...  Here are several buttons   Used to start animation 
 
  
 }

1 complement 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:


// Interval animation mode  - 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


// Interval animation mode  - java
private void start2() {
 // Create Transparent Animation 
 Animation lAnimation = new AlphaAnimation(0.0f, 1.0f);
 // Set animation time 
 lAnimation.setDuration(3000);
 // Set animation 
 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"/>

And 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 property value of the object, which will be explained in detail below 
   mImageView.setImageAlpha(currentValue);
   // Refresh the view, that is, redraw it to achieve animation effect 
   mImageView.requestLayout();
  }
 });
 //  Start animation 
 animator.start();
}

1.2 ValueAnimator java code mode


//属性动画方式 - ValueAnimator - java
public void start4() {
 // 第1步:设置动画属性的初始值 & 结束值
 // ofInt()作用有两个
 // 1. 创建动画实例
 // 2. 将传入的多个Int参数进行平滑过渡:此处传入0和1,表示将值从0平滑过渡到 255
 // 如果传入了3个Int参数 a,b,c ,则是先从a平滑过渡到b,再从b平滑过渡到 c,以此类推
 ValueAnimator anim = ValueAnimator.ofInt(0, 255);
 // 设置动画运行的时长
 anim.setDuration(500);
 // 设置动画延迟播放时间
 anim.setStartDelay(500);
 // 设置动画重复播放次数 = 重放次数+1
 // 动画播放次数 = infinite时,动画无限重复
 anim.setRepeatCount(0);
 // 设置重复播放动画模式
 // ValueAnimator.RESTART(默认):正序重放
 // ValueAnimator.REVERSE:倒序回放
 anim.setRepeatMode(ValueAnimator.RESTART);
 

 // 第2步:将改变的值手动赋值给对象的属性值:通过动画的更新监听器
 // 设置 值的更新监听器
 // 即:值每次改变、变化1次,该方法就会被调用1次
 anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
   
   int currentValue = (Integer) animation.getAnimatedValue();
   // 获得改变后的值
   System.out.println(currentValue);
   // 输出改变后的值
   
   // 步骤4:将改变后的值赋给对象的属性值,下面会详细说明
   mImageView.setImageAlpha(currentValue);
   
   // 步骤5:刷新视图,即重新绘制,从而实现动画效果
   mImageView.requestLayout();
   
  }
 });
 //第3步 启动动画
 anim.start();
 // 启动动画
 // ValueAnimator 类是先改变值,然后 手动赋值 给对象的属性从而实现动画;是 间接 对对象属性进行操作
 // ValueAnimator 类本质上是1种 改变 值 的操作机制
}

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);
 
 //  Setting Animation 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);
 //  Represents the :
 //  Animation action object is mButton
 //  The attribute of the animated object is transparency alpha
 //  The animation effect is : Routine  -  Full transparency  -  Routine 
 // ofFloat() There are two functions 
 anim.setDuration(500);
 //  Set how long the animation runs 
 
 anim.setStartDelay(500);
 //  Set the animation delay playback time 
 
 anim.setRepeatCount(0);
 //  Set the number of animation repetitions  =  Number of replays +1
 //  Number of animations played  = infinite Hour , Infinite repetition of animation 
 
 anim.setRepeatMode(ValueAnimator.RESTART);
 //  Set Repeat Animation Mode 
 // ValueAnimator.RESTART( Default ): Positive sequence playback 
 // ValueAnimator.REVERSE: Reverse playback 
 anim.start();
 
}

Related articles: