帧动画:通过顺序播放一系列图像从而产生动画效果
补间动画:是通过对场景里的对象不断做图像变换(透明度、缩放、平移、旋转)从而产生动画效果
属性动画:通过动态的改变对象的属性从而达到动画效果
帧动画
帧动画就是顺序播放一组预先定义好的图片
使用:1.在drawable目录下定义一个XML文件( android:oneshot=“false”: 表示是否重复播放动画,还是只播放一次;Drawable表示我们要播放的图片;duration表示这张图播放的时间)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/img1" android:duration="100"/>
<item android:drawable="@drawable/img2" android:duration="100"/>
<item android:drawable="@drawable/img3" android:duration="100"/>
</animation-list>
2.使用AnimationDrawable类播放第一步定义好的Drawable中的图片
Button button = (Button) findViewById(R.id.bt_001);
button.setBackgroundResource(R.drawable.animation1);//把Drawable设置为button的背景
//拿到这个我们定义的Drawable,实际也就是AnimationDrawable
AnimationDrawable animationDrawable = (AnimationDrawable) button.getBackground();
animationDrawable.start();//开启动画
view动画(补间动画)
补间动画的四种效果:平移,缩放,旋转,透明度
平移动画-translate-translateAnimation
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100" //X轴的移动起点
android:fromYDelta="0" //Y轴的移动起点
android:toXDelta="0" //X轴的移动终点
android:toYDelta="0"/> //Y轴的移动终点
Animation translateAnimation = new TranslateAnimation(fromXDelta ,toXDelta ,fromYDelta ,toYDelta);
translateAnimation.setDuration(3000);
mButton.startAnimation(translateAnimation);
缩放动画-scale-ScaleAnimation
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"//时间
android:fromXScale="0.0"//X轴的起始缩放倍数
android:fromYScale="0.0"//Y轴的起始缩放倍数
android:pivotX="50%"//动画结束后在X坐标的大小
android:pivotY="50%"//动画结束后在Y坐标的大小
android:toXScale="1.0"//缩放中心点的X坐标
android:toYScale="1.0"/>//缩放中心点的Y坐标
//效果:宽度从0.5到1.5,高度从0.0到1.0,缩放的圆心为顶部中心点,延迟1s开始,持续2s
ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f,1.5f,0.0f,1.0f,
Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0f);
scaleAnim.setDuration(2000);
旋转动画-rotate-rotateAnimation
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegree="0"//开始角度位置0
android:toDegree="1800"//结束角度位置,正数表示顺时针,负数表示逆时针。
android:pivotX = "50%"//旋转中心点的X轴坐标
android:pivotY="50%"//旋转中心点的Y轴坐标
android:duration = "3000"//时间
// 效果以图片中心点为中心,从负90度到正90度,持续5s
RotateAnimation rotateAnimation = new RotateAnimation(-90,90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(5000);
渐变动画-alpha/alphaAnimation
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"//时间
android:fromAlpha="1.0"//起始透明度
android:interpolator="@android:anim/accelerate_interpolator"//动画插入器
android:toAlpha="0.0" />//结束透明度
AlphaAnimation alphaAnimation =new AlphaAnimation(0f,1f);
alphaAnimation.setDuration(5000);
ivLauncher.startAnimation(alphaAnimation);
组合动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
>
<scale
android:duration="3000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"/>
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.5" />
<rotate
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX = "50%"
android:pivotY="50%"
android:duration = "3000"
/>
<translate
android:fromXDelta="0"
android:toXDelta="100"
android:fromYDelta="0"
android:toYDelta="100" />
</set>
AnimationSet setAnimation = new AnimationSet(true);
setAnimation.setRepeatMode(Animation.RESTART);
// 旋转动画
Animation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(1000);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
// 平移动画
Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT,-0.5f,
TranslateAnimation.RELATIVE_TO_PARENT,0.5f,
TranslateAnimation.RELATIVE_TO_SELF,0
,TranslateAnimation.RELATIVE_TO_SELF,0);
translate.setDuration(10000);
// 透明度动画
Animation alpha = new AlphaAnimation(1,0);
alpha.setDuration(3000);
alpha.setStartOffset(7000);
// 缩放动画
Animation scale1 = new ScaleAnimation(1,0.5f,1,0.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scale1.setDuration(1000);
scale1.setStartOffset(4000);
// 将创建的子动画添加到组合动画里
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(rotate);
setAnimation.addAnimation(translate);
setAnimation.addAnimation(scale1);
mButton.startAnimation(setAnimation);
属性动画
1.创建 ValueAnimator 或 ObjectAnimator 对象 —— 即可以从 XML 资源文件加载该动画也可以直接调用 ValueAnimator 或者 ObjectAnimator 的静态工厂方法创建动画。
2.根据需要为 Animator 对象设置属性。
3.如果需要监听 Animator 的动画开始事件,动画结束事件、动画重复事件、动画值改变事件,并根据事件提供响应处理代码,需要为Animator 对象设置监听器。
4.如果有多个动画需要同时播放,需要使用 AnimatorSet 组合这些动画。
5.调用 Animator 对象的 start 启动动画。
switch (v.getId()) {
case R.id.tranlate: //平移
ObjectAnimator translationY = ObjectAnimator.ofFloat(image, "translationY", new float[]{10f, 20f, 50f});
//时长
translationY.setDuration(2000);
//执行模式
translationY.setRepeatMode(ObjectAnimator.RESTART);
//重复次数
translationY.setRepeatCount(1);
//开始
translationY.start();
break;
case R.id.rotate: //旋转
ObjectAnimator rotationX = ObjectAnimator.ofFloat(image, "rotationX", 90f, 180f, 360f);
rotationX.setDuration(3000);
rotationX.setRepeatMode(ObjectAnimator.REVERSE);
rotationX.setRepeatCount(2);
rotationX.start();
break;
case R.id.alpha: //渐变 0.0是完全透明
ObjectAnimator alpha = ObjectAnimator.ofFloat(image, "alpha", 0.0f, 1.0f);
alpha.setDuration(2000);
alpha.setRepeatCount(3);
alpha.setRepeatMode(ObjectAnimator.REVERSE);
alpha.start();
break;
case R.id.scale://伸缩
ObjectAnimator scale = ObjectAnimator.ofFloat(image, "scaleX", 1f,4f,5f);
scale .setDuration(2000);
scale .setRepeatCount(3);
scale .setRepeatMode(ObjectAnimator.REVERSE);
scale .start();
break;
case R.id.animatorset://动画集合
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator sc = ObjectAnimator.ofFloat(image, "scaleX", 1f,4f,5f);
sc.setDuration(2000);
ObjectAnimator rotation = ObjectAnimator.ofFloat(image, "rotationX", 90f, 180f, 360f);
rotation.setDuration(2000);
animatorSet.playTogether(sc,rotation);
animatorSet.start();
break;
}
本文介绍了Android中三种主要的动画类型:帧动画,通过播放一系列图像实现;补间动画,通过连续变换对象属性如透明度、缩放等创造动画效果;属性动画,动态改变对象属性以实现更灵活的动画。详细讲解了每种动画的使用方法及其实现原理。
431

被折叠的 条评论
为什么被折叠?



