Android 动画的基本原理

本文详细介绍了Android中的两种动画类型——属性动画(Property Animation)和视图动画(View Animation),并深入解析了各自的实现方式及XML配置方法。涵盖了属性动画的ObjectAnimator与AnimatorSet的使用,以及视图动画中的补间动画(Tween Animation)和帧动画(Frame Animation)的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An animation resource can define one of two types of animations:

Property Animation

Creates an animation by modifying an object's property values over a set period of time with an Animator. 

View Animation
There are two types of animations that you can do with the view animation framework:

Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation

Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable. 

阅读官方源码,我们知道动画分为属性动画和视图动画两种类型,而视图动画又分为:

补间动画:创建一个动画通过执行一系列的转换在一个图像和动画
帧动画:或者创建一个动画通过显示一个图像序列为了AnimationDrawable。

一:Property Animation 属性动画
语法:
这里写图片描述
文件必须有一个根元素:,< objectAnimator >或< valueAnimator >。你可以动画元素分组在元素,包括其他元素。

1,<set>是包含其他动画元素的容器(< objectAnimator >、< valueAnimator >或<设置>元素)。代表一个动画集合。

属 性:
1.1:android:ordering 
    sequentially:表示按顺序按顺序播放动画在这个集合中
    together(default) : (默认)一起在这组同时播放动画。

2,<objectAnimator>的一个特定对象的属性在一个特定的时间。代表一个ObjectAnimator

属 性:
2.1:android:propertyName                 
    string,必需的。对象的属性动画,通过其名称引用。例如您可以指定“α”或“写成backgroundColor”视图对象。objectAnimator元素不暴露目标属性,
    然而,所以你不能在XML声明中设置对象动画。你必须抬高你的动画XML资源通过调用loadAnimator()和调用setTarget()来设置目标对象,其中包含这个属性。

2.2:android:valueTo 
    float,int或颜色。必需的。在动画属性的值。颜色表示为六位十六进制数字(例如,# 333333)。

2.3:android:valueFrom 
    float,int或颜色。动画的属性的值开始。如果不指定,动画开始获得的价值属性的get方法。颜色表示为六位十六进制数字(例如,# 333333)。

2.4:android:duration 
    int,动画的时间以毫秒为单位。300毫秒是默认的

2.5:android:startOffset 
    int。动画的毫秒延迟之后开始()

2.6:android:repeatCount 
    int。多少次重复动画。设置为“-1”无限重复或一个正整数。例如,值为“1”意味着动画是动画的初始运行后重复一次,所以动画共有两次。
    默认值为“0”,这意味着没有重复。

2.7:android:repeatMode
    int。动画表现当它达到动画的结束。android:repeatCount必须设置为一个正整数或“-1”这个属性产生影响。设置为“反向”动画的反方向与每个迭代或“重复”的动画循环每次从一开始。

2.8:android:valueType 
    没有指定这个属性值是一个颜色。动画框架自动处理颜色值

2.9:android:startOffset
    intType: 指定动画的值是integer
    floatType(default): 指定动画的值是float

在代码中添加动画

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);
set.setTarget(myObject);
set.start();

二:View Animation视图动画框架支持渐变和逐帧动画,它都可以在XML声明。以下部分描述如何使用这两种方法

1,Tween animation 补间动画中定义XML执行转换如旋转,褪色,移动,拉伸图形。
语法:
这里写图片描述
文件必须有一个根元素:,< objectAnimator >或< valueAnimator >。你可以动画元素分组在元素,包括其他元素。<set>是包含其他动画元素的容器(< objectAnimator >、< valueAnimator >或<设置>元素)。代表一个动画集合。

2,<set>是包含其他动画元素的容器(< objectAnimator >、< valueAnimator >或<设置>元素)。代表一个动画集合。

属性
android:interpolator  插入资源。一个在动画插入应用。的值必须是一个引用资源指定一个插入(不是一个插入类名)。有默认插入可用资源的平台或您可以创建自己的插入器资源。请参阅下面的讨论更多关于插入。
android:shareInterpolator 布尔类型。“真正的”如果你想共享相同的插入器在所有子元素。

2.1,alpha 淡入淡出 淡入或淡出动画。表示一个AlphaAnimation

android:fromAlpha  
    Float. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque. 

android:toAlpha  
    Float. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.For more attributes supported by <alpha>, 
    see the Animation class reference (of which, all XML attributes are inherrited by this element).

2.2,scale缩放一个调整动画。您可以指定图像的中心点,它向外生长(或进口)通过指定pivotX pivotY。例如,如果这些值0,0(左上角),所有增长将向下和向右。代表一个ScaleAnimation。

android:fromXScale 
    Float. Starting X size offset, where 1.0 is no change. 

android:toXScale 
    Float. Ending X size offset, where 1.0 is no change. 

android:fromYScale 
    Float. Starting Y size offset, where 1.0 is no change. 

android:toYScale 
    Float. Ending Y size offset, where 1.0 is no change. 

android:pivotX 
    Float. The X coordinate to remain fixed when the object is scaled. 

android:pivotY 
    Float. The Y coordinate to remain fixed when the object is scaled. 

2.3,translate平移 一个垂直和/或水平运动。支持以下属性有下列三种格式:价值观与“%”从-100年到100年结束,表明比例相对于本身,价值从-100年到100年结束在“% p”,指示一个百分比相对于其父;没有后缀的浮动值,指示一个绝对的价值。代表一个TranslateAnimation。

android:fromXDelta 
    Float or percentage. Starting X offset. Expressed either: in pixels relative to the normal position (such as "5"),
     in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p"). 

android:toXDelta 
    Float or percentage. Ending X offset. Expressed either: in pixels relative to the normal position (such as "5"), 
    in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p"). 

android:fromYDelta 
    Float or percentage. Starting Y offset. Expressed either: in pixels relative to the normal position (such as "5"), 
    in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p"). 

android:toYDelta 
    Float or percentage. Ending Y offset. Expressed either: in pixels relative to the normal position (such as "5"), 
    in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p"). 

2.4,rotate一个旋转动画。代表一个RotateAnimation

android:fromDegrees 
    Float. Starting angular position, in degrees. 

android:toDegrees 
    Float. Ending angular position, in degrees. 

android:pivotX 
    Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object's left edge (such as "5"), 
    in percentage relative to the object's left edge (such as "5%"), or in percentage relative to the parent container's left edge (such as "5%p"). 

android:pivotY 
    Float or percentage. The Y coordinate of the center of rotation. Expressed either: in pixels relative to the object's top edge (such as "5"), 
    in percentage relative to the object's top edge (such as "5%"), or in percentage relative to the parent container's top edge (such as "5%p"). 

事例:在Rnim文件夹下新建文件hyperspace_jump.xml
这里写图片描述
引用

ImageView image = (ImageView) findViewById(R.id.image);
Animation hyperspaceJump=AnimationUtils.loadAnimation(this,R.anim.hyperspace_jump);
image.startAnimation(hyperspaceJump);

三,Frame animation XML中定义的动画显示一个图像序列(如电影)。

1,语法
这里写图片描述

1.1 <animation-list> 这一定是根元素。包含一个或多个< item >元素。
    android:oneshot 布尔。“真正的”如果你想执行一次动画;“false”循环动画。

1.2 <item> 单帧动画。必须是一个孩子的< animation-list >元素。
    android:drawable drawable资源。用于绘制的帧。
    android:duration 整数。显示帧持续时间,以毫秒为单位。

事例:在Rnim文件夹下新建rocket_thrust.xml
这里写图片描述
引用

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();

到此,动画讲解就结束了,借此自我总结一下,也希望能帮到用到者

电动汽车数据集:2025年3K+记录 真实电动汽车数据:特斯拉、宝马、日产车型,含2025年电池规格和销售数据 关于数据集 电动汽车数据集 这个合成数据集包含许多品牌和年份的电动汽车和插电式车型的记录,捕捉技术规格、性能、定价、制造来源、销售和安全相关属性。每一行代表由vehicle_ID标识的唯一车辆列表。 关键特性 覆盖范围:全球制造商和车型组合,包括纯电动汽车和插电式混合动力汽车。 范围:电池化学成分、容量、续航里程、充电标准和速度、价格、产地、自主水平、排放、安全等级、销售和保修。 时间跨度:模型跨度多年(包括传统和即将推出的)。 数据质量说明: 某些行可能缺少某些字段(空白)。 几个分类字段包含不同的、特定于供应商的值(例如,Charging_Type、Battery_Type)。 各列中的单位混合在一起;注意kWh、km、hr、USD、g/km和额定值。 列 列类型描述示例 Vehicle_ID整数每个车辆记录的唯一标识符。1 制造商分类汽车品牌或OEM。特斯拉 型号类别特定型号名称/变体。型号Y 与记录关联的年份整数模型。2024 电池_类型分类使用的电池化学/技术。磷酸铁锂 Battery_Capacity_kWh浮充电池标称容量,单位为千瓦时。75.0 Range_km整数表示充满电后的行驶里程(公里)。505 充电类型主要充电接口或功能。CCS、NACS、CHAdeMO、DCFC、V2G、V2H、V2L Charge_Time_hr浮动充电的大致时间(小时),上下文因充电方法而异。7.5 价格_USD浮动参考车辆价格(美元).85000.00 颜色类别主要外观颜色或饰面。午夜黑 制造国_制造类别车辆制造/组装的国家。美国 Autonomous_Level浮点自动化能力级别(例如0-5),可能包括子级别的小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值