animation相关类

本文详细介绍了Android中的各种动画实现方式,包括AlphaAnimation、ScaleAnimation、TranslateAnimation和RotateAnimation等,并探讨了AnimationSet如何组合多种动画效果。

LinearInterpolator

变化率是个常数,即 f (x) = x.

方法:public float getInterpolation(float input)

AccelerateInterpolator--开始变化很慢,然后逐渐变快,即 f(x) = x*x 或者 f(x) = pow(x, 2*mFactor).

方法:public float getInterpolation(float input)

AccelerateDecelerateInterpolator--变化率开始和结束都很慢,但中间很快,即 f(x) = (cos ((x+1)*PI) / 2.0f) + 0.5f.

方法:public float getInterpolation(float input)

LinearInerpolator、AccelerateInterpolator, DecelerateInterpolator, AccelerateDecelerateInterpolator,CycleInterpolator 是 Interpolator 的子类,分别实现了匀速、加速、减速、变速、循环等效果。

AlphaAnimation

控制透明度

构造函数:

@param开始的透明度

@param结束的透明度

AlphaAnimation(float fromAlpha, float toAlpha)

方法:

willChangeBounds()                                     具体不详,不过其返回值是固定的都是false

willChangeTransformationMatrix()         false

ScaleAnimation

控制尺寸

构造函数:

@param开始时x方向的伸缩比列

@param结束时x方向的伸缩比列

@param开始时y方向的伸缩比列

@param结束时y方向的伸缩比列

ScaleAnimation(float fromX, float toX, float fromY, float toY)

@param动画在x轴的伸缩位置0%-100%中取值,50%为物件的XY方向坐标上的中点位置

@param动画在y轴的伸缩位置

ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)

@param  pivotXType动画在X轴相对于物件位置类型--固定的一些数据

@param  pivotYType动画在X轴相对于物件位置类型

pivotXValuepivotYValue同上面的pivotXpivotY

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

方法:

     initialize(int width, int height, int parentWidth, int parentHeight)设置动画的大小根据父类的维数(猜测)

TranslateAnimation

移动

构造函数:

@param开始时x的位置

@param结束时x的位置

@param开始时y的位置

@param结束时y的位置

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

@param参考上面ScaleAnimation

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)

方法:

@paramScaleAnimation

initialize(int width, int height, int parentWidth, int parentHeight)

RotateAnimation

旋转

构造函数:

@param开始的角度

@param结束的角度

RotateAnimation(float fromDegrees, float toDegrees)

@param动画在x轴的旋转位置

@param动画在x轴的旋转位置

RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

方法:

@paramScaleAnimation

         initialize(int width, int height, int parentWidth, int parentHeight)

Animation

抽象类,所有控制动画的都继承与该类

Aniamtion 是基类,他记录了动画的通用属性和方法。主要的属性包括动画持续时间、重复次数、interpolator等。

方法:---只列举了几个方法,用法很简单,可以去看sdk

getTransformation (currentTime, outTransformation)

该方法根据当前时间 (currentTime) 和 interpolator,计算当前的变换,在 outTransformation 中返回。

setRepeatMode(int)—在测试AnimationSet是该函数没有成功,可能有什么地方设置错了,以后再跟进

@param Animation.RESTART回到动作开始时的坐标,Animation.REVERSE反转的意思,a->b,在从b->a,Animation.INFINITE不断重复该动作

设置动作结束后,将怎样处理,只有在repeatCount大于0或为INFINITE的时候setRepeatMode才会生效

TranslateAnimation、RotateAnimation、AlphaAnimation、ScaleAnimation等是Animation的子类,分别实现了平移、旋转、改变透明度、缩放等动画。

AnimationSet

设置一个动画组,就是将一系列动作合在一起形成新的动画

可以包含多个Animation,但都是在同一个时间执行的,是并行,不是串行执行的。如果AnimationSet中有一些设定,如duration,fillBefore等,它包含的子动作也设定了的话,子动作中的设定将会给覆盖掉。

构造函数:

     @param是否公用动画插入器,为true表示共用AnimationSet的插入器

AnimationSet(boolean shareInterpolator)

方法:==

     @paramtrue时将在播放完后被应用,播完后停在最后一幅画上,这里重写了Animation的该方法,其他的基本上都是直接使用父类的

     setFillAfter(boolean fillAfter)

@paramtrue时将在开始前被应用,播完后停在第一幅画上,好像不设置该属性也会停留在最开始的那副上面

setFillBefore(boolean fillBefore)

@param在使用AnimationSet的重复mode时,还没有看到有什么效果,但是它的解释和Animation的该方法是一样的

setRepeatMode(int repeatMode)

@param动画停留时间间隔,从上次结束后停留多长时间进入下一次动画

setStartOffset(long startOffset)

@param添加动画

addAnimation(Animation a)

Example

AnimationSet aa = new AnimationSet(true);

    Animation myAnimaton = new TranslateAnimation(0,100,0,100); 

    aa.addAnimation(myAnimaton);

    Animation myAnimaton1 = new RotateAnimation(0,360);

    aa.addAnimation(myAnimaton1);

    myImage.startAnimation(aa);

    aa.start();

    aa.setDuration(2000);

aa.setFillAfter(true);

AnimationUtils

该类没有父类,动画的辅助类.构造函数也没有,使用默认的

方法:

@param可以看到不需要实例化该对象就可以使用,通过资源xml获得一个动画

static Animation loadAnimation(Context context, int id)

TranslateAnimation manmation = (TranslateAnimation)AnimationUtils. loadAnimation(this,R.anim.animation);

@param

LayoutAnimationController loadLayoutAnimation(Context context, int id)

LayoutAnimationController

Layout animation controller is used to animated a layout's, or a view group's, children. 

该类和其衍生类GridLayoutAnimationControllerGridLayoutAnimationController.AnimationParameters等还没有看到相关资料,也没有用过

Animation.Description

Utility class to parse a string description of a size. 

GridLayoutAnimationController

A layout animation controller is used to animated a grid layout's children. 

GridLayoutAnimationController.AnimationParameters

The set of parameters that has to be attached to each view contained in the view group animated by the grid layout animation controller. 

LayoutAnimationController.AnimationParameters

The set of parameters that has to be attached to each view contained in the view group animated by the layout animation controller. 

Transformation                                                                      

Defines the transformation to be applied at one point in time of an Animation. 

xml文件中设置动画可以参考android_xml文件中介绍的,有实例解释

 

AlphaAnimation

渐变透明度动画效果

ScaleAnimation

渐变尺寸伸缩动画效果

TranslateAnimation

画面转换位置移动动画效果

RotateAnimation

画面转移旋转动画效果

transition设置两张图片的过渡,好像只能设置两张之间的过渡,多设置几张也只显示前面两张


### C++ 中实现 Atlas Animation 的设计与示例代码 #### 1. **Atlas 设计** `Atlas` 通常用于管理纹理图集(Texture Atlas),它包含了多个子图像的信息。这些信息可以用来渲染动画帧或其他图形资源。 ##### 功能需求 - 存储纹理路径或 ID。 - 提供方法获取特定子图像的位置和尺寸。 - 支持加载配置文件(如 JSON 或 XML)来初始化子图像的数据。 --- #### 2. **Animation 设计** `Animation` 负责管理和播放基于 `Atlas` 的动画序列。其核心功能包括: - 维护一组关键帧(Frames),每一帧对应 `Atlas` 中的一个子区域。 - 控制动画的播放速度、循环模式以及当前状态。 --- #### 3. **结构与关系** - `Atlas` 是基础组件,提供子图像的相关元数据。 - `Animation` 利用 `Atlas` 数据来定义并播放一系列帧。 两者的关系可以通过依赖注入的方式建立:`Animation` 持有一个指向 `Atlas` 的引用或指针。 --- #### 4. **示例代码** 以下是一个简化版的实现: ```cpp #include <iostream> #include <vector> #include <string> #include <memory> // 定义一个简单的 Rect 结构体表示矩形区域 struct Rect { float x, y; // 左上角坐标 float width, height; // 尺寸 }; /// @brief 图像图集 (Texture Atlas) class Atlas { public: explicit Atlas(const std::string& texturePath) : m_texturePath(texturePath) {} void addSubImage(const std::string& name, const Rect& rect) { m_subImages[name] = rect; } const Rect* getSubImageRect(const std::string& name) const { auto it = m_subImages.find(name); if (it != m_subImages.end()) { return &it->second; } return nullptr; } private: std::string m_texturePath; // 纹理路径 std::unordered_map<std::string, Rect> m_subImages; // 名称 -> 子图像映射 }; /// @brief 动画 class Animation { public: struct Frame { std::string imageName; // 对应 Atlas 中的名称 float duration; // 帧持续时间(秒) }; explicit Animation(std::shared_ptr<Atlas> atlas, const std::vector<Frame>& frames, float frameRate) : m_atlas(atlas), m_frames(frames), m_frameRate(frameRate), m_currentTime(0.f), m_currentIndex(0) {} void update(float deltaTime) { m_currentTime += deltaTime; while (m_currentTime >= 1.f / m_frameRate && !isFinished()) { advanceToNextFrame(); m_currentTime -= 1.f / m_frameRate; } } bool isFinished() const { return m_currentIndex >= m_frames.size(); } const Rect* getCurrentFrameRect() const { if (!isFinished()) { const auto& currentFrameName = m_frames[m_currentIndex].imageName; return m_atlas->getSubImageRect(currentFrameName); } return nullptr; } private: void advanceToNextFrame() { ++m_currentIndex; if (m_currentIndex >= m_frames.size()) { m_currentIndex = 0; // 循环回放 } } std::shared_ptr<Atlas> m_atlas; // 关联的 Texture Atlas std::vector<Frame> m_frames; // 动画帧列表 float m_frameRate; // 帧率 float m_currentTime; // 当前累计时间 size_t m_currentIndex; // 当前帧索引 }; int main() { // 创建 Atlas 并添加一些子图像 std::unique_ptr<Atlas> atlas = std::make_unique<Atlas>("textures/atlas.png"); atlas->addSubImage("frame_1", {0, 0, 64, 64}); atlas->addSubImage("frame_2", {64, 0, 64, 64}); atlas->addSubImage("frame_3", {128, 0, 64, 64}); // 定义动画帧 std::vector<Animation::Frame> frames = { {"frame_1", 0.5f}, {"frame_2", 0.5f}, {"frame_3", 0.5f} }; // 初始化 Animation Animation animation(std::move(atlas), frames, 10); // 10 FPS // 更新动画并打印当前帧位置 for (float t = 0; t < 5; t += 0.1f) { animation.update(0.1f); const Rect* rect = animation.getCurrentFrameRect(); if (rect) { std::cout << "Current Frame: (" << rect->x << ", " << rect->y << ") " << rect->width << "x" << rect->height << "\n"; } else { std::cout << "No active frame.\n"; } } return 0; } ``` --- ### 设计要点解析 1. **模块化分离** - `Atlas` 负责存储静态的子图像信息。 - `Animation` 动态地控制帧切换逻辑。 2. **灵活性** - 使用 `std::shared_ptr` 和 `std::unique_ptr` 进行内存管理,减少手动释放的压力。 - 动画帧的时间间隔可独立设置,适应不同场景需求。 3. **扩展性** - 可轻松扩展支持更多特性,例如反向播放、随机跳转等。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值