Animation 动画:
定义
- private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;
- private AnimationSet animationSet;
具体实现代码如下:
- 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);//开始播放
- }
原文:http://www.linuxidc.com/Linux/2012-01/51450.htm
1、Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换 ( 平移、缩放、旋转 ) 产生动画效果;第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似。我们先介绍Tween来实现简单的动画;
2、首先看效果图

在项目res文件夹下建一个anim文件夹里建一个tween.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <alpha
- android:duration="5000"
- android:fromAlpha="0"
- android:toAlpha="1.0" />
- <!--
- 透明度控制动画效果 alpha
- 浮点型值:
- fromAlpha 属性为动画起始时透明度
- toAlpha 属性为动画结束时透明度
- 说明:
- 0.0表示完全透明
- 1.0表示完全不透明
- 以上值取0.0-1.0之间的float数据类型的数字
- 长整型值:
- duration 属性为动画持续时间
- 说明:
- 时间以毫秒为单位
- -->
- <rotate
- android:duration="5000"
- android:fromDegrees="0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:toDegrees="1080" />
- <!--
- rotate 旋转动画效果
- 浮点数型值:
- fromDegrees 属性为动画起始时物件的角度
- toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
- 说明:
- 当角度为负数——表示逆时针旋转
- 当角度为正数——表示顺时针旋转
- (负数from——to正数:顺时针旋转)
- (负数from——to负数:逆时针旋转)
- (正数from——to正数:顺时针旋转)
- (正数from——to负数:逆时针旋转)
- pivotX 属性为动画相对于物件的X坐标的开始位置
- pivotY 属性为动画相对于物件的Y坐标的开始位置
- 说明: 以上两个属性值 从0%-100%中取值
- 50%为物件的X或Y方向坐标上的中点位置
- 长整型值:
- duration 属性为动画持续时间
- 说明: 时间以毫秒为单位
- -->
- <scale
- android:duration="5000"
- android:fromXScale="0.1"
- android:fromYScale="0.1"
- android:pivotX="50%"
- android:pivotY="50%"
- android:toXScale="3.0"
- android:toYScale="3.0" />
- <!--
- 尺寸伸缩动画效果 scale
- 浮点型值:
- fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
- toXScale 属性为动画结束时 X坐标上的伸缩尺寸
- fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
- toYScale 属性为动画结束时Y坐标上的伸缩尺寸
- 说明:
- 以上四种属性值
- 0.0表示收缩到没有
- 1.0表示正常无伸缩
- 值小于1.0表示收缩
- 值大于1.0表示放大
- pivotX 属性为动画相对于物件的X坐标的开始位置
- pivotY 属性为动画相对于物件的Y坐标的开始位置
- 说明:
- 以上两个属性值 从0%-100%中取值
- 50%为物件的X或Y方向坐标上的中点位置
- 长整型值:
- duration 属性为动画持续时间
- 说明: 时间以毫秒为单位
- 布尔型值:
- fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
- -->
- <translate
- android:duration="5000"
- android:fromXDelta="-20"
- android:fromYDelta="-20"
- android:toXDelta="300"
- android:toYDelta="300" />
- <!--
- translate 位置转移动画效果
- 整型值:
- fromXDelta 属性为动画起始时 X坐标上的位置
- toXDelta 属性为动画结束时 X坐标上的位置
- fromYDelta 属性为动画起始时 Y坐标上的位置
- toYDelta 属性为动画结束时 Y坐标上的位置
- 注意:
- 没有指定fromXType toXType fromYType toYType 时候,
- 默认是以自己为相对参照物
- 长整型值:
- duration 属性为动画持续时间
- 说明: 时间以毫秒为单位
- -->
- </set>
Android的animation由四种类型组成
| alpha | 渐变透明度动画效果 |
| scale | 渐变尺寸伸缩动画效果 |
| translate | 画面转换位置移动动画效果 |
| rotate | 画面转移旋转动画效果 |
在main.xml文件中定义一个按钮和一个ImageView:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="重播"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:src="@drawable/pic"
- android:id="@+id/image"/>
- </LinearLayout>
最后是java代码,MyAnimation.java:
- package cn.csdn.anim;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MyAnimation extends Activity implements OnClickListener {
- private ImageView image = null;
- private Button button = null;
- private Animation animation;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init() {
- image = (ImageView) this.findViewById(R.id.image);
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(this);
- animation = AnimationUtils.loadAnimation(this, R.anim.tween);// 使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
- image.startAnimation(animation);// 开始动画播出
- }
- @Override
- public void onClick(View v) {
- image.startAnimation(animation);// 开始动画播出
- }
- }
| alpha | 渐变透明度动画效果 |
| scale | 渐变尺寸伸缩动画效果 |
| translate | 画面转换位置移动动画效果 |
| rotate | 画面转移旋转动画效果 |
原文:http://www.linuxidc.com/Linux/2012-01/51449.htm
本文详细介绍如何在Android中使用Tween动画实现透明度变化、旋转、尺寸伸缩及移动等效果,并提供完整的代码实例。
447

被折叠的 条评论
为什么被折叠?



