tweenAnimation在代码中的定义和实现方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public
void
onClick(View v)
{
switch
(v.getId())
{
case
R.id.button1:
mImageView.startAnimation(AnimationUtils.loadAnimation(
this
, R.anim.alpha));
//点击按钮播放动画
break
;
case
R.id.button2:
Animation animation = AnimationUtils.loadAnimation(
this
, R.anim.translate);
mImageView.startAnimation(animation);
animation.setAnimationListener(
new
AnimationListener()
//监听动画的方法
{
@Override
public
void
onAnimationStart(Animation animation)
{
}
@Override
public
void
onAnimationRepeat(Animation animation)
{
}
@Override
public
void
onAnimationEnd(Animation animation)
{
Toast.makeText(MainActivity.
this
,
"移动完成"
, Toast.LENGTH_LONG).show();
}
});
break
;
}
}
|
1.透明度alpha:
1
2
3
4
5
6
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<alpha xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fromAlpha=
"0.0"
android:toAlpha=
"1.0"
android:duration=
"5000"
>
</alpha>
|
2.位移translate:
1
2
3
4
5
6
7
8
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<translate xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fromXDelta=
"0"
android:fromYDelta=
"60"
android:toXDelta=
"0"
android:toYDelta=
"0"
android:duration=
"5000"
>
</translate>
|
1
|
translate android:fromYDelta=
"-100%p"
|
3.大小scale:
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<scale xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fromXScale=
"0.01"
android:fromYScale=
"0.01"
android:toXScale=
"1"
android:toYScale=
"1"
android:pivotX=
"50%"
android:pivotY=
"50%"
android:duration=
"10000"
>
</scale>
|
4.角度rotate:
1
2
3
4
5
6
7
8
9
10
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<rotate xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fromDegrees=
"-360"
android:toDegrees=
"0"
android:pivotX=
"90%"
android:pivotY=
"90%"
android:repeatCount=
"infinite"
android:duration=
"10000"
>
</rotate>
|
5.set:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<alpha
android:duration=
"3000"
android:fromAlpha=
"0.0"
android:toAlpha=
"1.0"
>
</alpha>
<scale
android:duration=
"3000"
android:fromXScale=
"0.1"
android:fromYScale=
"2"
android:pivotX=
"50%"
android:pivotY=
"50%"
android:toXScale=
"1"
android:toYScale=
"1"
/>
</set>
|
6.代码写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
private
void
initAnimation() {
//透明度控制动画效果 alpha
animation_alpha=
new
AlphaAnimation(
0
.1f,
1
.0f);
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
animation_alpha.setRepeatCount(-
1
);
//设置循环
animation_alpha.setDuration(
5000
);
//设置时间持续时间为 5000毫秒
// 旋转效果rotate
animation_rotate =
new
RotateAnimation(
0
, -
720
,
RotateAnimation.RELATIVE_TO_SELF,
0
.5f,
RotateAnimation.RELATIVE_TO_SELF,
0
.5f);
//第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_rotate.setRepeatCount(-
1
);
animation_rotate.setDuration(
5000
);
//设置时间持续时间为 5000毫秒
//尺寸伸缩动画效果 scale
animation_scale=
new
ScaleAnimation(
0
.1f,
3
.0f,
0
.1f,
3
.0f,Animation.RELATIVE_TO_SELF,
0
.5f,Animation.RELATIVE_TO_SELF,
0
.5f);
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
/*说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_scale.setRepeatCount(-
1
);
animation_scale.setDuration(
5000
);
//设置时间持续时间为 5000毫秒
//移动动画效果translate
animation_translate=
new
TranslateAnimation(-20f,300f,-20f,300f);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第三个参数toYDelta为动画结束时Y坐标上的移动位置
animation_translate.setRepeatCount(-
1
);
//设置动画执行多少次,如果是-1的话就是一直重复
animation_translate.setDuration(
5000
);
//设置时间持续时间为 5000毫秒
animationSet=
new
AnimationSet(
true
);
animationSet.addAnimation(animation_alpha);
//透明度
animationSet.addAnimation(animation_rotate);
//旋转
animationSet.addAnimation(animation_scale);
//尺寸伸缩
animationSet.addAnimation(animation_translate);
//移动
image.startAnimation(animationSet);
//开始播放
}
|
=====================================================================
常用范例:
1.仿iphone控件抖动
左右抖动R.anim.shake_x代码:
1
2
3
4
5
6
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<translate xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fromXDelta=
"0"
android:toXDelta=
"10"
android:duration=
"1000"
android:interpolator=
"@anim/cycle"
/>
|
上下抖动R.anim.shake_y代码:
1
2
3
4
5
6
7
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<translate xmlns:android=
"http://schemas.android.com/apk/res/android"
android:duration=
"1000"
android:fromYDelta=
"0"
android:interpolator=
"@anim/cycle"
android:toYDelta=
"10"
>
</translate>
|
cycle.xml代码:
1
2
3
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<cycleInterpolator xmlns:android=
"http://schemas.android.com/apk/res/android"
android:cycles=
"20"
/>
|
2.其中一个角边抖动
1
2
3
4
5
6
7
8
9
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<rotate xmlns:android=
"http://schemas.android.com/apk/res/android"
android:duration=
"180"
android:fromDegrees=
"-2"
android:pivotX=
"100%"
android:pivotY=
"100%"
android:repeatCount=
"infinite"
android:repeatMode=
"reverse"
android:toDegrees=
"2"
/>
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1225357,如需转载请自行联系原作者