您的位置:首页 > 理论基础 > 计算机网络

(三十九)android动画 Animation四大属性 详解(转载:http://www.android100.org/html/201304/25/2295.html)

2015-01-26 19:24 441 查看
一、Animation主要有四大属性,分别是淡入淡出,绕轴旋转,变化大小,位移变化

二、四大属性的共同的方法

1、setDuration(long durationMills):设置动画持续的时间(单位:毫秒)

2、setFillAfter(boolean fillAfter):如果fillAfter的值为true,则动画执行后看,控件将停留在执行结束的状态

3、setFillBefore(boolean fillBefore):如果fillBefore的值为true,则动画执行后看,控件将停留在动画执行之前的状态

4、setRepeatCount(int repeatCount):设置动画重复执行的次数

三、四种动画的构造函数的各个参数的值代表的意思

3、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毫秒


3.2 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毫秒


3.3 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毫秒


3.4 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毫秒


四、一个例子程序

4、1 新建tween_anim_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgTween"
android:src="@drawable/c01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1.0" />
<Button
android:id="@+id/btnControl"
android:text="开始"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>


4.2 TweenAnimationDemo.java文件如下所示:

package com.bison;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class TweenAnimationDemo extends Activity implements OnClickListener {
// 声明一个开始停止的标识符
private boolean flags = true;
private ImageView imgTween;
private Button btnCtrl;
private AnimationSet as;

/** 初始化 */
public void init() {
// 声明AnimationSet
as = new AnimationSet(true);
// 声明Alpha、Scale、Translate、Rotate 等Animation
AlphaAnimation aa = alphaAnim(1, 0.3f);
ScaleAnimation sa = scaleAnim(0.2f, 1.0f, 0.2f, 1.0f, 1, 1);
TranslateAnimation ta = translateAnim(50f, 100f, 50f, 100f);
RotateAnimation ra = rotateAnim(0, 360);
// 添加各种动画
as.addAnimation(aa);
as.addAnimation(sa);
as.addAnimation(ta);
as.addAnimation(ra);

imgTween = (ImageView) findViewById(R.id.imgTween);
imgTween.setScaleType(ScaleType.CENTER_INSIDE);

btnCtrl = (Button) findViewById(R.id.btnControl);
btnCtrl.setOnClickListener(this);

}

/** 缩放 */
private ScaleAnimation scaleAnim(float start_x, float end_x, float start_y,
float end_y, float x2, float y2) {
// 开始x坐标伸缩尺寸,结束x坐标伸缩尺寸,开始y坐标伸缩尺寸,结束y坐标伸缩尺寸,x轴的百分比,y轴的百分比
ScaleAnimation sa = new ScaleAnimation(start_x, end_x, start_y, end_y,
x2, y2);
sa.setDuration(3000);
sa.setRepeatMode(Animation.REVERSE);
sa.setRepeatCount(5);
return sa;
}

/** 透明度 */
private AlphaAnimation alphaAnim(float x, float y) {
AlphaAnimation aa = new AlphaAnimation(x, y);
aa.setDuration(2000);
aa.setRepeatMode(Animation.REVERSE);
aa.setRepeatCount(5);
return aa;
}

/** 移动 */
private TranslateAnimation translateAnim(float startX, float endX,
float startY, float endY) {
TranslateAnimation ta = new TranslateAnimation(startX, endX, startY,
endY);
ta.setDuration(3000);
ta.setRepeatMode(Animation.REVERSE);
ta.setRepeatCount(5);
return ta;
}

/** 旋转 */
private RotateAnimation rotateAnim(float startDegrees, float endDegrees) {
RotateAnimation ra = new RotateAnimation(startDegrees, endDegrees);
ra.setDuration(3000);
ra.setRepeatMode(Animation.RESTART);
ra.setRepeatCount(5);
return ra;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tween_anim_layout);
init();
}

public void onClick(View v) {
if (flags) {
btnCtrl.setText("停止");
imgTween.startAnimation(as);
flags = false;
} else {
btnCtrl.setText("开始");
imgTween.clearAnimation();
flags = true;
}
}
}
PS:这个方法比在XML中定义要好,可以传参,修改等,更方便操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐