Android 使用xml定义动画

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

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

anim/alpha.xml(渐变动画)

<?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>

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

fromAlpha:开始的透明度

toAlpha:结束的透明度

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

anim/scale(伸缩动画)

<?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(移动动画)

<?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(旋转动画的)

<?xml version="1.0" encoding="utf-8"?> <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> 

interpolator:动画插入器。

accelerate_decelerate_interpolator:加速-减速

accelerate_interpolator:加速

decelerate_interpolator:减速

fromDegrees:开始的角度

toDegrees:结束的角度

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


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

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

activity_alpha.xml

<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

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();
			}
		});
	}

}
这段代码对于入门的同学来说应该不是问题了,要注意的是这里
View view=View.inflate(this, R.layout.activity_alpha, null);
setContentView(view);
否则动画是无法start的。之后我设置了动画监听器,可以加上自己喜欢的事件。

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

转载于:https://my.oschina.net/zlLeaf/blog/133547

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值