目录:
1.android动画导图
2.View Animation(视图动画)
(1)Tween Animation(补间动画)
(2)Frame Animation(帧动画)
3.Property Animation(属性动画)
(1)属性动画概述
(2)相关类继承关系
(3)相关类、接口概述导图
(4)ValueAnimator类
(5)ObjectAnimator类
(6)TypeEvaluator接口相关类引入
(7)xml实现属性动画
4.Interpolator(插值器)
(1)插值器的作用与概述
(2)常见的插值器
(3)xml中设置插值器
(4代码中设置插值器
5.AnimationSet与AnimatorSet(动画集合)
6.自定义TypeEvaluator和属性实现属性动画总结案例
1.android动画导图
通过导图我们可以知道动画的实现可以通过java代码和xml文件两种方式实现,下面再学习每种动画的时候
我们试着用两种方法取实现。
2.View Animation(视图动画)
2.1 Tween Animation补间动画
(1)透明度(alpha)动画实现=淡入淡出动画
布局文件(两种实现方式一致):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.animation.MainActivity">
<ImageView
android:id="@+id/alphaImage"
android:src="@drawable/weimei"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
java代码实现:
package com.example.animation;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView alphaImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定布局控件
alphaImage = (ImageView) findViewById(R.id.alphaImage);
//实例化透明度动画,设置透明度从1f~0f
AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
//设置动画时间
alphaAnimation.setDuration(2000);
//设置动画结束后保持结束时的动画状态
//alphaAnimation.setFillAfter(true);
//设置结束时保存开始时的状态
alphaAnimation.setFillBefore(true);
//设置插值器
//alphaAnimation.setInterpolator();
//设置动画循环次数
alphaAnimation.setRepeatCount(2);
//设置循环模式REVERSE|RESTART|其他
alphaAnimation.setRepeatMode(Animation.REVERSE);
//将动画设置到控件
alphaImage.startAnimation(alphaAnimation);
}
}
xml文件实现:
在src/anim/下建文件alpha_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:fillBefore="true"
android:repeatCount="2"
android:repeatMode="reverse"
/>
</set>
<!--
常用属性设置:
android:fromAlpha="1.0"//开始时候的透明度
android:toAlpha="0.0"//结束的透明度
android:fillAfter="true"//动画结束后保持最后的状态
android:fillBefore="true"//动画结束后回复一开始的状态
android:fillEnabled=""//效果同fillBefore
android:interpolator=""//设置动画的插值器
android:startOffset=""//设置动画开始后延迟时长度,单位毫秒
android:detachWallpaper //设置是否在壁纸上运行
android:repeatCount=""///设置动画运行次数
android:zAdjustment="normal|top|bottom"//设置动画在运行时在Z轴的位置
android:repeatMode="reverse|restart"//设置重复模式,倒叙|从头
-->
java代码引入
package com.example.animation;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView alphaImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定布局控件
alphaImage = (ImageView) findViewById(R.id.alphaImage);
//xml定义动画,然后通过AnimationUtils.loadAnimation(Context context, int id)加载
Animation alphaAnimationX = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.alpha_animation);
//开启动画
alphaImage.startAnimation(alphaAnimationX);
}
}
上面是通过java代码和xml布局两种方式进行动画实现,下面就只使用xml布局实现了,有兴趣的朋友可以试试用java代码取实现
,在实际开发中其实也比较推荐xml布局文件吧,毕竟它比较简洁直观,易于管理,同时也满足逻辑与布局分离的分层架构思想。
(2)旋转(rotate)动画实现
在src/anim/下建文件rotate_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="180"
android:pivotX="0"
android:pivotY="0"
android:fillAfter="true"
android:repeatCount="4"
android:repeatMode="reverse"
android:duration="2000"/>
</set>
<!--
常用属性设置:
android:fromDegrees="0"//旋转开始角度
android:toDegrees="180"//旋转结束角度
android:pivotX="50%"//旋转中心点X坐标,百分数表示(控件长度*百分数)的的位置,如果是数值表示从控件左上角开始计算,
android:pivotY="50%"//旋转中心点Y坐标
android:fillAfter="true"//动画结束后保持最后的状态
android:fillBefore="true"//动画结束后回复一开始的状态
android:fillEnabled=""//效果同fillBefore
android:interpolator=""//设置动画的插值器
android:startOffset=""//设置动画开始后延迟时长度,单位毫秒
android:detachWallpaper //设置是否在壁纸上运行
android:repeatCount=""///设置动画运行次数
android:zAdjustment="normal|top|bottom"//设置动画在运行时在Z轴的位置
android:repeatMode="reverse|restart"//设置重复模式,倒叙|从头
-->
java代码引入
package com.example.animation;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by elimy on 2016-10-20.
*/
public class RotateActivity extends AppCompatActivity {
private ImageView rotateImage;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotate);
//绑定控件
rotateImage = (ImageView) findViewById(R.id.rotateImage);
//加载xml动画文件
Animation rotateAnimation = AnimationUtils.loadAnimation(
RotateActivity.this,R.anim.rotate_animation);
//启动动画
rotateImage.startAnimation(rotateAnimation);
}
}
(3)缩放(scale)动画实现
java代码引入部分的代码和上面的都一致,就不重复贴出来了,下面是xml布局实现缩放动画的代码
在src/anim/下建文件scale_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.5"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:repeatCount="4"
android:repeatMode="reverse"
android:duration="2000"/>
</set>