Android开发Android开发动画

2-4.1 基础动画全纪录

2019-12-16  本文已影响0人  Shimmer_

[TOC]

1. View/(Tween)补间动画 Animation

View动画时通过对View进行平移、旋转、缩放和显隐变换,从而产生动画效果,是一种渐进式的动画


View动画种类.png

1.1 平移 TranslateAnimation

Java代码
//起点X,Y 增加的X,Y
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
ta.setDuration(1000);
animationView.startAnimation(ta);

XML代码 xml动画文件路径:res/anim/xxxanimation.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0%p"
    android:fromXDelta="0%p"
    android:toYDelta="90%p"
    android:toXDelta="90%p"
    android:duration="1000" />

Animation ta1 = AnimationUtils.loadAnimation(this, R.anim.translate);
animationView.startAnimation(ta1);

更复杂实现

//fromXType,fromXValue, toXType,toXValue,fromYType,fromYValue, toYType,toYValue
//起点X方向参照值,起点X,终点X方向参照值,终点X,起点Y方向参照值,起点Y,终点Y方向参照值,终点Y
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1,
Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1);
参照值 描述
Animation.ABSOLUTE 具体的坐标值,指绝对的屏幕像素单位
Animation.RELATIVE_TO_SELF 相对自己的坐标值,0.1f是指自己的坐标值乘以0.1
Animation.RELATIVE_TO_PARENT 相对父容器的坐标值,0.1f是指父容器的坐标值乘以0.1

简单实现实际上是参照值全为Animation.ABSOLUTE的情况

1.2缩放 ScaleAnimation

Java代码
//起始X倍数,最终X倍数,起始Y倍数,最终X倍数
ScaleAnimation sa = new ScaleAnimation(4, 1, 4, 1);
sa.setDuration(2000);
baseAnimationView.startAnimation(sa);

XML代码
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0"/>

Animation sa1 = AnimationUtils.loadAnimation(this, R.anim.scale);
                animationView.startAnimation(sa1);

更复杂实现

//起始X倍数,最终X倍数,起始Y倍数,最终X倍数,缩放模式,0.5f自身中心,缩放模式,0.5f自身中心
//ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
animationView.startAnimation(sa);

1.3旋转 RotateAnimation

Java代码
//起始角度,最终角度,圆心X,Y坐标
//起始角度,最终角度,旋转模式,0.5f自身中心,旋转模式,0.5f自身中心
//RotateAnimation ra = new RotateAnimation(0, 360, 100, 100);
RotateAnimation ra = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_PARENT,0.5f);
ra.setDuration(1000);
animationView.startAnimation(ra);

XML代码
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration ="1000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%" />
//调用
Animation ra1 = AnimationUtils.loadAnimation(this, R.anim.rorate);
animationView.startAnimation(ra1);

1.4 透明度 AlphaAnimation

Java代码
//起始透明度,结束透明度
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
animationView.startAnimation(aa);

XML代码
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    />
//调用
Animation aa1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
animationView.startAnimation(aa1);

1.5 动画集合

Java代码
 TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 0);
ScaleAnimation sa = new ScaleAnimation(4, 2, 4, 2,
    Animation.RELATIVE_TO_SELF, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,      Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation aa = new AlphaAnimation(0, 1);
AnimationSet animationSet = new AnimationSet(true);
ta.setDuration(1000);
sa.setDuration(1000);
ra.setDuration(1000);
aa.setDuration(1000);
animationSet.addAnimation(ta);
animationSet.addAnimation(sa);
animationSet.addAnimation(ra);
animationSet.addAnimation(aa);
animationSet.setDuration(1000);
baseAnimationView.startAnimation(animationSet);
XML代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially"
    android:fillAfter="false"
    android:zAdjustment="normal"
    >
    <!-- 数字为绝对值,x%p为父容器坐标值的x%值    -->
    <translate
        android:duration="1000"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="90%p"
        android:toYDelta="90%p" />
    <scale
        android:duration="1000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        />
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        />
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>
调用
Animation a = AnimationUtils.loadAnimation(this, R.anim.base_animation);
baseAnimationView.startAnimation(a);

旋转动画与平移动画做集合时,旋转坐标轴异常,即如何实现 滚动至指定位置动画?

2. 帧动画/AnimationDrawable

帧动画是通过顺序播放一系列图像从而产生动画效果,是一种简单的图片切换动画

XML代码 xml动画文件路径:res/drawable/xxxanimation.xml 
<?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/p4" android:duration="100"/>
    <item android:drawable="@drawable/p5" android:duration="100"/>
    <item android:drawable="@drawable/p6" android:duration="100"/>

</animation-list>
调用
 fAnimaitonView.setBackgroundResource(R.drawable.drawable_animation);
((AnimationDrawable) fAnimaitonView.getBackground()).start();

oneshot ture表示执行一次 false表示循环执行

3. 自定义动画

我们可以通过继承Animation类,重写它的initialize和applyTransformation方法来实现我们想要的效果,中间涉及数学上矩阵变换的概念,但实际开发中几乎用不到,因此待后续有时间做基础整理

4. 使用补充

4.1 动画监听

View动画不经常使用监听,但也有需要的时候

ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
    public void onAnimationStart(Animation animation) {
        //启动时回调
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        //结束时回调
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        //重复时回调
    }
});

4.2 插值器

插值器可以理解为动画进行过程中不同阶段时候设置动画的速度值,如匀速运动,加速运动,减速运动等

4.3 特殊使用场景

上一篇下一篇

猜你喜欢

热点阅读