Android动画之补间动画(Tween Animation)基础学习

前言

之前说过了在Android中,动画Animation的实现有两种方式:Tween Animation(渐变动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。

小编也和大家分享了逐帧动画的基础知识,下面我们就来学习下Android中逐帧动画的基础知识。

原理 : 给出开始和结束两个关键帧,两个关键帧之间的插补帧是由计算机自动运算而得到的。

分类 : AlphaAnimation(透明度) ScaleAnimation(缩放) TranslateAnimation(位移) RotateAnimation (旋转) AnimationSet(组合)

方式 :

1.在代码中new

2.在anim文件夹下定义动画xml资源

效果

Android动画之补间动画(Tween Animation)基础学习

代码

第一步 :准备动画资源

目录

Android动画之补间动画(Tween Animation)基础学习

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="2000"
 android:fromAlpha="1.0"
 android:interpolator="@android:anim/linear_interpolator"
 android:toAlpha="0.3">

</alpha>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:interpolator/linear"
 android:repeatCount="infinite"
 android:repeatMode="reverse"
 android:duration="2000"
 android:fromDegrees="0"
 android:toDegrees="1080">
 android:pivotX="50%"
 android:pivotY="50%"
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/linear_interpolator"
 android:duration="2000"
 android:fillAfter="true"
 android:fromXScale="1.0"
 android:fromYScale="1.0"
 android:pivotX="50%"
 android:pivotY="50%"
 android:toXScale="0.3"
 android:toYScale="0.3">

</scale>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator"
  android:duration="2000"
  android:fromXDelta="10"
  android:fromYDelta="10"
  android:toXDelta="300"
  android:toYDelta="300">

</translate>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/linear_interpolator"
 android:duration="2000">

 <alpha android:fromAlpha="0.3"
  android:toAlpha="1.0"/>

 <rotate android:fromDegrees="0"
  android:toDegrees="360"
  android:pivotX="0"
  android:pivotY="0"
  android:repeatMode="restart"
  android:repeatCount="infinite"/>
</set>

第二步 :activity_main.xml ( 略 )

第三步 :MainActivity.java

package com.lyp.anim;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 private Button btnScale;
 private Button btnRotate;
 private Button btnTranslate;
 private Button btnAlpha;
 private Button btnAll;

 private ImageView mImage;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 initView();
 }

 private void initView() {
 btnScale= (Button) findViewById(R.id.btn_scale);
 btnRotate= (Button) findViewById(R.id.btn_rotate);
 btnTranslate= (Button) findViewById(R.id.btn_translate);
 btnAlpha= (Button) findViewById(R.id.btn_alpha);
 btnAll= (Button) findViewById(R.id.btn_all);

 mImage= (ImageView) findViewById(R.id.image);

 btnScale.setOnClickListener(this);
 btnRotate.setOnClickListener(this);
 btnTranslate.setOnClickListener(this);
 btnAlpha.setOnClickListener(this);
 btnAll.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()){
  case R.id.btn_scale:
  //加载缩放动画
  Animation scale = AnimationUtils.loadAnimation(this, R.anim.scale);
  scale.setFillAfter(true); //保留动画结束状态,在xml文件中设置无效!!
  mImage.startAnimation(scale);
  break;
  case R.id.btn_rotate:
  //加载旋转动画
  Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
  mImage.startAnimation(rotate);
  break;
  case R.id.btn_translate:
  //加载位移动画
  Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
  mImage.startAnimation(translate);
  break;
  case R.id.btn_alpha:
  //加载透明度渐变动画
  Animation alpha = AnimationUtils.loadAnimation(this, R.anim.alpha);
  mImage.startAnimation(alpha);
  break;
  case R.id.btn_all:
  //加载组合动画
  Animation all = AnimationUtils.loadAnimation(this, R.anim.all);
  mImage.startAnimation(all);
  break;
 }
 }
}

总结

以上Android中补间动画(Tween Animation)基础的全部内容了,动画Animation实现的两种方式小编现在已经都给大家分享了,希望能对各位Android开发者们有所帮助,如果有疑问大家可以留言交流。