Android 动画

帧动画 FrameAnimation

  • 帧动画其实就是多张图片连续播放实现动画效果
  • 在drawable文件夹中定义XML帧动画文件(根节点为animation-list)
  • 其中XML文件中的oneshot属性可选值为true或者false,指定是否循环播放,true表示只播放一次,false表示循环播放

先上图:

GIF图片

帧动画XML文件定义如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/g1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g2"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g3"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g4"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g5"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g6"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g7"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g8"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g9"
        android:duration="200"/>
    <item
        android:drawable="@drawable/g10"
        android:duration="200"/>

</animation-list>

在Activity中播放动画代码如下:

    ImageView iv = (ImageView) findViewById(R.id.iv);
    iv.setBackgroundResource(R.drawable.animation);
    AnimationDrawable animationDrawable =(AnimationDrawable) iv.getBackground();
    animationDrawable.start();

这里是直接在onCreate()中调用start()方法的,但是Google官方并不建议这么做,因为在onCreate()中,AnimationDrawable可能还没有完全的绑定到窗口中。如果想不和用户有任何交互就立即播放动画,Google推荐在onWindowFocusChanged()方法中进行帧动画的播放,即界面获得到焦点的时候会回调onWindowFocusChanged()方法。

补间动画 TweenAnimation

  • 从初始状态到终止状态之间的一个转变过程
  • 常用的四种动画效果:位移,旋转,缩放,透明度(Translate,Rotate, Scale, Alpha)

为ImageView对象设置位移动画

    TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 100);
    animation.setDuration(5000);
    iv.startAnimation(animation);

TranslateAnimation构造方法说明:

/**
 * @params fromXDelta 动画的起始x坐标,相对于ImageView自身位置的x偏移量
 * @params toXDelta 动画的结束x坐标,相对于ImageView自身位置的x偏移量
 * @params fromYDelta 动画的起始y左标,相对于ImageView自身位置的y偏移量
 * @params toYDelta 动画的结束y坐标,相对于ImageView自身位置的y偏移量
 */
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)


/**
 * @params fromXType fromXValue值得参考,可选Animation.RELATIVE_TO_SELF,Animation.ABSOLUTE,和Animation.RELATIVE_TO_PARENT
 * Animation.RELATIVE_TO_SELF 起始位置的x坐标为ImageView自身宽度的fromXValue倍+ImageView的自身x坐标
 * Animation.ABSOLUTE fromXValue和fromYValue的值是绝对的,如同第一个构造方法效果相同
 * Animation.RELATIVE_TO_PARENT (百分比)起始位置的x坐标为父控件的宽度的fromXValue倍+ImageView的自身x坐标
 *
 * @params fromYType 类似于fromXType
 */
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

/**
 * TranslateAnimation从XML文件中加载资源
 */
TranslateAnimation(Context context, AttributeSet attrs)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值