Android animation动画,android组件化

下面给出一个完整的XML定义(SDK提供)

<set android:shareInterpolator=“false” xmlns:android=“http://schemas.android.com/apk/res/android”>

<scale

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromXScale=“1.0”

android:toXScale=“1.4”

android:fromYScale=“1.0”

android:toYScale=“0.6”

android:pivotX=“50%”

android:pivotY=“50%”

android:fillAfter=“false”

android:duration=“700” />

<set android:interpolator="@android:anim/decelerate_interpolator">

<scale

android:fromXScale=“1.4”

android:toXScale=“0.0”

android:fromYScale=“0.6”

android:toYScale=“0.0”

android:pivotX=“50%”

android:pivotY=“50%”

android:startOffset=“700”

android:duration=“400”

android:fillBefore=“false” />

<rotate

android:fromDegrees=“0”

android:toDegrees="-45"

android:toYScale=“0.0”

android:pivotX=“50%”

android:pivotY=“50%”

android:startOffset=“700”

android:duration=“400” />

</set>

</set>

Tween Animation如何使用

使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

//main.xml中的ImageView

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);

//加载动画

Animation hyperspaceJumpAnimation =AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);

//使用ImageView显示动画

spaceshipImage.startAnimation(hyperspaceJumpAnimation);

如何在Java代码中定义动画

//在代码中定义 动画实例对象

private Animation myAnimation_Alpha;

private Animation myAnimation_Scale;

private Animation myAnimation_Translate;

private Animation myAnimation_Rotate;

//根据各自的构造方法来初始化一个实例对象

myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);

myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,

Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);

myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,

Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

interpolator的解释

interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。

Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

AccelerateDecelerateInterpolator在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator在动画开始的地方速率改变比较慢,然后开始加速

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

x">CycleInterpolator动画循环播放特定的次数,速率改变沿着正弦曲线DecelerateInterpolator在动画开始的地方速率改变比较慢,然后开始减速LinearInterpolator在动画的以均匀的速率改变

Frame Animation

Frame Animation是顺序播放事先做好的图像,跟电影类似。不同于animation package, Android SDK提供了另外一个类AnimationDrawable来定义、使用Frame Animation。

Frame Animation可以在XML Resource定义(还是存放到res\anim文件夹下),也可以使用AnimationDrawable中的API定义。由于Tween Animation与Frame Animation有着很大的不同,因此XML定义的格式也完全不一样,其格式是:**首先是animation-list根节点,animation-list根节点中包含多个item子节点,每个item节点定义一帧动画,当前帧的drawable资源和当前帧持续的时间。**下面对节点的元素加以说明:

XML属性说明
drawable当前帧引用的drawable资源
duration当前帧显示的时间(毫秒为单位)
oneshot如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。
variablePaddingIf true, allows the drawable’s padding to change based on the current state that is selected.
visible规定drawable的初始可见性,默认为flase;

下面就给个具体的XML例子,来定义一帧一帧的动画:

<animation-list xmlns:android=“http://schemas.android.com/apk/res/android”

android:oneshot=“true”>

<item android:drawable="@drawable/rocket_thrust1" android:duration=“200” />

<item android:drawable="@drawable/rocket_thrust2" android:duration=“200” />

<item android:drawable="@drawable/rocket_thrust3" android:duration=“200” />

上面的XML就定义了一个Frame Animation,其包含3帧动画,3帧动画中分别应用了drawable中的3张图片:rocket_thrust1,rocket_thrust2,rocket_thrust3,每帧动画持续200毫秒。

然后我们将以上XML保存在res/anim/文件夹下,命名为rocket_thrust.xml,显示动画的代码:

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);

rocketImage**.setBackgroundResource**(R.anim.rocket_thrust);

rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

}

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

rocketAnimation**.start();**

return true;

}

return super.onTouchEvent(event);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值