Android中两种使用Animation的方法

动画类型

Androidanimation由四种类型组成

XML
alpha渐变透明度动画效果
scale渐变尺寸伸缩动画效果
translate画面转换位置移动动画效果
rotate画面转移旋转动画效果


JavaCode
AlphaAnimation渐变透明度动画效果
ScaleAnimation渐变尺寸伸缩动画效果
TranslateAnimation画面转换位置移动动画效果
RotateAnimation画面转移旋转动画效果

Android动画模式

Animation主要有两种动画模式:
一种是tweened animation(渐变动画)
XMLJavaCode
alphaAlphaAnimation
scaleScaleAnimation


一种是frame by frame(画面转换动画)
XMLJavaCode
translateTranslateAnimation
rotate
方法一:在xml中定义动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        
<rotate 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0" 
        android:toDegrees="+360"
        android:duration="3000" />
        
<!-- rotate 旋转动画效果
       属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化
        fromDegrees 属性为动画起始时物件的角度    
        toDegrees   属性为动画结束时物件旋转的角度,+代表顺时针
        duration  属性为动画持续时间,以毫秒为单位
-->
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        
<rotate 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0" 
        android:toDegrees="+360"
        android:duration="3000" />
        
<!-- rotate 旋转动画效果
       属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化
        fromDegrees 属性为动画起始时物件的角度    
        toDegrees   属性为动画结束时物件旋转的角度,+代表顺时针
        duration  属性为动画持续时间,以毫秒为单位
-->
</set>

使用动画的Java代码,程序的效果是点击按钮,TextView旋转一周
package com.ray.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class TestAnimation extends Activity implements OnClickListener{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.Button01);
        btn.setOnClickListener(this);     
    }

	@Override
	public void onClick(View v) {
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);
		findViewById(R.id.TextView01).startAnimation(anim);
	}
}

package com.ray.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class TestAnimation extends Activity implements OnClickListener{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.Button01);
        btn.setOnClickListener(this);     
    }

	@Override
	public void onClick(View v) {
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);
		findViewById(R.id.TextView01).startAnimation(anim);
	}
}

 方法二:直接在代码中定义动画(效果跟方法一类似):

package com.ray.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;

public class TestAnimation extends Activity implements OnClickListener{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.Button);
        btn.setOnClickListener(this);     
    }

	public void onClick(View v) {
		Animation anim = null;
		anim = new RotateAnimation(0.0f,+360.0f);
		anim.setInterpolator(new AccelerateDecelerateInterpolator());
		anim.setDuration(3000);
		findViewById(R.id.TextView01).startAnimation(anim);	
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值