極客學(xué)院Animation教程講解的很詳細(xì),點(diǎn)擊進(jìn)入哦
這里為學(xué)習(xí)的整理和補(bǔ)充O(∩_∩)O
前言
LayoutAnimation :普通viewGroup 添加統(tǒng)一的進(jìn)入動畫
gridLayoutAnimation: 針對 gridView 添加進(jìn)入動畫
LayoutAnimation 和 gridLayoutAnimation 在 API 1 中就有的函數(shù),因?yàn)槭窃?API 1 中就引入了,所以也只能使用 animtion 來做動畫,而不能使用 animator。
一、LayoutAnimation
1. xml實(shí)現(xiàn)
- 第一步:在res/anim下新建單個item的slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
>
<translate
android:fromXDelta="-30%"
android:toXDelta="0"
/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
/>
</set>
- 第二步:定義viewGroup的layout_animation
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/slide_in_left"
android:animationOrder="normal"
android:delay="1"
/>
LayoutAnimation詳解:
- delay:指每個 Item 的動畫開始延時(shí),取值是 android:animation 所指定動畫時(shí)長的倍數(shù),取值類型可以是 float 類型,也可以是百分?jǐn)?shù),默認(rèn)是 0.5;
在 slide_in_left.xml 中android:duration="1000",即單次動畫的時(shí)長是 1000 毫秒,而我們在這里的指定 android:delay=”1”,即一個 Item 的動畫會在上一個 item 動畫完成后延時(shí)單次動畫時(shí)長的一倍時(shí)間開始,即延時(shí) 1000 毫秒后開始。
- animationOrder:指 viewGroup 中的控件動畫開始順序,取值有 normal(正序)、reverse(倒序)、random(隨機(jī))
animation:指定每個 item 入場所要應(yīng)用的動畫。僅能指定 res/aim 文件夾下的 animation 定義的動畫,不可使用 animator 動畫。
第三步:布局文件中的viewgroup引用
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layout_animation"
/>
- 第四步:java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, getData());
listView.setAdapter(arrayAdapter);
}
public List<String> getData() {
ArrayList<String> strings = new ArrayList<>();
for (int i = 0; i < 20; i++) {
strings.add("測試"+i);
}
return strings;
}
執(zhí)行效果如下:

會發(fā)現(xiàn):
1.第二屏以及之后的數(shù)據(jù)并不會有動畫
2.listview 中各個 item 從左至右滑入位置
3.動畫僅在第一次創(chuàng)建時(shí)有用,后期加入的數(shù)據(jù),將不會再有動畫(這個大家可以自己測一下,這里僅標(biāo)注一下),換句話說,就是
** android:layoutAnimation 只在 viewGroup 創(chuàng)建的時(shí)候,才會對其中的 item 添加動畫。在創(chuàng)建成功以后,再向其中添加 item 將不會再有動畫。**
2. LayoutAnimation 的代碼實(shí)現(xiàn)—LayoutAnimationController
- 構(gòu)造函數(shù)
public LayoutAnimationController(Animation animation)
public LayoutAnimationController(Animation animation, float delay)
- 方法
/**
* 設(shè)置 animation 動畫
*/
public void setAnimation(Animation animation)
/**
* 設(shè)置單個 item 開始動畫延時(shí)
*/
public void setDelay(float delay)
/**
* 設(shè)置 viewGroup 中控件開始動畫順序,取值為 ORDER_NORMAL、ORDER_REVERSE、ORDER_RANDOM
*/
public void setOrder(int order)
- 直接上代碼:(實(shí)現(xiàn)效果和xml一樣的)
Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
LayoutAnimationController layoutAnimationController=new LayoutAnimationController(loadAnimation);
layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
layoutAnimationController.setDelay(1);
listView.setLayoutAnimation(layoutAnimationController);
listView.startLayoutAnimation();
二、GridLayoutAnimation
1. xml實(shí)現(xiàn)
- 建立item的動畫,和LayoutAnimation一樣的slide_in_left.xml
- 建立gridlayout_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:rowDelay="75%"
android:columnDelay="60%"
android:directionPriority="row"
android:direction="top_to_bottom|right_to_left"
android:animation="@android:anim/slide_in_left"
/>
rowDelay:每一行動畫開始的延遲。與 LayoutAnimation 一樣
columnDelay:每一列動畫開始的延遲。取值類型及意義與 rowDelay 相同。
directionPriority:方向優(yōu)先級。取值為 row,collumn,none,意義分別為:行優(yōu)先,列優(yōu)先,和無優(yōu)先級(同時(shí)進(jìn)行)
direction:gridview 動畫方向。
left_to_right:列,從左向右開始動畫
right_to_left :列,從右向左開始動畫
top_to_bottom:行,從上向下開始動畫
bottom_to_top:行,從下向上開始動畫
這四個值之間可以通過“|”連接,從而可以取多個值。很顯然 left_to_right 和 right_to_left 是互斥的,top_to_bottom 和 bottom_to_top 是互斥的。如果不指定 direction 字段,默認(rèn)值為 left_to_right | top_to_bottom;即從上往下,從左往右。
animation: gridview 內(nèi)部元素所使用的動畫。
- xml中引用
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:layoutAnimation="@anim/gridlayout_animation"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
/>
- 最后一步
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, getData());
gridView.setAdapter(arrayAdapter);
效果:

2. 代碼實(shí)現(xiàn)-GridLayoutAnimationController
/**
* 設(shè)置列動畫開始延遲
*/
public void setColumnDelay(float columnDelay)
/**
* 設(shè)置行動畫開始延遲
*/
public void setRowDelay(float rowDelay)
/**
* 設(shè)置 gridview 動畫的入場方向。取值有:DIRECTION_BOTTOM_TO_TOP、DIRECTION_TOP_TO_BOTTOM、DIRECTION_LEFT_TO_RIGHT、DIRECTION_RIGHT_TO_LEFT
*/
public void setDirection(int direction)
/**
* 動畫開始優(yōu)先級,取值有 PRIORITY_COLUMN、PRIORITY_NONE、PRIORITY_ROW
*/
public void setDirectionPriority(int directionPriority)
好啦,就到這里啦在此,祝大家圣誕節(jié)快樂O(∩_∩)O