一.Android动画类型
Android的animation由四种类型组成:
XML中
alph | 渐变透明度动画效果 |
scale | 渐变尺寸伸缩动画效果 |
translate | 画面转换位置移动动画效果 |
rotate | 画面转移旋转动画效果 |
JavaCode中
AlphaAnimation | 渐变透明度动画效果 |
ScaleAnimation | 渐变尺寸伸缩动画效果 |
TranslateAnimation | 画面转换位置移动动画效果 |
RotateAnimation | 画面转移旋转动画效果 |
二.Android动画模式
Animation主要有两种动画模式:
一种是tweened animation(渐变动画)
XML中 | JavaCode |
alpha | AlphaAnimation |
scale | ScaleAnimation |
一种是frame by frame(画面转换动画)
XML中 | JavaCode |
translate | TranslateAnimation |
rotate | RotateAnimation |
三.Android动画实现
1.通过代码实现动画效果
(1)透明度渐变效果
//1.创建透明度动画对象,数值越小越透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.1f);
//设置动画的持续时间
alphaAnimation.setDuration(3000);
//设置是否保留最终状态
// alphaAnimation.setFillAfter(true);
//设置重复次数,填-1无限循环
alphaAnimation.setRepeatCount(2);
//设置动画的重复模式,默认是Restart,Reverse是反方向执行
alphaAnimation.setRepeatMode(Animation.REVERSE);
//通过控件启动动画
imageView.startAnimation(alphaAnimation);
(2)缩放效果
//相对控件自己的左上角为原点缩放
// ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 2, 0.2f, 2);
//相对点(200,300)进行缩放
// ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,2,0.2f,2,200,300);
//相对控件自己的中心点进行缩放
// ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,2,0.2f,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//相对父容器的中心点进行缩放
// fromX:开始缩放的X轴倍数。如1.0f:本身大小;如2.0f:从自己两倍开始
// toX:结束缩放的X轴倍数。同上...
// fromY:始缩放的Y轴倍数。
// toY:结束缩放的Y轴倍数。
/** pivotXType:X轴缩放中心点类型;可选值有:
Animation.RELATIVE_TO_SELF相对自己--常用
Animation.RELATIVE_TO_PARENT相对父窗体
Animation.ABSOLUTE 绝对的---不常用
*/
// pivotXValue:在pivotXType的基础上,X轴缩放中心的位置。如:0.5f:缩放中心就在控件的一半的位置。如果是0.0f,则会在控件本身的左边位置
// pivotYType:X轴缩放中心点类型;同上 ...
// pivotYValue:在pivotYType的基础上,Y轴缩放中心的位置。
ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 2, 0.5f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
imageView.startAnimation(scaleAnimation);
(3)旋转效果
//相对自己的左上角旋转,正数代表顺时针,负数逆时针
RotateAnimation rotateAnimation = new RotateAnimation(0,-180);
//相对(200,300)点旋转
//RotateAnimation rotateAnimation = new RotateAnimation(0,-180,200,300);
rotateAnimation.setDuration(3000);
imageView.startAnimation(rotateAnimation);
(4)平移效果
/*new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
例如:从控件自己的位置开始,向下移动自己的一倍距离。*/
//TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
// Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f);
// TranslateAnimation translateAnimation = new TranslateAnimation(0,200,0,0);
//获得屏幕宽度:
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int imgeWidth = imageView.getWidth();
//TranslateAnimation translateAnimation = new TranslateAnimation(0,screenWidth-imgeWidth,0,0);
TranslateAnimation translateAnimation = new TranslateAnimation(0,200,0,0);
translateAnimation.setDuration(3000);
translateAnimation.setFillAfter(true);
imageView.startAnimation(translateAnimation);
(5)动画集合效果
@Override
public void onCreate(Bundle savedInstanceState) { //重载onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象
Button btn1=(Button)findViewById(R.id.button1); //按钮对象
Button btn2=(Button)findViewById(R.id.button2);
final Animation translateAnimation=new TranslateAnimation(0,300,0,300); //设置位置变化动画
final Animation scaleAnimation = new
ScaleAnimation(0f,1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//设置尺寸变化动画
final Animation alphaAnimation=new AlphaAnimation(0.1f,1.0f); //设置透明度变化动画
btn1.setOnClickListener(new View.OnClickListener() { //设置监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
translateAnimation.setDuration(10000); //设置位置变化动画的持续时间
scaleAnimation.setDuration(10000); //设置尺寸变化动画的持续时间
alphaAnimation.setDuration(10000); //设置透明度渐变动画的持续时间
AnimationSet set=new AnimationSet(true); //创建动画集对象
set.addAnimation(translateAnimation); //添加位置变化动画
set.addAnimation(scaleAnimation); //添加尺寸变化动画
set.addAnimation(alphaAnimation); //添加透明度渐变动画
set.setFillAfter(true); //停留在最后的位置
set.setFillEnabled(true);
image.setAnimation(set); //设置动画
set.startNow(); //启动动画
}
});
btn2.setOnClickListener(new View.OnClickListener() { //设置监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
set.cancel(); //取消动画执行
}
});
}
2.通过xml实现动画效果
(1)透明度渐变效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:repeatMode="reverse">
<alpha android:fromAlpha="0.2" android:toAlpha="1"
android:repeatCount="infinite"/>
</set>
(2)缩放效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000">
<scale android:fromXScale="0.2" android:toXScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:fromYScale="0.2" android:toYScale="1"/>
</set>
(3)旋转效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:repeatMode="reverse">
<alpha android:fromAlpha="0.2" android:toAlpha="1"
android:repeatCount="infinite"/>
<rotate android:fromDegrees="0" android:toDegrees="180"
android:pivotX="50%p" android:pivotY="50%p"/>
<!--android:pivotX="50%p" 50%p代表的是相对父容器的中心点旋转
android:pivotX="50%" 50%代表的是相对自己的中心点旋转
-->
</set>
(4)平移效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true"
android:interpolator="@android:anim/overshoot_interpolator">
<!--
android:fillAfter="true" 设置是否保留最终状态 如果是集合,需要在set节点中设置
android:interpolator="@android:anim/overshoot_interpolator" 如果有集合,插值器要放在集合的属性中-->
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="400"
android:toYDelta="0"
>
<!-- android:repeatCount="infinite" 无限重复 只能在子节点中单独设置-->
</translate>
</set>
(5)加载xml动画效果
//加载动画资源
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translation_anim);
//开启动画
imageView.startAnimation(animation);