android Animations 动画效果(一)
2014-03-10 14:51
387 查看
Animations提供了一系列的动画效果,这些效果可以应用在绝大多数的控件上
Animations可以分为两大类:
第一类:Tweened Animations
该类Animations提供了旋转,移动,伸展和淡出效果
第二类:Frame-by-Frame Animations
这个类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示
Tweened Animations的分类
1.Alpha:淡入淡出效果
2.Scale:缩放效果
3.Rotate:旋转效果
4.Translate:移动效果
使用Tweened Animations的步骤
1.创建一个AnimationSet对象
2.根据需要创建相应的Animation对象
3.根据软件动画的需要,为Animation对象设置相应的数据
4.将Animation对象添加到AnimationSet对象中
5.使用控件对象开始执行AnimationSet
Tween Animations的通用属性
//设置动画执行的时间为1s
animationSet.setDuration(1000);
//如果为true,执行完动画后,停留到执行结束的时候
animationSet.setFillAfter(true);
//如果为true,执行完动画后,停留到执行开始的时候
animationSet.setFillBefore(false);
//执行前停留的时间(毫秒)
animationSet.setStartOffset(1000);
//执行次数
animationSet.setRepeatCount(2);
下面是例子程序
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="测试scale动画效果"
/>
<Button
android:id="@+id/rotateButton"
android:layout_above="@id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试rotate动画效果"
/>
<Button
android:id="@+id/alphaButton"
android:layout_above="@id/rotateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Alpha动画效果"
/>
<Button
android:id="@+id/translateButton"
android:layout_above="@id/alphaButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Translate动画效果"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="@+id/scaleButton"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50px"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.yx.animations01;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
public class MainActivity extends Activity {
private Button scaleButton=null;
private Button rotateButton=null;
private Button alphaButton=null;
private Button translateButton=null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleButton = (Button) findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new scaleButtonListener());
rotateButton = (Button) findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new rotateButtonListener());
alphaButton = (Button) findViewById(R.id.alphaButton);
alphaButton.setOnClickListener(new alphaButtonListener());
translateButton = (Button) findViewById(R.id.translateButton);
translateButton.setOnClickListener(new translateButtonListener());
imageView = (ImageView) findViewById(R.id.imageViewId);
}
class scaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
/*Parameters
fromX Horizontal scaling factor to apply at the start of the animation
toX Horizontal scaling factor to apply at the end of the animation
fromY Vertical scaling factor to apply at the start of the animation
toY Vertical scaling factor to apply at the end of the animation
pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotXType
is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is
ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f
);
//设置动画执行的时间为1s
scaleAnimation.setDuration(1000);
//将alphaAnimation添加到animationSet中
animationSet.addAnimation(scaleAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
class rotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
/**
* Parameters
fromDegrees Rotation offset to apply at the start of the animation.
toDegrees Rotation offset to apply at the end of the animation.
pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
//1.从哪个角度2.到那个角度3.x轴坐标的类型可取值Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT
//4.其中1f表示一个父控件的横坐标5-6同3-4
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0f
);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
//淡入淡出
class alphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象括号为从什么透明度到什么样的透明度1为不透明,0为透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
//设置动画执行的时间为1s
alphaAnimation.setDuration(1000);
//将alphaAnimation添加到animationSet中
animationSet.addAnimation(alphaAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
class translateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
/*
* Parameters
fromXType Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromXValue Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toXType Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toXValue Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
fromYType Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromYValue Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toYType Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toYValue Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,1.0f
);
//设置动画执行的时间为1s
animationSet.setDuration(1000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setStartOffset(1000);
animationSet.setRepeatCount(2);
//将alphaAnimation添加到animationSet中
animationSet.addAnimation(translateAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
}
Animations可以分为两大类:
第一类:Tweened Animations
该类Animations提供了旋转,移动,伸展和淡出效果
第二类:Frame-by-Frame Animations
这个类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示
Tweened Animations的分类
1.Alpha:淡入淡出效果
2.Scale:缩放效果
3.Rotate:旋转效果
4.Translate:移动效果
使用Tweened Animations的步骤
1.创建一个AnimationSet对象
2.根据需要创建相应的Animation对象
3.根据软件动画的需要,为Animation对象设置相应的数据
4.将Animation对象添加到AnimationSet对象中
5.使用控件对象开始执行AnimationSet
Tween Animations的通用属性
//设置动画执行的时间为1s
animationSet.setDuration(1000);
//如果为true,执行完动画后,停留到执行结束的时候
animationSet.setFillAfter(true);
//如果为true,执行完动画后,停留到执行开始的时候
animationSet.setFillBefore(false);
//执行前停留的时间(毫秒)
animationSet.setStartOffset(1000);
//执行次数
animationSet.setRepeatCount(2);
下面是例子程序
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="测试scale动画效果"
/>
<Button
android:id="@+id/rotateButton"
android:layout_above="@id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试rotate动画效果"
/>
<Button
android:id="@+id/alphaButton"
android:layout_above="@id/rotateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Alpha动画效果"
/>
<Button
android:id="@+id/translateButton"
android:layout_above="@id/alphaButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Translate动画效果"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="@+id/scaleButton"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50px"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.yx.animations01;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
public class MainActivity extends Activity {
private Button scaleButton=null;
private Button rotateButton=null;
private Button alphaButton=null;
private Button translateButton=null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleButton = (Button) findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new scaleButtonListener());
rotateButton = (Button) findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new rotateButtonListener());
alphaButton = (Button) findViewById(R.id.alphaButton);
alphaButton.setOnClickListener(new alphaButtonListener());
translateButton = (Button) findViewById(R.id.translateButton);
translateButton.setOnClickListener(new translateButtonListener());
imageView = (ImageView) findViewById(R.id.imageViewId);
}
class scaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
/*Parameters
fromX Horizontal scaling factor to apply at the start of the animation
toX Horizontal scaling factor to apply at the end of the animation
fromY Vertical scaling factor to apply at the start of the animation
toY Vertical scaling factor to apply at the end of the animation
pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotXType
is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is
ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f
);
//设置动画执行的时间为1s
scaleAnimation.setDuration(1000);
//将alphaAnimation添加到animationSet中
animationSet.addAnimation(scaleAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
class rotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
/**
* Parameters
fromDegrees Rotation offset to apply at the start of the animation.
toDegrees Rotation offset to apply at the end of the animation.
pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
//1.从哪个角度2.到那个角度3.x轴坐标的类型可取值Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT
//4.其中1f表示一个父控件的横坐标5-6同3-4
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0f
);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
//淡入淡出
class alphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象括号为从什么透明度到什么样的透明度1为不透明,0为透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
//设置动画执行的时间为1s
alphaAnimation.setDuration(1000);
//将alphaAnimation添加到animationSet中
animationSet.addAnimation(alphaAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
class translateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
/*
* Parameters
fromXType Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromXValue Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toXType Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toXValue Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
fromYType Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromYValue Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toYType Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toYValue Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,1.0f
);
//设置动画执行的时间为1s
animationSet.setDuration(1000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setStartOffset(1000);
animationSet.setRepeatCount(2);
//将alphaAnimation添加到animationSet中
animationSet.addAnimation(translateAnimation);
//imageView的setAnimation(animationSet)执行动画
imageView.setAnimation(animationSet);
}
}
}
相关文章推荐
- Android ViewGroup实现页面滑动效果并实现不同的动画效果(转载)
- Activity切换动画(overridePendingTransition)-翻页效果
- js实现绿白相间竖向网页百叶窗动画切换效果
- 解决css3动画效果在360兼容模式跟猎豹浏览器下不兼容
- 前端动画效果实现的简单比较
- 出场、入场动画大全,基于NineOldAndroids轻松实现动画效果
- 使用LayoutAnimationController实现ListView动画效果
- 纯css3圆形从中心向四周扩散动画效果
- 【Android UI设计与开发】10:滑动菜单栏(二)SlidingMenu 动画效果的实现
- 加载动画效果(圆形转圈)-1
- Android动画开发——Animation动画效果
- CCS3动画效果插件——Animae.css和WOW.js
- matrix 实现动画3D旋转效果
- flex动画效果
- UIView的,翻转、旋转,偏移,翻页,缩放,取反的动画效果
- <HeadFirst_jQuery> O'REILLY_Chap.5_jQuery 效果和动画
- 基于OpenCV的视频图像组态 (6): 形状动画效果
- JS实现的简单折叠展开动画效果示例
- Android的Activity跳转动画各种效果整理
- 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation