Android动画开发
本文主要研究Android中的三种动画,第一种是单个View的各种动画效果,第二种是两个Activity切换时的动画效果,第三种是类似于Gif的Frame By Frame动画效果,其中View的各种动画包括在xml文件中定义和代码中定义两种方式。 一、动画基本类型: 如下表所示,Android的动画由四种类型组成,即可在xml中定义,也可在代码中定义,如下所示:
XML
CODE
渐变透明度动画效果
alpha
AlphaAnimation
渐变尺寸伸缩动画效果
scale
ScaleAnimation
画面转换位置移动动画效果
translate
TranslateAnimation
画面转移旋转动画效果
rotate
RotateAnimation
二、如何在XML文件中定义动画 1.alpha
四、如何在java代码中定义动画 1.代码: //在代码中定义 动画实例对象 private Animation myAnimation_Alpha; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation myAnimation_Rotate;
//根据各自的构造方法来初始化一个实例对象 myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f); 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_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f); myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 2. 分析: 1.1 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毫秒
1.2 ScaleAnimation
第一步:ScaleAnimation类对象定义
private AlphaAnimation myAnimation_Scale;
第二步: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毫秒
1.3 TranslateAnimation
第一步:TranslateAnimation类对象定义
private AlphaAnimation myAnimation_Translate;
第二步:TranslateAnimation类对象构造
TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta)
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置 //第二个参数toXDelta为动画结束时 X坐标上的移动位置 //第三个参数fromYDelta为动画起始时Y坐标上的移动位置 //第四个参数toYDelta为动画结束时Y坐标上的移动位置
第三步:设置动画持续时间 m myAnimation_Translate.setDuration(2000);
1.4 RotateAnimation 第一步:RotateAnimation类对象定义 private AlphaAnimation myAnimation_Rotate;
第二步: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);
五、如何使用java代码中的动画效果
使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画效果
public void startAnimation (Animation animation)
Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。Frame动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。
实现一个人跳舞的Frame动画,6张图片如下所示:
1、把这6张图片放到res/drawable目录下,分别取名为:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。 2、在res/anim目录下创建一个XML配置文件,文件名为:dance.xml,文件内容:
<!-- 动画控制按钮 -->
<LinearLayout apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:orientation="horizontal">
<Button apk:text="开始" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onStartDance" />
<Button apk:text="结束" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onStopDance" />
</LinearLayout>
import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import com.aboy.android.study.R;
/**
-
Frame动画 */ public class FrameActivity extends Activity { public static final String TAG = "FrameActivity";
// 显示动画的组件 private ImageView imgDance; // Frame动画 private AnimationDrawable animDance;
/**
-
@see android.app.Activity#onCreate(android.os.Bundle) */ public void onCreate(Bundle cycle) { super.onCreate(cycle); super.setContentView(R.layout.dance);
// 实例化组件 this.imgDance = (ImageView) super.findViewById(R.id.ImgDance);
// 获得背景(6个图片形成的动画) this.animDance = (AnimationDrawable) this.imgDance.getBackground(); }
/**
- 按钮:开始‘跳舞’动画 */ public void onStartDance(View view) { this.animDance.start(); }
/**
- 按钮:停止‘跳舞’动画 */ public void onStopDance(View view) { this.animDance.stop(); }
-
} 代码就那么的几行,运行之后,点击“开始”按钮后,动画一直不停的播放,直到点击“停止”为止。