当前位置: 代码迷 >> Android >> Android中应用animation的方法
  详细解决方案

Android中应用animation的方法

热度:7   发布时间:2016-05-01 15:49:19.0
Android中使用animation的方法
Android中使用animation的方法

在Android中,分别可以在xml中定义Animation,也可以在程序代码中定义,
下面的小例子是利用RotateAnimation简单展示一下两种方法的用法,对于其他动画,如ScaleAnimation,AlphaAnimation,原理是一样的。


方法一:在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>

使用动画的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.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);
}
}


补充说明:
Android动画解析 --JavaCode


AlphaAnimation

① AlphaAnimation类对象定义
private AlphaAnimation myAnimation_Alpha;
复制代码
② AlphaAnimation类对象构造

AlphaAnimation(float fromAlpha, float toAlpha)
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
//说明:
//??????????????? 0.0表示完全透明
//??????????????? 1.0表示完全不透明
复制代码
③ 设置动画持续时间

myAnimation_Alpha.setDuration(5000);
//设置时间持续时间为 5000毫秒
复制代码



ScaleAnimation

① ScaleAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;
复制代码
② ScaleAnimation类对象构造

ScaleAnimation(float fromX, float toX, float fromY, float toY,
?????????? int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸???
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸?????
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸???
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸??
/*说明:
??????????????????? 以上四种属性值???
??????????????????? 0.0表示收缩到没有
??????????????????? 1.0表示正常无伸缩?????
??????????????????? 值小于1.0表示收缩??
??????????????????? 值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型??
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型???
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
???????????? Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
复制代码
③ 设置动画持续时间

myAnimation_Scale.setDuration(700);
//设置时间持续时间为 700毫秒
复制代码



TranslateAnimation


① TranslateAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;
复制代码
② TranslateAnimation类对象构造

TranslateAnimation(float fromXDelta, float toXDelta,
?????????????????????? float fromYDelta, float toYDelta)
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置???
//第二个参数toXDelta为动画结束时 X坐标上的移动位置??????
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置?????
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
复制代码
③ 设置动画持续时间

myAnimation_Translate.setDuration(2000);
//设置时间持续时间为 2000毫秒
复制代码


RotateAnimation
①  RotateAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;
复制代码

②  RotateAnimation类对象构造

RotateAnimation(float fromDegrees, float toDegrees,
??????????? int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一个参数fromDegrees为动画起始时的旋转角度???
//第二个参数toDegrees为动画旋转到的角度???
//第三个参数pivotXType为动画在X轴相对于物件位置类型??
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型???
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
?????????????? Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
复制代码

③  设置动画持续时间

myAnimation_Rotate.setDuration(3000);

//设置时间持续时间为 3000毫秒
复制代码


如何使用 Java 代码中的动画效果

使用从 View 父类继承过来的方法 startAnimation ()来为 View 或是子类 View 等等添加一个动画效果

public void startAnimation (Animation animation)
  相关解决方案