您的位置:首页 > 移动开发 > Android开发

Android动画

2020-07-13 05:11 459 查看

1.视图动画(View 动画/Tween(补间)动画)

作用于视图对象View,如TextView
可由XML或JAVA代码定义。建议使用XML文件,这种动画的XML定义方式文件一般放在res/anim/目录

1.1.AlphaAnimation(透明度渐变效果)

anim_alpha.xml:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>

fromAlpha:动画开始的透明度,取值:0-1,完全透明-完全不透明
toAlpha:动画结束的透明度,取值:0-1,完全透明-完全不透明
Duration:动画的运行时间(以毫秒为单位);必须设置
Interpolator:控制动画的变化速度
① AccelerateInterpolator:在动画开始的地方改变速度较慢,然后开
始加速
② AccelerateDecelerateInterpolator:在动画开始、结束的地方改
变速度较慢,中间时加速
③ CycleInterpolator:动画循环播放特定次数,变化速度按正弦曲线改
变: Math.sin(2 * mCycles * Math.PI * input)
④ DecelerateInterpolator:在动画开始的地方改变速度较快,然后开
始减速
⑤ AnticipateInterpolator:反向,先向相反方向改变一段再加速播放
⑥ AnticipateOvershootInterpolator:开始的时候向后然后向前甩一
定值后返回最后的值
⑦ BounceInterpolator: 跳跃,快到目的值时值会跳跃,如目的值100,
后面的值可能依次为85,77,70,80,90,100
⑧ OvershottInterpolator:回弹,最后超出目的值然后缓慢改变到目
的值
⑨ LinearInterpolator:动画以均匀的速度改变

1.2.ScaleAnimation(缩放渐变渐变效果)

anim_scale.xml:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"     // 沿着X轴缩放的起始比例 ,1.0表示无变化
android:toXScale="1.5"      // 沿着X轴缩放的结束比例
android:fromYScale="0.2"    // 沿着Y轴缩放的起始比例
android:toYScale="1.5"     // 沿着Y轴缩放的结束比例
android:pivotX="50%"       // 缩放的中轴点X/Y坐标,比如50%就是以图像的 中心为中轴点
android:pivotY="50%"
android:duration="2000"/>

1.3.TranslateAnimation(位移渐变)

anim_translate.xml:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"    // 动画起始位置的X坐标
android:toXDelta="320"   // 动画结束位置的X坐标
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>

1.4.RotateAnimation(旋转渐变)

anim_rotate.xml:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"    // 旋转的起始/结束角度
android:toDegrees="90"
android:duration="1000"
android:repeatCount="1"  // 旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次
// ,值为-1或者infinite时,表示动画永不停止
android:repeatMode="reverse"/>  // 设置重复模式,默认restart
1.5AnimationSet(组合渐变)
anim_set.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />

<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>

1.6为View动态设置动画效果

先调用AnimationUtils.loadAnimation(动画xml文件),然后View控件调用startAnimation(anim) 开始动画~这是静态加载的方式,当然你也可以直接创建一个动画对象,用Java代码完成设置,再调用 startAnimation开启动画~

Animation  animation = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
img_show.startAnimation(animation);

1.7取消动画

clearAnimation()取消
特别特别注意:补间动画执行之后并未改变View的真实布局属性值。

2.帧动画(Frame 动画、Drawable 动画)

帧动画非常容易理解,其实就是简单的由N张静态图片收集起来,然后我们通过控制依次显示 这些图片,因为人眼"视觉残留"的原因,会让我们造成动画的"错觉",跟放电影的原理一样!
这种动画的XML定义方式文件一般放在res/drawable/目录

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@mipmap/img_miao1"
android:duration="80" />
<item
android:drawable="@mipmap/img_miao2"
android:duration="80" />
<item
android:drawable="@mipmap/img_miao3"
android:duration="80" />
<!--限于篇幅,省略其他item,自己补上-->
...</animation-list>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
<ImageView
android:id="@+id/img_show"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:background=”@drawable/loading_animation" />
</LinearLayout>

开始动画:

AnimationDrawable anim = (AnimationDrawable) img_show.getBackground();
anim.start();

结束动画:

anim.stop();

3.Activity转场动画

Google在Android5.0之后,又推出的新的转场动画。
Android5.0之后Activity的出入场动画总体上来说可以分为两种。
分解、滑动进入、淡入淡出
是共享元素动画

3.1激活Activity中元素的过渡效

在styles.xml文件中添加:

<item name="android:windowContentTransitions">true</item>

3.2分解(从屏幕中间进或出)

跳转方式:

Intent intent  = new Intent(MainActivity.this,ExplodeActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());

所要跳转的ExplodeActivity中设置该Activity的进出场动画:

Explode explode = new Explode();
explode.setDuration(1000);
getWindow().setEnterTransition(explode);
getWindow().setExitTransition(explode);

3.3滑动

跳转按钮的跳转方式:

startActivity(new Intent(this, SlideActivity.class), ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

所要跳转的SlideActivity设置该Activity的进出场动画:

Slide slide = new Slide();
slide .setDuration(1000);

getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);

3.4浅入浅出

跳转按钮的跳转方式:

startActivity(new Intent(this, FadeActivity.class), ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

所要跳转的FadeActivity设置该Activity的进出场动画:

Slide slide = new Slide();
slide .setDuration(1000);

getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);

4.属性动画(Property animation)

Android3.0引入,补间动画的增强版
执行动画的对象不只是UI控件,可以是任意对象
实现动画效果不止四种,可以定义任意属性的变化
目标对象的属性值实际被改变了
补间动画通过XML或java定义两种方式实现

4.1工作原理

在一定时间间隔内,通过不断对值进行改变,并不断将该值赋给对象的属性,从而实现该对象在该属性上的动画效果。

4.2属性动画 API

Animator: 提供创建属性动画的基类。
ValueAnimator:属性动画用到的主要的时间引擎,负责计算各个帧的属性值。
ObjectAnimator: ValueAnimator 的子类,对指定对象的属性执行动画。
AnimatorSet:Animator 的子类,用于组合多个 Animator。

4.3 ValueAnimator

属性动画的使用:
创建 ValueAnimator 或 ObjectAnimator 对象 (可以从 XML 资源文件加载该动画也可以直接调用 ValueAnimator 或者 ObjectAnimator 的静态工厂方法创建动画)
根据需要为 Animator 对象设置属性。
如果需要监听 Animator 的动画开始事件,动画结束事件、动画重复事件、动画值改变事件,并根据事件提供响应处理代码,需要为Animator 对象设置监听器。
如果有多个动画需要同时播放,需要使用 AnimatorSet 组合这些动画。
调用 Animator 对象的 start 启动动画。

4.3.1 ValueAnimator—值动画

使用 ValueAnimator 创建动画的步骤:
调用 ValueAnimator 的 ofInt()、ofFloat() 或者 ofObject() 静态方法创建 ValueAnimator 实例。
调用 ValueAnimator 的 setXxx() 等方法设置持续时间,插值方式、重复次数等。
调用 ValueAnimator 的 start() 方法启动动画。
为 ValueAnimator 注册 AnimatorUpdateListener 监听器,在该监听器中可以监听 ValueAnimator 计算出来的值改变,并将这些值应用到指定对象上。

width = ly_root.getWidth();
height = ly_root.getHeight();
// 通过ofInt()创建对象,参数是个数不定,是一个过渡过程
ValueAnimator xValue =
ValueAnimator.ofInt(height,0,height / 4,height / 2,height / 4 * 3 ,height);
// 设置动画播放时长,单位为毫秒
xValue.setDuration(3000);
// 置循环次数,-1时则是无限循环
xValue.setRepeatCount(ValueAnimator.INFINITE);
//设置循环模式,有两种,RESTRAT重新播放,REVERSE倒序播放
xValue.setRepeatMode(ValueAnimator.RESTART);
//
xValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 轨迹方程 x = width / 2
int y = (Integer) animation.getAnimatedValue();
int x = width / 2;
moveView(img_babi, x, y);
}
});
xValue.setInterpolator(new LinearInterpolator());// 设置动画的变化速度,画以均匀的速度改变
xValue.start();

4.3.2 ObjectAnimator

ObjectAnimator显得更为易用,通过该类我们可以直接 对任意对象的任意属性进行动画操作!
ObjectAnimator内部机制: 寻找传输的属性名对应的get和set方法,而非找这个属性值!

ObjectAnimator animator1 =
ObjectAnimator.ofFloat(tv_pig, "alpha", 1f, 0f, 1f, 0f, 1f);

animator1.setDuration(3000);
animator1.setRepeatCount(ObjectAnimator.INFINITE);
animator1.setRepeatMode(ObjectAnimator.RESTART);
animator1.start();

4.3.2 AnimatorSet

ObjectAnimator animator1 =
ObjectAnimator.ofFloat(tv_pig, "alpha", 1f, 0f, 1f, 0f, 1f);
ObjectAnimator animator2 =
ObjectAnimator.ofFloat(tv_pig, "rotation", 0f, 360f, 0f);
ObjectAnimator animator3 =
ObjectAnimator.ofFloat(tv_pig, "scaleX", 2f, 4f, 1f, 0.5f, 1f);
ObjectAnimator animator4 =
ObjectAnimator.ofFloat(tv_pig, "translationY", height / 8, -100, height / 2);

AnimatorSet animSet = new AnimatorSet();
animSet.play(animator4).with(animator3).with(animator2).after(animator1);
animSet.setDuration(5000l);
animSet.start();

AnimatorSet 常用方法:
· after(Animator anim) after中的动画先执行, 之后才是play中的动画。
· after(long delay) after中设置时间, 那么play中的动画会根据时间延迟执行。
· before(Animator anim) before中的动画后执行, play中的先执行。
· with(Animator anim) play中的动画和with中的一同执行。
· playTogether() 中间可以放入要一起执行的全部动画, 之后不可接after(), before()这些函数。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: