欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

浅谈Android动画(一)

程序员文章站 2024-03-25 23:38:16
...

Android的动画可分为三种

View动画
帧动画
属性动画

View动画

View动画有四种效果:

  • 平移:TranslateAnimation
  • 缩放:ScaleAnimation
  • 旋转:RotateAnimation
  • 透明:AlphaAnimation

它们对应的XML文件的标签为:
- <translate/>
- <scale/>
- <rotate/>
- <alpha/>

下面通过具体的代码来说明如何使用和对应属性的意义

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true"
    android:fillAfter="true"
    >
    <alpha
        android:duration="1000"
        android:fromAlpha="0.5"
        android:toAlpha="1.0"/>
    <rotate
        android:duration="1000"
        android:pivotY="50%"
        android:pivotX="50%"
        android:fromDegrees="0"
        android:toDegrees="+180"/>
    <scale
        android:duration="1000"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5"/>
    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="180"
        android:toYDelta="180"/>
</set>
  • set标签代表动画的集合,指的是把以上所有动画集在一起。
  • translate:
    fromXDelta:表示X的起始值
    toXDelta :表示X的结束值
    fromYDelta:表示Y的起始值
    toYDelta:表示Y的结束值

  • scale
    fromXScale:水平缩放是起始值
    toXScale:水平缩放的结束值
    fromYScale:竖直缩放的起始值
    toYScale:竖直缩放的结束值
    pivotX:缩放点的X坐标
    pivotY:缩放点的Y坐标
    缩放的默认点是中心

  • rotate
    fromDegrees:旋转的最开始角度
    toDegrees:旋转结束的角度
    pivotY:旋转点的Y坐标
    pivotX:旋转点的X坐标

  • alpha
     fromAlpha:透明度起始值
     toAlpha:透明度结束值

共同属性
duration:表示动画持续的时间
filAfter:表示是否停留在动画结束后的位置

如何在代码中使用定义好的动画呢?

 Animation animation= AnimationUtils.loadAnimation(AnimationActivity.this,
                R.anim.定义好的资源文件);
        mImageView.startAnimation(animation);

简简单单的两句代码便可以让定义好的动画生效。

帧动画

帧动画的使用也很简单,就是把一组图片先预定好,然后在代码中进行使用,下面给个例子:
- 先在资源文件预定好:

<?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/dog_ic01" android:duration="500"/>
<item android:drawable="@drawable/dog_ic02" android:duration="500"/>
<item android:drawable="@drawable/dog_ic03" android:duration="500"/>
<item android:drawable="@drawable/dog_ic04" android:duration="500"/>
</animation-list>
  • 在代码中使用定义好的动画文件
ImageView img=findViewById(R.id.img);
img.setBackgroundResource(R.drawable.anim);
AnimationDrawable animDrawable=img.getBackground();
animDrawable.start();

这样便可以播放动画。

属性动画

属性动画是API11加入的,与View动画不同,它可以对任何对象做动画。
其中有ValueAnimator、ObjectAnimator、AnimatorSet等。
为了做到兼容,可以采用nineoldandroids这个动画库。

属性动画的使用

  • 让一个对象随着Y轴平移
ObjectAnimator.ofFloat(mImageView,"translationY",mImageView.getHeight()).start();
  • 让一个对象的背景颜色改变
ValueAnimator colorAnim=ObjectAnimator.ofInt(mImageView,"backgroundColor",0x000000,0xFFFFFF);
        colorAnim.setDuration(3000);//动画时间
        colorAnim.setEvaluator(new ArgbEvaluator());//估值器
        colorAnim.setInterpolator(new LinearInterpolator());//匀速
        colorAnim.setRepeatCount(3);//动画次数
        colorAnim.setRepeatMode(ValueAnimator.REVERSE);
        colorAnim.start();
  • 让一个对象同时用有多个动画
AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(mImageView, "rotationX", 0, 360),
                ObjectAnimator.ofFloat(mImageView, "rotationY", 0, 360),
                ObjectAnimator.ofFloat(mImageView, "rotation", 0, 90),
                ObjectAnimator.ofFloat(mImageView, "translationX", 0, 100),
                ObjectAnimator.ofFloat(mImageView, "translationY", 0, 100),
                ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 1.5f),
                ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 0.5f),
                ObjectAnimator.ofFloat(mImageView, "alpha", 1, 0.5f, 1)
        );
        set.setDuration(5000).start();

属性动画与View动画不同的另一点是:用代码动态创建比较好。

以上是动画的基本用法介绍。时候不早啦,祝大家做个好梦。