Android常用动画Animation的使用

来源:互联网 发布:sql语句count用法 编辑:程序博客网 时间:2024/05/16 02:14

Andriod中有几种常用的Animation

AlphaAnimation  淡入淡出效果

RotateAnimation 旋转效果

ScaleAnimation 缩放动画

TranslaAnimation 移动动画

这几种动画可以通过xml实现也可以通过java代码实现,先看下在代码中是怎样实现了

在布局文件(animation.xml)中声明几个效果的按钮和图片

<?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" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:id="@+id/rotate"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="rotate演示" />    <Button        android:id="@+id/scale"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="scale演示" />    <Button        android:id="@+id/translate"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="translate演示" />    <Button        android:id="@+id/alpha"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="alpha演示" />    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="50dip"        android:src="@drawable/ic_launcher" /></LinearLayout>


在Activity中

package com.example.animation;import com.example.widgetdemo.R;import android.app.Activity;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.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;public class AnimationDemo extends Activity {private Button rotate = null;private Button scale = null;private Button translate = null;private Button alpha = null;private ImageView image = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.animation);rotate = (Button) findViewById(R.id.rotate);scale = (Button) findViewById(R.id.scale);translate = (Button) findViewById(R.id.translate);alpha = (Button) findViewById(R.id.alpha);image = (ImageView) findViewById(R.id.image);rotate.setOnClickListener(new rotateListener());scale.setOnClickListener(new scaleListener());translate.setOnClickListener(new translateListener());alpha.setOnClickListener(new alphaListener());}/** * 旋转动画 * @author Administrator * */class rotateListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimationSet animationSet = new AnimationSet(true);RotateAnimation totateAnimation = new RotateAnimation(0, //旋转开始角度360,   //旋转结束角度Animation.RELATIVE_TO_SELF,   //X轴的旋转类型有三种选择Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT2f,    //X轴的旋转值Animation.RELATIVE_TO_SELF, 0f);//动画持续时间totateAnimation.setDuration(2000);   //添加动画效果animationSet.addAnimation(totateAnimation);//为图片添加动画image.startAnimation(animationSet);}}/** * 缩放动画 * @author Administrator * */class scaleListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimationSet animationSet = new AnimationSet(true);ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,0.1f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(2000);animationSet.addAnimation(scaleAnimation);image.startAnimation(animationSet);}}/** * 移动 * @author Administrator * */class translateListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimationSet animationSet = new AnimationSet(true);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);translateAnimation.setDuration(2000);animationSet.addAnimation(translateAnimation);image.startAnimation(animationSet);}}/** * 渐变动画 淡入淡出 * @author Administrator * */class alphaListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimationSet animationSet = new AnimationSet(true);AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);alphaAnimation.setDuration(2000);animationSet.setStartOffset(1000); // 1s后开始animationSet.addAnimation(alphaAnimation);image.startAnimation(animationSet);}}}

难点主要是每个动画的构造函数如何设置参数。

第二种实现方法

下面再来看下如何在xml中设置动画

首先在res目录下创建anim目录动画效果的配置文件

alpha.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator" >    <alpha        android:duration="3000"        android:fromAlpha="1.0"        android:toAlpha="0.0"        android:startOffset="500" /></set>

rotate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator" >    <rotate        android:duration="3000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="+360" /></set>

scale.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator" >    <scale        android:duration="3000"        android:fromXScale="1.0"        android:toXScale="0.0"        android:fromYScale="1.0"        android:toYScale="0.0"        android:pivotX="50%"        android:pivotY="50%"/></set>

translate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator" >    <translate        android:duration="3000"        android:fromXDelta="50%"        android:toXDelta="100%"        android:fromYDelta="0%"        android:toYDelta="100%" /></set>

再来看下在Activity中如何实现

package com.example.animation;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;import com.example.widgetdemo.R;public class AnimationXmlDemo extends Activity {private Button rotate = null;private Button scale = null;private Button translate = null;private Button alpha = null;private ImageView image = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.animation_xml);rotate = (Button) findViewById(R.id.rotate);scale = (Button) findViewById(R.id.scale);translate = (Button) findViewById(R.id.translate);alpha = (Button) findViewById(R.id.alpha);image = (ImageView) findViewById(R.id.image);rotate.setOnClickListener(new rotateListener());scale.setOnClickListener(new scaleListener());translate.setOnClickListener(new translateListener());alpha.setOnClickListener(new alphaListener());}/** * 旋转动画 * @author Administrator * */class rotateListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.rotate);image.startAnimation(animation);}}/** * 缩放动画 * @author Administrator * */class scaleListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.scale);image.startAnimation(animation);}}/** * 移动 * @author Administrator * */class translateListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.translate);image.startAnimation(animation);}}/** * 渐变动画 * @author Administrator * */class alphaListener implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.alpha);image.startAnimation(animation);}}}
在代码中和xml中实现的效果都是一样的,最后上图



完整的代码可以到以下链接下载

点击打开链接

原创粉丝点击