Android上运用Animation开发简单动画程序

来源:互联网 发布:seo日志分析工具 编辑:程序博客网 时间:2024/05/18 00:12

首先介绍Animation

 一.继承关系:

     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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {// 创建一个AnimationSet对象AnimationSet animSet = new AnimationSet(false);// the rate of change is constantLinearInterpolator 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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);}});}}




0 0
原创粉丝点击