特別提醒:本文參考摘自【Carson_Ho:http://www.lxweimin.com/p/733532041f46 強(qiáng)烈建議讀者進(jìn)入原博客查看學(xué)習(xí)。】
1、Activity之間切換的動(dòng)畫
特別提示:Activity之間的切換動(dòng)畫有很多種方式,這里僅僅摘抄上文中提到的博客(http://www.lxweimin.com/p/733532041f46 )中的方法,想要學(xué)習(xí)更多方法可參考這篇博客(http://blog.csdn.net/qq_23547831/article/details/51821159 ),里面提到了五種方式來實(shí)現(xiàn)
Activity切換動(dòng)畫即 Activity 啟動(dòng) / 退出時(shí)的動(dòng)畫效果。
1.1 設(shè)置動(dòng)畫的方式
a、 啟動(dòng)時(shí)的動(dòng)畫
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);
//設(shè)置啟動(dòng)動(dòng)畫,采用overridePendingTransition
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
//參數(shù)說明:
//enterAnim:從Activity a跳轉(zhuǎn)到Activity b,進(jìn)入b時(shí)的動(dòng)畫效果資源ID
//exitAnim:從Activity a跳轉(zhuǎn)到Activity b,離開a時(shí)的動(dòng)畫效果資源ID
// 特別注意
// overridePendingTransition()必須要在startActivity(intent)后被調(diào)用才能生效
b、退出時(shí)的動(dòng)畫
@Override
public void finish() {
super.finish();
// 采用overridePendingTransition(int enterAnim, int exitAnim)進(jìn)行設(shè)置
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
//參數(shù)說明:
// enterAnim:從Activity a跳轉(zhuǎn)到Activity b,進(jìn)入b時(shí)的動(dòng)畫效果資源ID
// exitAnim:從Activity a跳轉(zhuǎn)到Activity b,離開a時(shí)的動(dòng)畫效果資源Id
// 特別注意
// overridePendingTransition()必須要在finish()后被調(diào)用才能生效
}
1.2 使用系統(tǒng)自帶的動(dòng)畫
- 對(duì)于參數(shù) enterAnim & exitAnim 的資源ID,系統(tǒng)有自帶的效果android.R.anim.xxx,如下設(shè)置:
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);
//系統(tǒng)自帶動(dòng)畫--淺入淺出效果
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
//系統(tǒng)自帶動(dòng)畫--從左向右滑動(dòng)的效果
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
//等等....
-
效果圖大致如下:
淺入淺出.gif
從左向右滑.gif
1.3 自定義切換效果
- 除了使用系統(tǒng)自帶的切換效果,還可以自定義Activity的切換效果:
此處就用到補(bǔ)間動(dòng)畫了
1.3.1 自定義實(shí)例 --淡入淡出 效果
- 淡入淡出 效果是采用透明度動(dòng)畫(Alpha)。
fade_in.xml (淡入)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
fade_out.xml (淡出)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
在Java代碼中設(shè)置
Intent intent = new Intent(MainActivity.this, SecActivity.class);
startActivity(intent);
// 自定義的淡入淡出動(dòng)畫效果
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
1.3.2 自定義實(shí)例 --左右滑動(dòng) 效果
左右滑動(dòng) 效果是采用平移動(dòng)畫(Translate)
-
先了解 Activity 的位置信息,如下圖
944365-c5bc4784f2ba9764.png 從上圖可以看出:
- 以屏幕底邊為X軸,屏幕左邊為Y軸;
當(dāng)Activity在X軸 = -100%p時(shí),剛好完全超出屏幕到左邊(位置1)
當(dāng)Activity在X軸 = 0%p時(shí),剛好完全在屏幕內(nèi)(位置2)
當(dāng)Activity在X軸 = 100%p時(shí),剛好完全超出屏幕到右邊(位置3)
定義一個(gè)動(dòng)畫效果:從右滑到左
out_to_left.xml
從中間滑到左邊,即從位置2 - 位置1
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="500"
android:fromXDelta="0%p"
android:toXDelta="-100%p"
/>
</set>
in_from_right.xml
從右邊滑到中間,即從位置3 - 位置2
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0%p"
/>
</set>
在Java代碼中設(shè)置效果
Intent intent = new Intent(MainActivity.this, SecActivity.class);
startActivity(intent)
// 自定義 從右向左滑動(dòng)的效果
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
1.4 關(guān)于其他切換效果
關(guān)于 縮放和旋轉(zhuǎn)動(dòng)畫 作為Activity的動(dòng)畫效果也是類似的
通過 想象力 能組合 上述4種基本動(dòng)畫 進(jìn)行動(dòng)畫效果展示
即這種切換效果還能使用補(bǔ)間動(dòng)畫的組合動(dòng)畫
- 此處僅列出較為簡單的切換效果,如想實(shí)現(xiàn)更多酷炫的切換動(dòng)畫,http://www.lxweimin.com/p/37e94f8b6f59)
2、Fragment動(dòng)畫切換效果
2.1 系統(tǒng)自帶的動(dòng)畫切換效果
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setTransition(int transit);
// 通過setTransition(int transit)進(jìn)行設(shè)置
// transit參數(shù)說明
// 1. FragmentTransaction.TRANSIT_NONE:無動(dòng)畫
// 2. FragmentTransaction.TRANSIT_FRAGMENT_OPEN:標(biāo)準(zhǔn)的打開動(dòng)畫效果
// 3. FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:標(biāo)準(zhǔn)的關(guān)閉動(dòng)畫效果
// 標(biāo)準(zhǔn)動(dòng)畫設(shè)置好后,在Fragment添加和移除的時(shí)候都會(huì)有。
2.2 自定義動(dòng)畫切換效果
// 采用`FragmentTransavtion`的 `setCustomAnimations()`進(jìn)行設(shè)置
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(
R.anim.in_from_right,
R.anim.out_to_left);
// 此處的自定義動(dòng)畫效果同Activity,此處不再過多描述
3、視圖組(ViewGroup)中子元素的出場效果
視圖組(ViewGroup)中子元素可以具備出場時(shí)的補(bǔ)間動(dòng)畫效果
常用需求場景:為ListView的 item 設(shè)置出場動(dòng)畫
使用步驟:
步驟1:設(shè)置子元素的出場動(dòng)畫
res/anim/view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
// 此處采用了組合動(dòng)畫
<set xmlns:android="http://schemas.android.com/apk/res/android" >
android:duration="3000"
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
<translate
android:fromXDelta="500"
android:toXDelta="0"
/>
</set>
步驟2:設(shè)置 視圖組(ViewGroup)的動(dòng)畫文件
res/ anim /anim_layout.xml
<?xml version="1.0" encoding="utf-8"?>
// 采用LayoutAnimation標(biāo)簽
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
// 子元素開始動(dòng)畫的時(shí)間延遲
// 如子元素入場動(dòng)畫的時(shí)間總長設(shè)置為300ms
// 那么 delay = "0.5" 表示每個(gè)子元素都會(huì)延遲150ms才會(huì)播放動(dòng)畫效果
// 第一個(gè)子元素延遲150ms播放入場效果;第二個(gè)延遲300ms,以此類推
android:animationOrder="normal"
// 表示子元素動(dòng)畫的順序
// 可設(shè)置屬性為:
// 1. normal :順序顯示,即排在前面的子元素先播放入場動(dòng)畫
// 2. reverse:倒序顯示,即排在后面的子元素先播放入場動(dòng)畫
// 3. random:隨機(jī)播放入場動(dòng)畫
android:animation="@anim/view_animation"
// 設(shè)置入場的具體動(dòng)畫效果
// 將步驟1的子元素出場動(dòng)畫設(shè)置到這里
/>
步驟3:為視圖組(ViewGroup)指定andorid:layoutAnimation屬性
- 指定的方式有兩種: XML / Java代碼設(shè)置
方式1:在 XML 中指定
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FFFFFF"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layoutAnimation="@anim/anim_layout"
// 指定layoutAnimation屬性用以指定子元素的入場動(dòng)畫
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
方式2:在Java代碼中指定
這樣就不用額外設(shè)置res/ anim /anim_layout.xml該xml文件了
ListView lv = (ListView) findViewById(R.id.listView1);
// 加載子元素的出場動(dòng)畫
Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_item);
// 設(shè)置LayoutAnimation的屬性
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
// 為ListView設(shè)置LayoutAnimation的屬性
lv.setLayoutAnimation(controller);
效果大致如下:
4、關(guān)于ListView滑動(dòng)時(shí)的動(dòng)畫效果
- 這里推薦兩篇博客,里面有具體實(shí)現(xiàn)ListView滑動(dòng)時(shí)條目的動(dòng)畫效果思想
Android Animation實(shí)戰(zhàn)之一個(gè)APP的ListView的動(dòng)畫效果