Android中的补间动画

在动画中一般有三种动画方式:

补间动画;逐针动画;属性动画。

首先补间动画的实现步骤:

1.在res目录下建立文件夹anim

2.在anim文件夹下建立animation.xml动画文件

3.在animation.xml文件中描述某种动画的属性

4.在代码中利用AnimationUtils.loadAnimation方法加载动画文件

5.在代码中用View的startAnimation方法启动动画。

根据动画效果一般为:渐变动画(AlphaAnimation)

渐变动画的属性有:

fromAlpha动画起始时透明度,取值在0.0-1.0之间

toAlpha 动画结束时透明度,取值在0.0-1.0之间

duration 动画持续时间,以毫秒为单位

repeatCount 动画重复次数,这个重复次数不包括第一次播放。

实现代码:

xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="2000"
        android:repeatCount="3"
    ></alpha>
</set>
java代码:

 public void qidong01(View v){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);  //加载xml文件为对象  
        iv.clearAnimation();
        iv.startAnimation(animation);   //赋值给ImageView   启动
    }
第二种实现方法 只用java代码

 //第二个启动动画事件
    public void qidong02(View v){
        Animation animation;
        animation = new AlphaAnimation(0.1f, 1.0f);  //设置透明度起点和终点
        animation.setDuration(2000);   //设置时长
        animation.setRepeatCount(2);   //设置次数

        iv.clearAnimation();  //删除动画
        iv.startAnimation(animation);  // 启动动画
    }
缩放动画(ScaleAnimation)

主要属性有:

fromXScale 动画起始时X坐标上的伸缩倍数0.0表示收缩到没有

fromYScale 动画起始时Y坐标上的伸缩倍数

toXScale 动画终止时X坐标上的伸缩倍数2.0表示扩大2倍

toYScale 动画终止时Y坐标上的伸缩倍数

pivotX  X轴上的伸缩参考点

pivotY  Y轴上的伸缩参考点50%代表X或Y方向坐标上的中点位置

duration 动画持续时间,以毫秒为单位
repeatCount 动画重复次数,这个重复次数不包括第一次播放。

实现代码:

xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="2.0"
        android:toYScale="2.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        />

</set>
java代码:

public void qidong01(View v){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
        iv.clearAnimation();
        iv.startAnimation(animation);
    }
只用java实现的时候的代码:

public void qidong02(View v){
        Animation animation;
        animation = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  //设置x起点终点,y起点终点,以自身角度以50%的点为参考点扩展
        animation.setDuration(2000);   //设置时长
        animation.setRepeatCount(2);   //设置次数

        iv.clearAnimation();  //删除动画
        iv.startAnimation(animation);  // 启动动画
    }
平移动画(TranslateAnimation)  一般的主要属性有:

fromXDelta 动画起始时X坐标上的位置

fromYDelta 动画起始时Y坐标上的位置

toXDelta 动画终止时X坐标上的位置

toYDelta 动画终止时Y坐标上的位置

xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="1080"
        android:toXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="0"
        android:duration="2000"
        />

</set>
java代码:

public void qidong01(View v){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.trans);
        iv.clearAnimation();
        iv.startAnimation(animation);
    }
只用java代码实现的时候代码:

 public void qidong02(View v){
        Animation animation;
        animation = new TranslateAnimation(1080.0f, 0.0f, 0.0f, 0.0f);  //设置x的起始位置,终止位置;Y的起始位置,终止位置。
        animation.setDuration(2000);   //设置时长
        animation.setRepeatCount(2);   //设置次数

        iv.clearAnimation();  //删除动画
        iv.startAnimation(animation);  // 启动动画
    }
旋转动画(RotateAnimation) 主要属性

fromDegrees 动画起始时的角度

toDegrees 动画终止时的角度

pivotX 旋转围绕点的X轴上相对位置,50% 代表自己的中点

pivotY 旋转围绕点的Y轴上相对位置,50% 代表自己的中点

实现代码:

xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="1080"
        android:toXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="0"
        android:duration="2000"
        />

</set>
java代码:

 public void qidong01(View v){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        iv.clearAnimation();
        iv.startAnimation(animation);
    }
只用java代码实现的代码“

public void qidong02(View v){
        Animation animation;
        animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  //设置起始角度,终止角度,相对于自己,中点,相对于自己,中点
        animation.setDuration(2000);   //设置时长
        animation.setRepeatCount(2);   //设置次数

        iv.clearAnimation();  //删除动画
        iv.startAnimation(animation);  // 启动动画
    }<pre name="code" class="html">

 

逐针动画(FrameAnimation)

实现步骤:

1.在drawable目录下放置动画帧图片

2.在drawable文件夹下建立animation.xml动画文件

3.在animation.xml文件中描述每一帧

4.在代码中利用view.setBackgroundResource方法加载动画文件

5.在代码中用AnimationDrawable的start()方法启动动画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/loading_1" android:duration="80" />
    <item android:drawable="@drawable/loading_2" android:duration="80" />
    <item android:drawable="@drawable/loading_3" android:duration="80" />
    <item android:drawable="@drawable/loading_4" android:duration="80" />
    <item android:drawable="@drawable/loading_5" android:duration="80" />
    <item android:drawable="@drawable/loading_6" android:duration="80" />
    <item android:drawable="@drawable/loading_7" android:duration="80" />
    <item android:drawable="@drawable/loading_8" android:duration="80" />
</animation-list>
java代码:

 public void qidong01(View v){

        iv.setBackgroundResource(R.drawable.fram);

        AnimationDrawable animationable = (AnimationDrawable) iv.getBackground();
        animationable.start();
    }
只用代码实现:

public void qidong02(View v){
        AnimationDrawable animationDrawable = new AnimationDrawable();//帧动画对象
        iv.setBackgroundDrawable(animationDrawable);//设置组件背景
        String packageName = this.getApplicationContext().getPackageName();//获取当前包名
        for (int i = 1; i <= 8; i++) {
            //从图片名称反射资源ID
            int id = this.getResources().getIdentifier("loading_"+i, "drawable",  packageName);
            Drawable frame = this.getResources().getDrawable(id);//得到动画帧的对象
            animationDrawable.addFrame(frame, 80);//添加帧,设定时间间隔
        }
        animationDrawable.setOneShot(false);// 只运行一次,false-无限循环
        animationDrawable.start();// 开始播放
    }

























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值