【Android】补间动画用法最全详解

article/2025/11/7 23:52:58

本文目录

  • 补间动画概述和分类
  • 各类补间动画实现
    • xml实现补间动画
      • 透明度动画-AlphaAnimation
      • 缩放动画-ScaleAnimation
      • 位移动画-TranslateAnimation
      • 旋转动画-RotateAnimation
      • 动画组合-AnimationSet
    • 代码实现补间动画
      • 透明度动画(AlphaAnimation)
      • 缩放动画(ScaleAnimation)
      • 位移动画(TranslateAnimation)
      • 旋转动画(RotateAnimation)
      • 动画组合(AnimationSet)

推荐阅读:
Android 动画之帧动画用法详解
Android 属性动画:一文让你彻底了解和掌握属性动画用法

在这里插入图片描述

补间动画概述和分类

在Android动画中,补间动画一共可以分成四类即透明度动画、缩放动画、旋转动画、位移动画。
其实现方法可以通过xml来配置,也可以通过代码来实现。

各类补间动画实现

xml实现补间动画

xml实现补间动画,需要将xml放到res下的anim目录,Android工程默认是没有anim文件夹的在读文件前我们先把anim 文件夹以及文件建好

  • 点中工程的res目录 右键New -> Directory -> 弹窗中输入anim
  • 点中刚刚新建的anim目录 右键New -> Animation Resource File
  • 创建好了以后输入如下的布局文件代码

透明度动画-AlphaAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<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结束透明度
duration持续时间(毫秒)

缩放动画-ScaleAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:fromXScale="0.2"android:toXScale="1.5"android:fromYScale="0.2"android:toYScale="1.5"android:pivotX="50%"android:pivotY="50%"android:duration="2000"/>
属性值含义
fromXScale沿着X轴缩放的起始比例
fromYScale沿着X轴缩放的起始比例
toXScale沿着X轴缩放的结束比例
toYScale沿着Y轴缩放的结束比例
pivotX缩放的中轴点X坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
pivotY缩放的中轴点Y坐标
duration持续时间

位移动画-TranslateAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromXDelta="0"android:toXDelta="320"android:fromYDelta="0"android:toYDelta="0"android:duration="2000"/>
属性值含义
fromXDelta动画起始位置的X坐标
fromYDelta动画起始位置的Y坐标
toXDelta动画结束位置的X坐标
toYDelta动画结束位置的Y坐标
duration持续时间

旋转动画-RotateAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromDegrees="0"android:toDegrees="360"android:duration="1000"android:repeatCount="1"android:repeatMode="reverse"/>
属性值含义
fromDegrees/toDegrees旋转的起始/结束角度
repeatCount旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
repeatMode设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动
duration持续时间

动画组合-AnimationSet

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/decelerate_interpolator"android:shareInterpolator="true" ><scaleandroid:duration="2000"android:fromXScale="0.2"android:fromYScale="0.2"android:pivotX="50%"android:pivotY="50%"android:toXScale="1.5"android:toYScale="1.5" /><rotateandroid:duration="1000"android:fromDegrees="0"android:repeatCount="1"android:repeatMode="reverse"android:toDegrees="360" /><translateandroid:duration="2000"android:fromXDelta="0"android:fromYDelta="0"android:toXDelta="320"android:toYDelta="0" /><alphaandroid:duration="2000"android:fromAlpha="1.0"android:toAlpha="0.1" /></set>

添加布局文件代码
activity_main.xml 中添加5个按钮和一张图片

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#F44336"tools:context=".MainActivity"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="透明度"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="缩放"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="位移"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button2" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="旋转"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button3" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="组合"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button4" /><ImageViewandroid:id="@+id/image"android:layout_width="100dp"android:layout_height="100dp"android:background="@drawable/animation_flower"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.497"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/button2"app:layout_constraintVertical_bias="0.76" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java 中响应点击事件

public class MainActivity extends AppCompatActivity {Button mButton1;Button mButton2;Button mButton3;Button mButton4;Button mButton5;Animation animation = null;ImageView mImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImageView = findViewById(R.id.image);mButton1 = findViewById(R.id.button);mButton1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_alpha);mImageView.startAnimation(animation);}});mButton2 = findViewById(R.id.button2);mButton2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_scale);mImageView.startAnimation(animation);}});mButton3 = findViewById(R.id.button3);mButton3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_translate);mImageView.startAnimation(animation);}});mButton4 = findViewById(R.id.button4);mButton4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_rotate);mImageView.startAnimation(animation);}});mButton5 = findViewById(R.id.button5);mButton5.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_set);mImageView.startAnimation(animation);}});}
}

代码实现补间动画

用代码实现补间动画我们主要是通过构造 Animation animation = new AlphaAnimation(); 拿到响应的引用以后去设置动画的一些属性。
这些动画的公共方法如下:

animation.setDuration(2000);
animation.setFillAfter(true);
animation.setFillBefore(false);
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.RESTART);
属性值含义
setDuration(2000)动画持续时间
setFillAfter(true)如果设置为true,控件动画结束时,将保持动画最后时的状态
setFillBefore(false)如果设置为true,控件动画结束时,还原到开始动画前的状态
setRepeatCount(2)持续时间如果设置为true,控件动画结束时,还原到开始动画前的状态
setRepeatMode(Animation.RESTART);重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用

透明度动画(AlphaAnimation)

透明度动画参数最多的构造方法如下:

  //fromAlpha   动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
//toAlpha       动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明public AlphaAnimation(float fromAlpha, float toAlpha) {}

使用:

animation=  new AlphaAnimation(0, 1);
animation.setDuration(2000);
mImageView.startAnimation(animation);

在这里插入图片描述

缩放动画(ScaleAnimation)

我们先来看下缩放动画参数最多的构造方法

    public ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {}

构造方法参数的含义同前面的布局文件一样

属性值含义
fromX沿着X轴缩放的起始比例
fromY沿着X轴缩放的起始比例
toX沿着X轴缩放的结束比例
toY沿着Y轴缩放的结束比例
pivotXValue缩放的中轴点X坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
pivotXType有2种模式,RELATIVE_TO_SELF(相对于自身)和RELATIVE_TO_PARENT(相对于父布局)pivotx,pivotY的值就应该是0-1的浮点数,分别对应xml中的%(自身)和%p(父布局)
pivotYValue缩放的中轴点Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
pivotYType同pivotXType

填入构造方法的参数

      animation = new ScaleAnimation(0, 1.4f, 0, 1.4f,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);animation.setDuration(2000);mImageView.startAnimation(animation);

效果如下:
在这里插入图片描述

位移动画(TranslateAnimation)

参数最多的构造方法如下:

 //fromXDelta     起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,同scale//fromYDelta    起始点Y轴从标,可以是数值、百分数、百分数p 三种样式
//toXDelta         结束点X轴坐标//toYDelta        结束点Y轴坐标 // fromYType、toYType同ScaleAnimation,public TranslateAnimation(int fromXType, float fromXValue, int toXType,float toXValue,int fromYType, float fromYValue, int toYType, float toYValue) {}

使用:

animation= new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0.5f,TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
mImageView.startAnimation(animation);

效果如下:
在这里插入图片描述

旋转动画(RotateAnimation)

参数最多的构造方法如下:

public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType,float pivotXValue, int pivotYType, float pivotYValue) {}

使用:

 animation= new RotateAnimation(0, -720, RotateAnimation.RELATIVE_TO_SELF, 0.5f,RotateAnimation.RELATIVE_TO_SELF, 0.5f);animation.setDuration(2000);mImageView.startAnimation(animation);

效果如下:
在这里插入图片描述

动画组合(AnimationSet)

通过AnimationSet可以将前面的四种动画组合在一起,实现一些混合动画效果

Animation rotateAnimation = new RotateAnimation(0, -720, RotateAnimation.RELATIVE_TO_SELF, 0.5f,RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);Animation translateAnimation = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.5f,TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.5f);
translateAnimation.setDuration(2000);Animation scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f, ScaleAnimation.RELATIVE_TO_SELF,0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);Animation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.setDuration(4000);
animationSet.setFillAfter(true);
mImageView.startAnimation(animationSet);

在这里插入图片描述

作者:lucashu
出处:https://blog.csdn.net/huweiliyi/article/details/105670978
原创不易,欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。


http://chatgpt.dhexx.cn/article/nTm3JlJP.shtml

相关文章

补间动画详解一 基类Animation

补间动画(Tween animation)是通过在两个关键帧之间补充渐变的动画效果来实现的。 Android系统提供了四个补间动画的类,分别是AlphaAnimation、RotateAnimation、ScaleAnimation和TranslateAnimation,另外还有一个能够把多个动画组合起来的AnimationSet类,这些类都有一个共…

Android动画之补间动画

Android动画之补间动画 和上面一章学的帧动画不同&#xff0c;帧动画 是通过连续播放图片来模拟动画效果&#xff0c;而补间动画开发者只需指定动画开始&#xff0c;以及动画结束"关键帧"&#xff0c; 而动画变化的"中间帧"则由系统计算并补齐&#xff01…

使用Gstreamer处理RTSP视频流

文章目录 RTSP视频流处理方法1. Gstreamer整体框架1.1 Media Applications1.2 Core Framework1.3 Plugins 2. Gstreamer组件2.1 Element2.2 Pad2.3 Bin和Pipeline 3. gstreamer tools3.1 gst-inspect-1.03.2 gst-launch-1.0 4. 参考链接 RTSP视频流处理方法 这里使用Gstreamer…

GStreamer基础教程02——GStreamer概念

上一个教程演示了如何自动生成一个pipeline。这次我们打算用一个个element来手动搭建一个pipeline。我们这个教程会演示&#xff1a; 1. 什么是GStreamer的element以及如何建立一个element 2. 如何在element直接建立连接 3. 如何客制化element的行为 4. 如何监视总线上的错…

GStreamer功能详解

参考&#xff1a;https://blog.csdn.net/tx3344/article/details/7497434 参考&#xff1a;https://thebigdoc.readthedocs.io/en/latest/gstreamer/gst-concept.html 参考&#xff1a;https://blog.csdn.net/sdjhs/article/details/51444934 什么是GStreamer&#xff1f; …

基于gstreamer的rtsp推送和转发

基于gstreamer的rtsp推送和转发 一、配置gstreamer环境二、安装gstreamer-rtsp-server三、读取usb摄像头并推rtsp流四、转发rtsp 前段时间因为实验室项目要求&#xff0c;需要读取摄像头并推rtsp流&#xff0c;由于我们实验室不是做与之相关的工作&#xff0c;所以并没有什么参…

深入浅出gstreamer开发

Gstreamer解决什么问题&#xff1f; — 上层接口和应用方式的 相对稳定 与底层接口、平台环境的 多样化 。例如&#xff1a; codec 不同种类不同实现&#xff0c;音视频处理不同&#xff0c;硬件输入、输出、采集播放不同&#xff0c;芯片不同&#xff0c;操作系统不同。 — 通…

【GStreamer 】3-1 gstreamer插件之 videotestsrc 介绍

目录 ​编辑 1、简介 2、videotestsrc 3、videotestsrc 不同pattern参数测试罗列 3.1 (0): smpte - SMPTE 100% color bars 3.2 (1): snow - Random (television snow) 3.3 (2): black - 100% Black ​编辑 3.4 checkers 方块 ​编辑 3.5 几何图形 4、videotestsrc…

Gstreamer概述

1、什么是GStreamer GStreamer 是用来构建流媒体应用的开源多媒体框架(framework)&#xff0c;其基本设计思想来自于俄勒冈(Oregon)研究生学院有关视频管道的创意, 同时也借鉴了DirectShow的设计思想。其目标是要简化音/视频应用程序的开发&#xff0c;已经能够被用来处理像 M…

gstreamer简介

常用 gchar * caps_string gst_caps_to_string (new_selected_caps); g_free (caps_string); 需要弄懂的问题 tunnel tee queue 最后列一下Gstreamer中常见的时间宏&#xff0c;注意Gstreamer中的时间单位是&#xff1a;纳秒 #define G_USEC_PER_SEC 1000000 #define GST_S…

Gstreamer基础知识介绍

由于deepstream是基于gstreamer的&#xff0c;所以要想在deepstream上做拓展&#xff0c;需要对gstreamer有一定的认识。以下主要介绍Gstreamer整体框架和Gstreamer基础概念。 一、Gstreamer整体框架 gstreamer是一个用于开发流式多媒体应用的开源框架。本身这个框架是为了更…

【GStreamer 】1-扫盲介绍

从历史的角度来看&#xff0c;Linux 在多媒体方面已经远远落后于其它的操作系统。微软的Windows和苹果的MacOS它们对多媒体设备、多媒体创作、播放和实时处理等方面已经有了很好的支持。另一方面&#xff0c;Linux对多媒体应用的综合贡献比较少&#xff0c;这也使得Linux很难在…

详细的GStreamer开发教程

详细的GStreamer开发教程 文章目录 详细的GStreamer开发教程1. 什么是GStreamer&#xff1f;2. GStreamer架构2.1 Media Applications2.2 Core Framework2.3 Plugins 3. GStreamer组件3.1 Element创建一个 GstElement 3.2 箱柜&#xff08;bin&#xff09;元件的状态 3.3 衬垫&…

gstreamer(一)入门和概述

一&#xff0e;概述 在音视频领域接触最多实现的方案通常是通过ffmpeg&#xff08;PC和sever端居多&#xff09;或者硬件厂家的的SDK实现特定硬件的编解码功能&#xff08;机顶盒&#xff0c;电视等嵌入式设备&#xff09;。这里我们介绍一个在国内不太常用的解决方案----gstr…

二、什么是GStreamer

GStreamer是一个用于创建流媒体应用程序的框架。基本的设计来自俄勒冈研究生院的视频管道&#xff0c;还有一些来自DirectShow的想法。 GStreamer的开发框架使编写任何类型的流媒体应用程序成为可能。GStreamer框架旨在使编写处理音频或视频或两者同时处理的应用程序变得容易。…

Gstreamer 应用开发:1-基础介绍

我们之前的系列&#xff0c;正式的介绍了Gstreamer&#xff0c;并且围绕如何使用USB相机推流实现RTSP服务器来做了介绍&#xff0c;并在Jeston TX1 平台上做了优化急速的一些探索。 今天我们开始围绕如何用命令实现一个音视频混合&#xff0c;或者单独的音频&#xff0c;和单独…

【gstreamer】入门介绍

概述 GStreamer是一个基于流媒体的框架&#xff0c;是一个开放源代码的多媒体框架&#xff0c;用于创建音频和视频处理应用程序。它是一个运行在多个操作系统上的跨平台框架&#xff0c;支持多种不同的多媒体格式。 GStreamer框架的核心是基于插件的体系结构&#xff0c;插件…

Gstreamer基础讲解

Gstreamer讲解 文章目录 Gstreamer讲解基础背景小结 元件&#xff08;Element&#xff09;衬垫(Pads)Gstreamer的面向对象Gstreamer的多线程 实用工具Gstreamer常用插件介绍gstreamer工程记录关于YUV的补充知识 基础 背景 ​ 从历史的角度来看&#xff0c;Linux在多媒体方面已…

Gstreamer 简介

转载自&#xff1a;John.Leng - 博客园http://www.cnblogs.com/xleng/ 什么是Gstreamer&#xff1f; Gstreamer是一个支持Windows&#xff0c;Linux&#xff0c;Android&#xff0c; iOS的跨平台的多媒体框架&#xff0c;应用程序可以通过管道&#xff08;Pipeline&#xff0…

微博视频怎么下载?微博视频下载和保存工具

本文转载自&#xff1a;怎么下载微博视频 ​​​微博视频怎么下载&#xff1f;当你刷微博看到自己特别喜欢的视频时&#xff0c;除了转发该视频到自己的微博外&#xff0c;你可能还想把这个微博视频保存到自己电脑或者手机中&#xff0c;那应该怎么实现weibo视频的下载呢&#…