Android动画的使用——补间动画

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

基础知识

谈起 Android动画,我们就得讲讲他的分类:从大的方向来说主要分为两类:View动画(视图动画) 属性动画。其中 View动画又包括 补间动画帧动画。其中,补间动画 使用广泛,下面我们一起来看看如何实现其动画效果。

补间动画:说白了就是涵盖了 平移、缩放、旋转 和 透明度四种变化的动画。实现方式有两种:xml文件java代码

平移

效果展示

xml 方式

使用步骤:

      1、res 下 创建 anim 文件夹,并创建 xxx.xml 文件

注意:让你创建 anim 名字的文件夹,你就别不信邪搞别的名字。

<?xml version="1.0" encoding="utf-8"?>
<!--set 表示动画集合,可放各个动画组合,同时设置时间、最后的位置等属性-->
<set xmlns:android="http://schemas.android.com/apk/res/android"android:duration="3000" android:fillAfter="true">
<!-- translate    平移动画--><!--        50%p 表示 相对于父控件宽高--><translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0%p" android:toXDelta="50%p" android:fromYDelta="0%p" android:toYDelta="50%p"></translate>
</set>

     2、代码调用

package com.wust.myanimation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv_hello_world;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件tv_hello_world = findViewById(R.id.tv_hello_world);//利用 AnimationUtils 这个工具类获取 AnimationAnimation translate = AnimationUtils.loadAnimation(this, R.anim.translate);//因为这一步我们在 xml 中做了申明,所以这里不需要写,但是要记住,Duration 不设置你将会看不到动画
//        translate.setDuration(3000);//开始动画tv_hello_world.startAnimation(translate);}
}

 代码方式

使用步骤:

  1. 编写java代码
package com.wust.myanimation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv_hello_world;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件tv_hello_world = findViewById(R.id.tv_hello_world);//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_PARENT:表示后面的值相对于父控件Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.5f);//设置运动完了之后是否回来translate.setFillAfter(true);//这一步不能忘记了translate.setDuration(3000);//开始动画tv_hello_world.startAnimation(translate);}
}

缩放

效果展示

xml 方式

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillAfter="true" android:duration="3000"><scale android:fromXScale="0%" android:toXScale="100%" android:fromYScale="0%" android:toYScale="100%"/>
</set>
package com.wust.myanimation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv_hello_world;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件tv_hello_world = findViewById(R.id.tv_hello_world);//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_PARENT:表示后面的值相对于父控件Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale);//开始动画tv_hello_world.startAnimation(scale);}
}

java代码方式

package com.wust.myanimation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv_hello_world;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件tv_hello_world = findViewById(R.id.tv_hello_world);//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_SELF:表示后面的值相对于自己 这里的值大小都是 [0,1]Animation scale = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//设置运动完了之后是否回来scale.setFillAfter(true);//这一步不能忘记了scale.setDuration(3000);//开始动画tv_hello_world.startAnimation(scale);}
}

旋转 和 透明度大家根据上面两种方式的规律自行尝试,我们抓紧篇幅赶紧来讲讲如何将这四种动画集合在一起展示。

综合动画

效果展示

xml方式

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="true"><translate android:fromXDelta="0%p"  android:toXDelta="50%p" android:fromYDelta="0%p" android:toYDelta="50%p"/><scale android:pivotX="0%" android:pivotY="0%" android:fromXScale="0%" android:toXScale="100%" android:fromYScale="0%" android:toYScale="100%"/><alpha android:fromAlpha="0" android:toAlpha="1"/><rotate android:pivotY="0%" android:pivotX="0%" android:fromDegrees="0" android:toDegrees="18"/>
</set>
package com.wust.myanimation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv_hello_world;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件tv_hello_world = findViewById(R.id.tv_hello_world);//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_SELF:表示后面的值相对于自己 这里的值大小都是 [0,1]Animation scale = AnimationUtils.loadAnimation(this,R.anim.multianim);//开始动画tv_hello_world.startAnimation(scale);}
}

java代码方式

package com.wust.myanimation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv_hello_world;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件tv_hello_world = findViewById(R.id.tv_hello_world);//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_SELF:表示后面的值相对于自己 这里的值大小都是 [0,1]Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.5f);Animation scale = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);Animation alpha = new AlphaAnimation(0, 1);Animation rotate = new RotateAnimation(0, 18,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);//创建 AnimationSet 装 AnimationAnimationSet animationSet = new AnimationSet(false);animationSet.addAnimation(translate);animationSet.addAnimation(scale);animationSet.addAnimation(alpha);animationSet.addAnimation(rotate);animationSet.setFillAfter(true);animationSet.setDuration(3000);//开始动画tv_hello_world.startAnimation(animationSet);}
}

动画监听

animation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {//动画开始时调用}@Overridepublic void onAnimationEnd(Animation animation) {//动画结束时调用}@Overridepublic void onAnimationRepeat(Animation animation) {//动画重复时调用}
});

总结

在 java 代码中,我们主要用到了如下几个类: Animation  |||  TranslateAnimation、ScaleAnimation、RotateAnimation、 AlphaAnimation  ||| AnimationSet 

从颜色中可以看到,总的分为三类:Animation 是父亲级别,TranslateAnimation、ScaleAnimation、RotateAnimation、 AlphaAnimation 继承自 Animation,表示具体动画,

AnimationSet 继承自 Animation,表示动画集合。

View动画 ---- 朴间动画 的应用场景:

  • 标准的动画效果:平移、旋转、缩放和透明度
  • 特殊的应用场景:
  1. Activity切换效果
  2. Fragment切换效果
  3. 视图组 (ViewGroup)中子元素的出场效果

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

相关文章

An动画优化之补间形状与传统补间的优化

文章目录 一、补间形状的优化&#xff08;1&#xff09;准备工作1&#xff09;写字2&#xff09;画圆3&#xff09;标明 &#xff08;2&#xff09;创建关键帧及动画1&#xff09;创建关键帧2&#xff09;创建补间形状3&#xff09;改变速度 &#xff08;3&#xff09;效果 二、…

Android studio 动画---补间动画

1、新建文件。【注意&#xff1a;文件名只能命名为anim】 2、新建文件 3、在新建的文件中添加代码&#xff1a;【以下代码仅供参考】 3.1、改变动画的透明度&#xff1a; <?xml version"1.0" encoding"utf-8"?> <set xmlns:android"http:/…

动画三部曲--补间动画

图片从慢慢退出&#xff0c;过程中通过缩放、渐变等实现动画效果 将缩放的参数写入xml 中&#xff0c;translate_animation.xml <translatexmlns:android"http://schemas.android.com/apk/res/android"android:fromXDelta"0"android:fromYDelta"…

Android 补间动画原理

这段时间项目中用到了动画&#xff0c;所以趁热打铁&#xff0c;看看动画原理 补间动画 使用举例 TranslateAnimation translateAnim new TranslateAnimation(0, 100, 0, 100);translateAnim.setDuration(1000);translateAnim.setFillAfter(true);testBut.startAnimation(t…

补间动画和逐帧动画

补间动画 补间&#xff08;Tween&#xff09;动画通过对View进行一系列的图形变换来实现动画效果&#xff0c;其中图像变换包括平移、缩放、旋转、改变透明度等。补间动画最常用的方式是通过XML文件定义动画。 透明度渐变动画&#xff08;AlphaAnimation&#xff09; 主要通…

Android 动画—补间动画

帧动画是通过连续播放图片来模拟动画效果&#xff0c;而补间动画开发者只需指定动画开始&#xff0c;以及动画结束"关键帧"&#xff0c;而动画变化的"中间帧"则由系统计算并补齐&#xff01; 1.补间动画的分类和Interpolator Andoird所支持的补间动画效果…

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

本文目录 补间动画概述和分类各类补间动画实现xml实现补间动画透明度动画-AlphaAnimation缩放动画-ScaleAnimation位移动画-TranslateAnimation旋转动画-RotateAnimation动画组合-AnimationSet 代码实现补间动画透明度动画&#xff08;AlphaAnimation&#xff09;缩放动画&…

补间动画详解一 基类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 衬垫&…