打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android 使用xml定义动画

 首先在res中新建一个anim的文件夹

在anim中新建需要的动画xml资源文件(这里我把四个都写出来)

anim/alpha.xml(渐变动画)

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="2000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />
</set><span></span>

duration:动画的持续时间(单位是毫秒)

fromAlpha:开始的透明度

toAlpha:结束的透明度

透明度0.0是完全透明,1.0是不透明

anim/scale(伸缩动画)

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="2000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="00%"
        android:pivotY="00%"
        android:toXScale="1.4"
        android:toYScale="1.4" />
</set>

interpolator:动画插入器。

accelerate_decelerate_interpolator:加速-减速

accelerate_interpolator:加速

decelerate_interpolator:减速

fromXScale,fromYScale:开始X和Y的大小(0为缩小到没有)

toXScale,toYScale:结束X和Y的大小(1为没有变化)

pivotX,privotY:左上角的坐标(可以用百分比也可以用数字)

anim/translate(移动动画)

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="2000"
        android:fromXDelta="30"
        android:fromYDelta="30"
        android:toXDelta="-80"
        android:toYDelta="300" />
</set>
fromXDelta,fromYDelta:开始时候左上角的坐标

toXDelta,toYDelta:结束时候左上角的坐标

anim/rotate(旋转动画的)

1
2
3
4
5
6
7
8
9
<span style="font-size:9pt;line-height:1.5;"><?xml version="1.0" encoding="utf-8"?></span> <set xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+360" />
</set> <span></span><span></span>

interpolator:动画插入器。

accelerate_decelerate_interpolator:加速-减速

accelerate_interpolator:加速

decelerate_interpolator:减速

fromDegrees:开始的角度

toDegrees:结束的角度

pivotX,privotY:旋转中心的坐标(可以用百分比也可以用数字)


到此xml资源文件就完成了,接下来就是如何调用这些资源文件。

以alpha.xml为例子,先建立AlphaActivity

activity_alpha.xml

1
2
3
4
5
6
7
8
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/start_bg"
    tools:context=".AlphaActivity" >
</RelativeLayout>
background是我自己的一张图片,可以使用其他的

AlphaActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.animationdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
public class AlphaActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view=View.inflate(this, R.layout.activity_alpha, null);
        setContentView(view);
        Animation animation=AnimationUtils.loadAnimation(this, R.anim.alpha);
         
        view.startAnimation(animation);
        animation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0) {}
             
            @Override
            public void onAnimationRepeat(Animation arg0) {}
             
            @Override
            public void onAnimationEnd(Animation arg0) {
                Toast.makeText(AlphaActivity.this, "动画完成", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
这段代码对于入门的同学来说应该不是问题了,要注意的是这里
1
2
View view=View.inflate(this, R.layout.activity_alpha, null);
setContentView(view);
否则动画是无法start的。之后我设置了动画监听器,可以加上自己喜欢的事件。

通过在http://www.eoeandroid.com/forum.php?mod=viewthread&tid=564学习后写出这篇博文 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android 动画之补间动画
Android动画效果translate(位移)、scale(缩放)、alpha(淡入淡出)、rotate(旋转)
Android 如何在XML文件中定义动画
Android画图学习总结
android animation
Android 使用XML做动画UI的深入解析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服