Android View Animation Example
来源:互联网 发布:java开发实战1200例pdf 编辑:程序博客网 时间:2024/05/06 05:20
Android View Animation 是android的一个基础的动画结构,它可以让我们轻松地制作补间动画(tween animation),只要你设置了你的View的动画的起始时间那么他可以帮助我们通过连续的简单变化或者同时的变化轻松地制作android动画。这个animation类几乎是兼容所有的android上面的View控件如:ImageView,TextView和Button等。但是这个animation类在制作动画的时候只是通过改变控件的位置,大小,角度和透明度来实现的。
android动画旋转的例子:
我们在制作Animation动画的时候既可以通过在XMl里面进行静态的建立也可以通过java代码来动态的实现,Google的建议是通过XML的方式来建立,因为这样的XML文件便于阅读,更改,而且可以重复利用。下面就给出一些例子吧,这里对于每一个动画我都会用两种方式来实现。
下面的例子我将制作一个图片旋转的动画,在这个demo的布局如下:
当我按下上面的按钮的时候布局中间的那个猫的图片就会以图片中心为中心点360的旋转一圈。下面是布局的文件(main.xml):
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03
android:layout_width
=
"fill_parent"
04
android:layout_height
=
"fill_parent"
>
05
<
Button
android:id
=
"@+id/testButton"
android:text
=
"@string/animationText"
06
android:layout_width
=
"wrap_content"
07
android:layout_height
=
"wrap_content"
/>
08
09
<
ImageView
android:id
=
"@+id/testImage"
android:src
=
"@drawable/cat2"
10
android:contentDescription
=
"@string/image_desc"
11
android:layout_centerInParent
=
"true"
12
android:layout_height
=
"wrap_content"
android:layout_width
=
"wrap_content"
android:scaleType
=
"fitCenter"
/>
13
</
RelativeLayout
>
1
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
2
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
3
android:duration
=
"1000"
4
android:valueFrom
=
"0"
5
android:valueTo
=
"360"
6
android:valueType
=
"floatType"
7
android:propertyName
=
"rotation"
8
android:repeatCount
=
"0"
/>
1
ImageView animationTarget = (ImageView)
this
.findViewById(R.id.testImage);
2
Animator animation = AnimatorInflater.loadAnimator(
this
, R.anim.rotate_around_center_point);
3
animation.setTarget(animationTarget);
4
animation.start();
前面已经说了还有一个直接在java代码里面写的方法,如下:
1
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(animationTarget,
"rotation"
, 360f);
2
scaleXAnimator.setRepeatCount(
0
);
3
scaleXAnimator.setDuration(
1000
);
4
scaleXAnimator.start();
android动画放大缩小的例子:
android不仅仅可以帮助我们实现图片的旋转而且它也可以帮助我们实现图片的放大与缩小,类似于上面的方法,我们就试试改变图片大小的动画吧!
1
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
2
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
3
android:duration
=
"1000"
4
android:valueFrom
=
"1"
5
android:valueTo
=
"0.5"
6
android:valueType
=
"floatType"
7
android:propertyName
=
"scaleX"
8
android:repeatCount
=
"1"
9
android:repeatMode
=
"reverse"
/>
1
ImageView animationTarget = (ImageView)
this
.findViewById(R.id.testImage);
2
Animator animation = AnimatorInflater.loadAnimator(
this
, R.anim.scale_animation);
3
animation.setTarget(animationTarget);
4
animation.start();
1
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(animationTarget,
"scaleX"
,
0
.5f);
2
scaleXAnimator.setRepeatMode(ValueAnimator.REVERSE);
3
scaleXAnimator.setRepeatCount(
1
);
4
scaleXAnimator.setDuration(
1000
);
5
scaleXAnimator.start();
运行完上面的例子,我们就会发现其实只是我们的图片的宽度变小了,高度上面没有变化,这是因为我们把一个动画对象应用在了图片上,下面我们来试一下几个动画效果放在同一个控件的例子,这样的话我们就要用到AnimatorSet对象,他可以帮助我们同时应用几个动画效果,下面我们来看一下代码吧!
01
<
set
>
02
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03
android:duration
=
"1000"
04
android:valueFrom
=
"1"
05
android:valueTo
=
"0.5"
06
android:valueType
=
"floatType"
07
android:propertyName
=
"scaleX"
08
android:repeatCount
=
"1"
09
android:repeatMode
=
"reverse"
/>
10
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
11
android:duration
=
"1000"
12
android:valueFrom
=
"1"
13
android:valueTo
=
"0.5"
14
android:valueType
=
"floatType"
15
android:propertyName
=
"scaleY"
16
android:repeatCount
=
"1"
17
android:repeatMode
=
"reverse"
/>
18
</
set
>
1
ImageView zoomTarget = (ImageView) findViewById(R.id.testImage);
2
Animator zoomAnimation = (AnimatorSet)AnimatorInflater.loadAnimator(
this
, R.anim.zoom_in_out_animation);
3
zoomAnimation.setTarget(zoomTarget);
4
zoomAnimation.start();
01
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(zoomTarget,
"scaleX"
,
0
.5f);
02
scaleXAnimator.setRepeatMode(ValueAnimator.REVERSE);
03
scaleXAnimator.setRepeatCount(
1
);
04
scaleXAnimator.setDuration(
1000
);
05
06
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(zoomTarget,
"scaleY"
,
0
.5f);
07
scaleYAnimator.setRepeatMode(ValueAnimator.REVERSE);
08
scaleYAnimator.setRepeatCount(
1
);
09
scaleYAnimator.setDuration(
1000
);
10
11
AnimatorSet set =
new
AnimatorSet();
12
set.playTogether(scaleXAnimator, scaleYAnimator);
13
set.start();
Android的大小变化与旋转的例子:
在上面的例子中我们已经实现了旋转的动画与大小变化的动画,现在就让我们来把他们整合在一起吧!
XMl的代码:
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
set
>
03
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
04
android:duration
=
"1000"
05
android:valueFrom
=
"1"
06
android:valueTo
=
"0.5"
07
android:valueType
=
"floatType"
08
android:propertyName
=
"scaleX"
09
android:repeatCount
=
"1"
10
android:repeatMode
=
"reverse"
/>
11
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
12
android:duration
=
"1000"
13
android:valueFrom
=
"1"
14
android:valueTo
=
"0.5"
15
android:valueType
=
"floatType"
16
android:propertyName
=
"scaleY"
17
android:repeatCount
=
"1"
18
android:repeatMode
=
"reverse"
/>
19
<
objectAnimator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
20
android:duration
=
"1000"
21
android:valueFrom
=
"0"
22
android:valueTo
=
"360"
23
android:valueType
=
"floatType"
24
android:propertyName
=
"rotation"
25
android:repeatCount
=
"1"
26
android:repeatMode
=
"restart"
/>
27
</
set
>
1
ImageView zoomTarget = (ImageView) findViewById(R.id.testImage);
2
Animator zoomAnimation = (AnimatorSet)AnimatorInflater.loadAnimator(
this
, R.anim.zoom_rotation_animation);
3
zoomAnimation.setTarget(zoomTarget);
4
zoomAnimation.start();
01
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(zoomTarget,
"scaleX"
,
0
.5f);
02
scaleXAnimator.setRepeatMode(ValueAnimator.REVERSE);
03
scaleXAnimator.setRepeatCount(
1
);
04
scaleXAnimator.setDuration(
1000
);
05
06
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(zoomTarget,
"scaleY"
,
0
.5f);
07
scaleYAnimator.setRepeatMode(ValueAnimator.REVERSE);
08
scaleYAnimator.setRepeatCount(
1
);
09
scaleYAnimator.setDuration(
1000
);
10
11
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(zoomTarget,
"rotation"
, 0f, 360f);
12
rotationAnimator.setRepeatMode(ValueAnimator.RESTART);
13
rotationAnimator.setRepeatCount(
1
);
14
rotationAnimator.setDuration(
1000
);
15
16
AnimatorSet set =
new
AnimatorSet();
17
set.playTogether(scaleXAnimator, scaleYAnimator, rotationAnimator);
18
set.start();
上面我们已经实现了各种效果同时变化的效果了,可是有时候我们需要先执行一个动画然后执行另外的一个,那么这个怎么呢?下面就给出一个例子,它实现了先对图片进行大小变化的动画然后再让这个图片旋转一圈的效果:
01
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(zoomTarget,
"scaleX"
,
0
.5f);
02
scaleXAnimator.setRepeatMode(ValueAnimator.REVERSE);
03
scaleXAnimator.setRepeatCount(
1
);
04
scaleXAnimator.setDuration(
1000
);
05
06
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(zoomTarget,
"scaleY"
,
0
.5f);
07
scaleYAnimator.setRepeatMode(ValueAnimator.REVERSE);
08
scaleYAnimator.setRepeatCount(
1
);
09
scaleYAnimator.setDuration(
1000
);
10
11
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(zoomTarget,
"rotation"
, 0f, 360f);
12
rotationAnimator.setRepeatMode(ValueAnimator.RESTART);
13
rotationAnimator.setRepeatCount(
1
);
14
rotationAnimator.setDuration(
1000
);
15
16
//sequencial animation
17
AnimatorSet set =
new
AnimatorSet();
18
set.play(scaleXAnimator).with(scaleYAnimator);
19
set.play(scaleXAnimator).before(rotationAnimator);
20
set.start();
- Android View Animation Example
- Android-animation-View Animation
- Android Animation Example (tween animations)
- Android View Animation
- android.view.animation译
- Android View Animation
- android学习View Animation
- android动画 -- view Animation
- Android:View Animation
- android-view animation
- Android View Animation
- Android:View Animation 动画
- 【Android基础知识】Drawable Animation和View Animation
- android View Animation之Tween animation
- Android Animation——view animation
- Android视图动画---View Animation
- Android视图动画(View Animation)
- Android动画之View Animation
- java同步代码块和同步函数
- 使用jQuery获取部署文件的绝对路径
- 我的博客处女秀
- c++primer第五版课后练习答案(第三章)
- 暑期实习简要总结
- Android View Animation Example
- C++构造函数&继承方式
- iframe动态改变内嵌页面高度
- Java设计模式-适配器模式
- 如果回到十八岁,我要对自己说......
- MySQL会出现中文乱码的原因不外乎下列几点
- C#-记录打开过的文件---ShinePans
- Spark快速入门指南
- ZOJ 3829 Known Notation (2014牡丹江H题)