Android 动画之帧动画 + 补间动画 + 属性动画

简介: 本文介绍了Android开发中的三种动画类型:帧动画、补间动画和属性动画。帧动画通过依次播放一系列静态图片实现动态效果,支持Java代码与XML两种实现方式。补间动画基于起始和结束位置自动生成过渡效果,涵盖透明度、位移、旋转、缩放及组合动画等多种形式,并可搭配插值器优化动画过程。属性动画则通过改变对象属性实现动画,支持透明度、位移、旋转、缩放及组合动画,灵活性更高且适用于更复杂的场景。文中提供了详细的代码示例,帮助开发者快速上手。

帧动画

帧动画的原理就是让一系列的静态图片依次播放,实现动画效果。
下面了解一下两种实现帧动画的方式

1.利用 Java 代码实现帧动画
2.利用 xml 实现帧动画(开发中通常使用这种方法实现帧动画)

1.利用 Java 代码实现帧动画
1.png

源代码如下:
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <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/iv_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java文件如下

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    AnimationDrawable animationDrawable;
    private Button btn_start;
    private Button btn_stop;
    private ImageView iv_animation;//java的形式实现


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        iv_animation = (ImageView) findViewById(R.id.iv_animation);

        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                //1.实现帧动画的类(相当于一本空白的小人书)
                animationDrawable = new AnimationDrawable();
                //2.为帧动画添加内容(在小人书里添加内容)
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim1)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim2)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim3)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim4)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim5)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim6)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim7)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim8)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim9)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim10)), 100);
                //3.将帧动画设置给View做背景
                iv_animation.setBackground(animationDrawable);
                //其他操作,如,设置只执行一次
                animationDrawable.setOneShot(true);
                //4.开启动画(相当于翻书)
                animationDrawable.start();
                break;
            case R.id.btn_stop:
                //停止动画
                animationDrawable.stop();
                break;
        }
    }
}

2.利用 xml 实现帧动画(开发中通常使用这种方法实现帧动画)
(1).帧动画通常在XML 资源进行定义,在 标签下使用 子元素标签定义动画的全部帧,并指定各帧的持续时间。
2.png

2、布局中将 AnimationDrawable 对象直接作为背景
3.png

3.在Activity文件中进行操作(播放,停止)
4.png

补间动画

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
为了更好的演示,写了一个Demo,xml界面如下(最后有源码)
5.png

要介绍的有:
1.透明动画:AlphaAnimation
2.位移动画:TranslateAnimation
3.各种动画插入器
4.旋转动画:RotateAnimation
5.缩放动画:ScaleAnimation
6.组合显示:AnimationSet(动画集合容器)

1.透明动画:AlphaAnimation是一个实现透明动画的API,参数:制定透密度从多少到多少(范围0-1),0代表透明
6.png

2.位移动画:TranslateAnimation:四个参数:X轴开始位置,X轴结束位置,Y轴开始位置,Y轴结束位置
7.png

3.各种动画插入器:改变中间的补充过程(变得更有趣!!!,可以不加)

 //AccelerateDecelerateInterpolator    :其变化开始和结束速率较慢,中间加速
                //AccelerateInterpolator    :加速,开始速度慢,后面加速
                //DecelerateInterpolator    :减速,开始速度快,后面减速
                //LinearInterpolator    :线性(线性均匀变化)    其变化速率恒定
                //AnticipateInterpolator:反向    先向相反的方向改变一段距离后,再加速
                //AnticipateOvershootInterpolator:反向加超越:开始向后甩,然后向前甩,过冲到目标值,最后又回到了终值
                //OvershootInterpolator    :超越,其变化开始向前甩,过冲到目标值,最后又回到了终值
                //BounceInterpolator    :跳跃    其变化在结束时反弹
                //CycleInterpolator     :    循环播放,其速率为正弦曲线
                //TimeInterpolator     :    一个接口,可以自定义插值器

4.旋转动画:RotateAnimation:参数,开始时角度,结束时角度,旋转中心的横坐标,旋转中心的纵坐标
8.png

5.缩放动画:ScaleAnimation,参数:x开始的比例值,x结束的比例值,Y开始的比例值,Y结束的比例值,后面两个参数为缩放中心的坐标点
9.png

6.组合显示:AnimationSet(动画集合容器)
10.png

源码如下:
activity_second.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="透明动画" />

        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="位移动画" />

        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="旋转动画" />

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="缩放动画" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="groupshow"
        android:text="组合显示" />
</RelativeLayout>

SecondActivity.java文件

//补间动画
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_alpha;
    private Button btn_translate;
    private Button btn_rotate;
    private Button btn_scale;
    private ImageView iv_show;
    Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
    }

    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_translate = (Button) findViewById(R.id.btn_translate);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);

        btn_alpha.setOnClickListener(this);
        btn_translate.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
        iv_show = (ImageView) findViewById(R.id.iv_show);
        iv_show.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_alpha://透明动画
                //AlphaAnimation是一个实现透明动画的API,参数:制定透密度从多少到多少(范围0-1),0代表透明
                animation = new AlphaAnimation(1f, 0);
                break;
            case R.id.btn_translate://位移动画
                //TranslateAnimation:四个参数:X轴开始位置,X轴结束位置,Y轴开始位置,Y轴结束位置
                //指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
                //中间的变化过程又分为各种动画插入器
                //AccelerateDecelerateInterpolator    :其变化开始和结束速率较慢,中间加速
                //AccelerateInterpolator    :加速,开始速度慢,后面加速
                //DecelerateInterpolator    :减速,开始速度快,后面减速
                //LinearInterpolator    :线性(线性均匀变化)    其变化速率恒定
                //AnticipateInterpolator:反向    先向相反的方向改变一段距离后,再加速
                //AnticipateOvershootInterpolator:反向加超越:开始向后甩,然后向前甩,过冲到目标值,最后又回到了终值
                //OvershootInterpolator    :超越,其变化开始向前甩,过冲到目标值,最后又回到了终值
                //BounceInterpolator    :跳跃    其变化在结束时反弹
                //CycleInterpolator     :    循环播放,其速率为正弦曲线
                //TimeInterpolator     :    一个接口,可以自定义插值器
                animation = new TranslateAnimation(0, -200f, 0, 200f);
                //指定一个插入器,来改变中间的补充过程(变得更有趣!!!,可以不加)
                animation.setInterpolator(new BounceInterpolator());
                break;
            case R.id.btn_rotate://旋转动画
                //顺时针旋转, RotateAnimation:参数,开始时角度,结束时角度,旋转中心的横坐标,旋转中心的纵坐标
                animation = new RotateAnimation(0, 180f, iv_show.getWidth(), iv_show.getHeight());
                //指定一个插入器,来改变中间的补充过程(变得更有趣!!!,可以不加)
                animation.setInterpolator(new BounceInterpolator());
                break;
            case R.id.btn_scale://缩放动画
                //以1为标准,比一小就变小,比1大就放大
                //ScaleAnimation参数:x开始的比例值,x结束的比例值,Y开始的比例值,Y结束的比例值,后面两个参数为缩放中心的坐标点
                animation = new ScaleAnimation(2f, 0.5f, 2f, 0.5f, iv_show.getWidth(), iv_show.getHeight());
                break;

        }

        //设置动画播放时间
        animation.setDuration(3000);
        //设置是否保存动画结束后的状态
        animation.setFillAfter(true);
        //不管点击那个动画,最终都要开启,开启动画
        iv_show.startAnimation(animation);
    }

    public void groupshow(View view) {//组合显示
        //分别创建要组合的动画
        RotateAnimation rotate = new RotateAnimation(0, 180f, iv_show.getWidth(), iv_show.getHeight());
        ScaleAnimation scale = new ScaleAnimation(2f, 0.5f, 2f, 0.5f, iv_show.getWidth(), iv_show.getHeight());
        //创建动画集合容器
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(scale);
        set.addAnimation(rotate);
        //设置动画播放时间
        set.setDuration(3000);
        //设置是否保存动画结束后的状态
        set.setFillAfter(true);
        //设置次数
        set.setRepeatCount(2);
        //设置重复模式
        set.setRepeatMode(Animation.RESTART);
        iv_show.startAnimation(set);

    }
}

属性动画

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
为了更好的演示,写了一个Demo,xml界面如下(最后有源码)
11.png

要介绍的有:
1.透明动画:alpha
2.位移动画:translationX,translationY
3.旋转动画:rotation
4.缩放动画:scaleX,scaleY
5.组合显示:AnimatorSet(动画集合容器)

1.透明动画:alpha
12.png

2.位移动画:translationX,translationY
13.png

3.旋转动画:rotation
14.png

4..缩放动画:scaleX,scaleY
15.png

5.组合显示:AnimatorSet(动画集合容器)
16.png

源码如下:
activity_third.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ThirdActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="透明动画" />

        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="位移动画" />

        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="旋转动画" />

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="缩放动画" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="groupshow"
        android:text="组合显示" />
</RelativeLayout>

ThirdActivity.java文件:

//属性动画
public class ThirdActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_alpha;
    private Button btn_translate;
    private Button btn_rotate;
    private Button btn_scale;
    private ImageView iv_show;
    ObjectAnimator objectAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        initView();
    }


    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_translate = (Button) findViewById(R.id.btn_translate);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        iv_show = (ImageView) findViewById(R.id.iv_show);

        btn_alpha.setOnClickListener(this);
        btn_translate.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //ofFloat:三个参数 :1.受到动画影响的对象(UI控件)2. 要执行的动画类型 3. 一组动画的属性
        switch (v.getId()) {
            case R.id.btn_alpha://透明动画
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "alpha", 0.5f, 1f, 0.5f, 1f);
                break;
            case R.id.btn_translate://位移动画
                //只会执行一个
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "translationX", 0, 200);
                //objectAnimator=ObjectAnimator.ofFloat(iv_show,"translationY",0,200);
                break;
            case R.id.btn_rotate://旋转动画
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);
                break;
            case R.id.btn_scale://缩放动画
                //只会执行一个
                objectAnimator = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 3f, 4f);
                // objectAnimator=ObjectAnimator.ofFloat(iv_show,"scaleX",1f,2f,3f,4f);

                break;
        }
        //动画持续时间
        objectAnimator.setDuration(3000);
        //启动动画
        objectAnimator.start();

    }

    public void groupshow(View view) {//组合

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 1f, 2f);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(iv_show, "scaleX", 1f, 2f, 1f, 2f);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);
        //创建属性动画的集合容器
        AnimatorSet animatorSet = new AnimatorSet();
        //同时播放
        animatorSet.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3);
        //按照顺序播放
        // animatorSet.play(objectAnimator1).after(objectAnimator2).after(objectAnimator3);

        /*
        * 另一种方式
        List<Animator> list=new ArrayList<>();
        list.add(objectAnimator1);
        list.add(objectAnimator2);
        list.add(objectAnimator3);
        animatorSet.playSequentially(list);//按照顺序播放
        animatorSet.playTogether(list);//同时执行
        */
        //设置时长
        animatorSet.setDuration(3000);
        //启动
        animatorSet.start();

    }
}
相关文章
|
2月前
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
144 30
|
2月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
308 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
2月前
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
|
2月前
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
216 65
|
2月前
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
77 26
|
2月前
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
Android自定义view之围棋动画(化繁为简)
|
2月前
|
Java Android开发 开发者
Android自定义view之围棋动画
本文详细介绍了在Android中自定义View实现围棋动画的过程。从测量宽高、绘制棋盘背景,到创建固定棋子及动态棋子,最后通过属性动画实现棋子的移动效果。文章还讲解了如何通过自定义属性调整棋子和棋盘的颜色及动画时长,并优化视觉效果,如添加渐变色让白子更明显。最终效果既可作为围棋动画展示,也可用作加载等待动画。代码完整,适合进阶开发者学习参考。
|
XML Java Android开发
Android App开发中集合动画和属性动画的讲解及实战演示(附源码 简单易懂 可直接使用)
Android App开发中集合动画和属性动画的讲解及实战演示(附源码 简单易懂 可直接使用)
136 0
Android App开发中集合动画和属性动画的讲解及实战演示(附源码 简单易懂 可直接使用)
|
API Android开发
Android动画——属性动画
在属性动画中,常用到的API有ValueAnimator,ObjectAnimator。 ValueAnimator:时间引擎,负责计算各个帧的属性值,基本上其他属性动画都会直接或间接继承它; ObjectAnimator: ValueAnimator 的子类,对指定对象的属性执行动画。
280 0
|
XML Android开发 数据格式
Android View动画和属性动画
Android View动画和属性动画
Android View动画和属性动画

热门文章

最新文章