Android 动画机制(一)

Android 动画机制(一)

1.逐帧动画(Frame Animation)

    在Android中实现逐帧动画,就是有设计师给出一系列的状态不断变化的图片,开发者可以指定动画中每一帧对应的图片和持续的时间,然后就可以开始播放动画了,可以通过代码和XML资源文件实现:

  • XML 实现方式

将处理好的图片放入drawable目录中,在res/drawable 目录新建一个xml文件,在文件中使用标签来定义动画帧序列,使用item标签来定义动画的每一帧,指定帧持续的时间等属性。格式如下:

<?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/loading_01"
        android:duration="120"/>
    <item android:drawable="@drawable/loading_02"
        android:duration="120"/>
    <item android:drawable="@drawable/loading_03"
        android:duration="120"/>
	....
</animation-list>

其中 android:onshot 用来控制动画是否循环播放,如果取值为true,表示动画只会播放一次,false表示动画循环播放,
        android:duration 用来指定每一帧的播放持续时间。

  • 代码实现方式
AnimationDrawable animationDrawable = new AnimationDrawable();
for (int i = 0; i < 10; i++) {
     Drawable drawable = context.getResources().getDrawable(
                context.getResources().getIdentifier("loading_"+i,"drawable",context.getPackageName()));
     animationDrawable.addFrame(drawable,120);
 }
imageView.setBackgroundDrawable(animationDrawable);
animationDrawable.setOneShot(false);

代码中帧动画的触发和停止使用:

//获取AnimationDrawable 对象实例,用来控制动画的播放和停止
 AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
//开始播放动画
animationDrawable.start();
//停止播放动画
 animationDrawable.stop();
2.补间动画(Tween Animation)

补间动画是指开发者定义动画的开始和结尾,指定时间和方式,通过在两个关键帧之间插入渐变值来实现过度,从而实现的一种动画效果,主要包括四种基本效果:位移变化(Translate)、透明度变化(Alpha)、大小变化(Scale)、旋转变化(Rotate),这四种效果可以动态组合,从而实现更复杂灵活的动画。

  • TranslateAnimation

改变位置的动画,创建动画时需要制定动画开始和结束的X,Y 坐标,以及动画的持续时间

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="200"
        android:toYDelta="200"
        android:duration="1000"/>
</set>

直接在代码中使用TranslateAnimation:

        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,2f,
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,2f);
        animation.setDuration(2000);
        animation.setFillAfter(true);//执行结束之后停留不动
        animation.setRepeatMode(Animation.REVERSE);//设置重复的模式
        animation.setRepeatCount(1);//设置动画的执行次数为1次
        imageView.setAnimation(animation);

通过AnimationUtils 加载xml动画:

Animation animation = AnimationUtils.loadAnimation(this,R.anim.translate_anim);
imageView.startAnimation(animation);
  • ScaleAnimation

缩放大小的动画,创建动画需制定开始和结束的在X和Y轴的缩放比例,以及动画时间,同时指定缩放的中心点

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale 
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5"
        android:duration="2000"/>
        ScaleAnimation animation = new ScaleAnimation(1.0f,4.0f,1.0f,4.0f,2.0f,2.0f);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        imageView.setAnimation(animation);
  • AlphaAnimation

改变透明度的动画,创建需制定动画开始和结束的透明度和动画时间,透明度取值范围 0–1

  <!-- 1.0 表示完全不透明 0.0表示完全透明 -->
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="2000"/>
        AlphaAnimation animation = new AlphaAnimation(0,1);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        imageView.setAnimation(animation);
  • RotateAnimation

旋转动画,创建是需指定动画的开始和结束的旋转角度,动画时间,同时要指定旋转的中心点,即旋转坐标

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        android:startOffset="100"
        android:repeatMode="restart"
        android:repeatCount="-1"
        /> 
        RotateAnimation animation = new RotateAnimation(0,720,0.5f,0.5f);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        imageView.setAnimation(animation);

重点:补间动画不会改变真实的坐标。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值