Android上运用Animation开发简单动画程序
2014-10-20 18:49
337 查看
首先介绍Animation
一.继承关系:
android.view.animation.Animation
二.四个常用的类及一个辅助类
AlphaAnimation:通过改变图像的Alpha值来实现渐变(淡入淡出)
RotateAnimation:旋转动画
ScaleAnimation:缩放动画
TranslateAnimation:平移动画
AnimationSet:使用方法见下面代码
三.示例代码
首先给出该程序的布局文件:
效果图:
java文件
一.继承关系:
android.view.animation.Animation
二.四个常用的类及一个辅助类
AlphaAnimation:通过改变图像的Alpha值来实现渐变(淡入淡出)
RotateAnimation:旋转动画
ScaleAnimation:缩放动画
TranslateAnimation:平移动画
AnimationSet:使用方法见下面代码
三.示例代码
首先给出该程序的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/game_disc" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/rotatebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" /> <Button android:id="@+id/scalebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" /> <Button android:id="@+id/alphabtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="淡入淡出" /> <Button android:id="@+id/translatebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移动" /> </LinearLayout> </LinearLayout>
效果图:
java文件
package com.example.animation; import android.app.Activity; import android.media.Image; 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.LinearInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; /* * 使用TweenedAnimations的步骤: 1.创建一个AnimationSet对象(Animation子类); 2.增加需要创建相应的Animation对象; 3.根据项目的需求,为Animation对象设置相应的数据; 4.将Animatin对象添加到AnimationSet对象当中; 5.使用控件对象开始执行AnimationSet。 */ /* * Interpolator定义了动画变化的速率或规律 */ public class MainActivity extends Activity { // 旋转按钮 private Button mRotateBtn = null; // 缩放按钮 private Button mScaleBtn = null; // 淡入淡出按钮 private Button mAlphaBtn = null; // 移动按钮 private Button mTranslateBtn = null; // 目标图片 private ImageView mImage = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animation); // 初始化按钮 mRotateBtn = (Button) findViewById(R.id.rotatebtn); mScaleBtn = (Button) findViewById(R.id.scalebtn); mAlphaBtn = (Button) findViewById(R.id.alphabtn); mTranslateBtn = (Button) findViewById(R.id.translatebtn); // 初始化图片 mImage = (ImageView) findViewById(R.id.image); // 旋转按钮监听器 mRotateBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 创建一个AnimationSet对象 AnimationSet animSet = new AnimationSet(false); // the rate of change is constant LinearInterpolator interpolator = new LinearInterpolator(); // 创建一个旋转动画对象 RotateAnimation rotateAnim = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 设置属性 rotateAnim.setInterpolator(interpolator); rotateAnim.setDuration(2400); rotateAnim.setRepeatCount(-1); // 将旋转动画加入到Set里 animSet.addAnimation(rotateAnim); // 启动该动画 mImage.startAnimation(animSet); } }); // 缩放按钮监听器 mScaleBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); // 参数1:x轴的初始值 // 参数2:x轴收缩后的值 // 参数3:y轴的初始值 // 参数4:y轴收缩后的值 // 参数5:确定x轴坐标的类型 // 参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴 // 参数7:确定y轴坐标的类型 // 参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴 ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(2000); animationSet.addAnimation(scaleAnimation); mImage.startAnimation(animationSet); } }); // 淡入淡出按钮监听器 mAlphaBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AnimationSet animSet = new AnimationSet(true); AlphaAnimation alphaAnim = new AlphaAnimation(0, 1); alphaAnim.setDuration(2000); animSet.addAnimation(alphaAnim); mImage.startAnimation(animSet); } }); // 移动按钮监听器 mTranslateBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); // 参数1~2:x坐标的开始位置 // 参数3~4:x坐标的结束位置 // 参数5~6:y坐标的开始位置 // 参数7~8:y坐标的结束位置 TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f); translateAnimation.setDuration(1000); animationSet.addAnimation(translateAnimation); mImage.startAnimation(animationSet); } }); } }
相关文章推荐
- Android动画开发——Animation动画效果
- Android动画开发——Animation动画效果
- android+unity3D游戏开发joint的简单运用
- Android动画开发——Animation动画效果
- Android动画效果Animation之Tween实现简单动画
- Android 开发笔记 动画效果 --Animation 动画专题研究 一 (转帖)
- Android UI开发第十二篇——动画效果Animation
- Android动画开发——Animation动画效果
- Android动画开发——Animation动画效果
- Android动画开发——Animation动画效果
- Android动画开发——Animation显示隐藏菜单效果
- Android UI开发第十二篇——动画效果Animation(一)
- Android UI开发第十二篇——动画效果Animation(一)
- [转]Android动画开发——Animation动画效果
- 【Android2D游戏开发十四】(未完待续)手把手教你在SurfaceView中照样使用Android 动画—Tween Animation!
- android开发(9) 渐变动画演示(Tween Animation)
- Android游戏开发学习笔记(一):tweened animation自定义动画的实现
- Android动画开发——Animation动画效果
- Android动画开发——Animation动画效果
- Android 开发笔记 动画效果 --Animation 动画专题研究(xml)