一、LayoutAnimation布局动画
用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果
在res-anim文件下新建一个动画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=""
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=""
android:fromAlpha=""
android:toAlpha="1.0" />
</set>
zoom_in
list为列表
//布局动画控制器
LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.zoom_in));
//动画方式
lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
//加载布局动画
list.setLayoutAnimation(lac);
//开始动画
list.startLayoutAnimation();
//为列表视图中选中的项添加响应事件
效果图:
全部代码:
package other; import com.example.allcode.R; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast; public class Donghua_List extends Activity{
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.donghua_layout); list = (ListView) findViewById(R.id.listView1);
//布局动画控制器
LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.zoom_in));
//动画方式
lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
//加载布局动画
list.setLayoutAnimation(lac);
//开始动画
list.startLayoutAnimation();
//为列表视图中选中的项添加响应事件
list.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
// TODO Auto-generated method stub
String result = parent.getItemAtPosition(pos).toString(); //获取选择项的值
Toast.makeText(Donghua_List.this, "点击了 "+result, Toast.LENGTH_SHORT).show();//输出选中项消息
}
});
} }
Donghua_list.java
<?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" > <ListView
android:id="@+id/listView1"
android:entries="@array/list" android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>
donghua_layout.xml
------------------------------------------------------------------------------------------------------------------
二、Drawable动画(帧动画)
即多张连续的动画顺序播放
核心类:AnimationDrawable
使用步骤:
1、在res下创建drawable文件夹创建资源文件 xx.xml (名字随便取)
有个属性 android:oneshot="false" 判断是否重复播放帧动画,默认false 表示循环播放,true表示只播放一次
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:drawable="@drawable/refresh_001" android:duration="" />
<item android:drawable="@drawable/refresh_002" android:duration="" />
<item android:drawable="@drawable/refresh_003" android:duration="" />
<item android:drawable="@drawable/refresh_004" android:duration="" />
<item android:drawable="@drawable/refresh_005" android:duration="" />
<item android:drawable="@drawable/refresh_006" android:duration="" />
<item android:drawable="@drawable/refresh_007" android:duration="" />
<item android:drawable="@drawable/refresh_008" android:duration="" />
<item android:drawable="@drawable/refresh_009" android:duration="" />
<item android:drawable="@drawable/refresh_010" android:duration="" />
<item android:drawable="@drawable/refresh_011" android:duration="" />
<item android:drawable="@drawable/refresh_012" android:duration="" />
<item android:drawable="@drawable/refresh_013" android:duration="" />
<item android:drawable="@drawable/refresh_014" android:duration="" />
<item android:drawable="@drawable/refresh_015" android:duration="" />
<item android:drawable="@drawable/refresh_016" android:duration="" />
<item android:drawable="@drawable/refresh_017" android:duration="" />
<item android:drawable="@drawable/refresh_018" android:duration="" />
<item android:drawable="@drawable/refresh_019" android:duration="" />
<item android:drawable="@drawable/refresh_020" android:duration="" />
<item android:drawable="@drawable/refresh_021" android:duration="" />
<item android:drawable="@drawable/refresh_022" android:duration="" />
<item android:drawable="@drawable/refresh_023" android:duration="" />
<item android:drawable="@drawable/refresh_024" android:duration="" />
<item android:drawable="@drawable/refresh_025" android:duration="" />
<item android:drawable="@drawable/refresh_026" android:duration="" />
<item android:drawable="@drawable/refresh_027" android:duration="" />
<item android:drawable="@drawable/refresh_028" android:duration="" />
<item android:drawable="@drawable/refresh_029" android:duration="" />
<item android:drawable="@drawable/refresh_030" android:duration="" />
<item android:drawable="@drawable/refresh_031" android:duration="" />
<item android:drawable="@drawable/refresh_032" android:duration="" />
<item android:drawable="@drawable/refresh_033" android:duration="" />
<item android:drawable="@drawable/refresh_034" android:duration="" />
<item android:drawable="@drawable/refresh_035" android:duration="" />
<item android:drawable="@drawable/refresh_036" android:duration="" />
<item android:drawable="@drawable/refresh_037" android:duration="" />
<item android:drawable="@drawable/refresh_038" android:duration="" />
<item android:drawable="@drawable/refresh_039" android:duration="" />
<item android:drawable="@drawable/refresh_040" android:duration="" />
<item android:drawable="@drawable/refresh_041" android:duration="" />
<item android:drawable="@drawable/refresh_042" android:duration="" />
<item android:drawable="@drawable/refresh_043" android:duration="" />
<item android:drawable="@drawable/refresh_044" android:duration="" />
<item android:drawable="@drawable/refresh_045" android:duration="" />
<item android:drawable="@drawable/refresh_046" android:duration="" />
<item android:drawable="@drawable/refresh_047" android:duration="" />
<item android:drawable="@drawable/refresh_048" android:duration="" /> </animation-list>
2、布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/refresh_001" /> </RelativeLayout>
activity_main.xml
3、使用AnimationDrawable 类
(1)找到要显示动画的ImageView控件
imageView = (ImageView) findViewById(R.id.imageView);
(2)给图片控件设置资源
imageView.setBackgroundResource(R.drawable.waiting_anim); //将帧动画资源文件作为View控件背景
(3)获取AnimationDrawable 类对象
AnimationDrawable rocketAnimation = (AnimationDrawable) imageView.getBackground(); //获取背景并强转成为帧动画对象
(4)启动动画
rocketAnimation.start();
效果图:
-------------------------------------------------------------------------------------------------------------------
动画汇总:
<?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=""
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=""
android:fromAlpha=""
android:toAlpha="1.0" />
</set>
zoom_in.xml 从右往左
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p"
android:toXDelta=""
android:duration=""/>
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="" />
</set>
push_left_in.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:fromYDelta="-100%" android:toXDelta="" android:duration="" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="" />
</set>
slide_top_to_bottom.xml 从上往下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"> <translate android:fromYDelta="100%" android:toXDelta="" android:duration="" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="" />
</set>
slide_bottom_to_top.xml从下往上