转载

Android动画Animation简单示例

Animation是Android给我们提供的一个可以实现动画效果的API,利用Animation我们可以实现一系列的动画效果,比如缩放动画,透明度动画,旋转动画,位移动画,布局动画,帧动画等等.Android基础动画分为四种,分别是Tween Animation,Frame Animation,Layout Animation,Property Animation.由于动画效果不好用图片进行展示,大家可以自己亲手试试看下效果.下面给出前三种简单的示例代码,帮助大家进行简单的动画入门.

(1) Tween Animation (变换动画)

Tween Animation按效果可以分为四种:Alpha,Scale,Translate,Rotate,它们有一下共同属性:

1. Duration: 动画持续时间(毫秒);

2. fillAfter: 设置为True,动画转换在动画结束后被应用;

3. fillBefore: 设置为True,动画转换在动画开始前被应用;

4. interpolator: 动画插入器(加速,减速插入器);

5. repeatCount: 动画重复次数;

6. repeatMode: 正序重复/倒序重复

7. startOffSet: 动画之间的时间间隔

① Alpha (渐变透明度动画)

MainActivity.java主要代码:

ImageView image = (ImageView) findViewById(id.image); Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha); image.startAnimation(loadAnimation);

alpha.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >  <alpha   android:duration="1000"   android:fromAlpha="0.1"   android:toAlpha="1.0" >  </alpha> </set> 

② Scale (渐变尺寸缩放动画 )

MainActivity.java主要代码:

ImageView image = (ImageView) findViewById(id.image); Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale); image.startAnimation(loadAnimation);

scale.xml

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

③ Translate (位置移动动画)

MainActivity.java主要代码:

ImageView image = (ImageView) findViewById(id.image); Animation loadAnimationloadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate); image.startAnimation(loadAnimation);

translate.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >  <translate   android:duration="1000"   android:fromXDelta="10"   android:fromYDelta="10"   android:toXDelta="100"   android:toYDelta="100" /> </set> 

④ Rotate (旋转动画)

MainActivity.java主要代码:

ImageView image = (ImageView) findViewById(id.image); Animation loadAnimation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate); image.startAnimation(loadAnimation);

rotate.xml

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

(2) Layout Animation (布局动画)

MainActivity.java主要代码:

Intent intent=new Intent(MainActivity.this,ListActivity.class); startActivity(intent);

ListActivity.java代码:

package com.example.caobotao.learnanimation; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.animation.AnimationUtils; import android.view.animation.LayoutAnimationController; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListActivity extends Activity{  private ListView listView;  @Override  protected void onCreate(Bundle savedInstanceState) {   // TODO Auto-generated method stub   super.onCreate(savedInstanceState);   setContentView(R.layout.list_layout);   listView=(ListView) findViewById(R.id.listView);   List<String>list=new ArrayList<String>();   for(int i=0;i<20;i++)   {    list.add("示例"+i);   }   ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);   listView.setAdapter(adapter);   LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));   lac.setOrder(LayoutAnimationController.ORDER_NORMAL);   listView.setLayoutAnimation(lac);   listView.startLayoutAnimation();  } } 

zoom_in.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@android:anim/decelerate_interpolator" >   <scale    android:duration="1000"    android:fromXScale="0.1"    android:fromYScale="0.1"    android:pivotX="50%"    android:pivotY="50%"    android:toXScale="1.0"    android:toYScale="1.0" />   <alpha    android:duration="1000"    android:fromAlpha="0"    android:toAlpha="1.0" /> </set> 

(3) Frame Animation (帧动画)

MainActivity.java主要代码:

ImageView image = (ImageView) findViewById(id.image); image.setImageResource(R.drawable.anim_list); AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable(); animationDrawable.start();

anim_list.xml

<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >  <item   android:drawable="@mipmap/one"   android:duration="500"/>  <item   android:drawable="@mipmap/two"   android:duration="500"/>  <item   android:drawable="@mipmap/three"   android:duration="500"/>  <item   android:drawable="@mipmap/four"   android:duration="500"/>  <item   android:drawable="@mipmap/five"   android:duration="500"/>  <item   android:drawable="@mipmap/six"   android:duration="500"/> </animation-list> 
正文到此结束
Loading...