android 电视关闭动画,Activity 展开和关闭动画
平常從一個 Activity A打開另個一個Activity B ,一般 Activity B 都是從屏幕右邊出來。而關閉 Activity B 返回Activity A ,則 Activity B 會從左邊沿向右邊沿消失。有時候,想讓新的 Activity 從屏幕的底部彈出,或者屏幕的中間向彈出,或者想讓Activity從屏幕的中間收回。本文章講的是這中需要效果。
實現原理
主要是 Activity 類的 overridePendingTransition(int enterAnim, int exitAnim) 方法的調用,在 res/anim目錄下創建所需的動畫文件,然后在 overridePendingTransition(int enterAnim, int exitAnim) 方法中調用動畫即可。
效果圖
show_activity_anim
具體代碼
在 res/anim 目錄下新建你需要的動畫文件,例如,我需要新的 Activity 從頂部向上彈出,然后從頂部向底部收回。那么我創建了activity_open_from_bottom_to_top.xml 文件,activity_close_from_top_to_bottom.xml 文件和 activity_keep_status.xml 文件。如下
activity_open_from_bottom_to_top.xml
android:duration="1500"
android:fromYDelta="100%p"
android:toYDelta="0"/>
android:fromAlpha="0.2"
android:toAlpha="1.0"
android:duration ="1500"/>
activity_close_from_top_to_bottom.xml
android:duration="1500"
android:fromYDelta="25"
android:toYDelta="100%p"/>
android:fromAlpha="1.0"
android:toAlpha="0.2"
android:duration ="1500"/>
activity_keep_status.xml
android:duration="1500"
android:fromYDelta="0"
android:toYDelta="10%p"/>
android:fromAlpha="1.0"
android:toAlpha="0.8"
android:duration ="1500"/>
然后在Activity的代碼中,用 overridePendingTransition(int enterAnim, int exitAnim) 方法調用這三個文件。
在啟動 Activity 的地方:
public static void startMe(Activity activity, String title, ArrayList imageList, int position) {
Intent intent = new Intent(activity, PhotoViewGroupActivity.class);
intent.putExtra(TAG_IMAGE_LIST, imageList);
intent.putExtra(TAG_POSITION, position);
intent.putExtra(TAG_TITLE, title);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.activity_photo_center_to_edge, R.anim.activity_photo_keep_status);
}
在退出 Activity 的地方:
@Override
public void finish() {
super.finish();
overridePendingTransition(0, R.anim.activity_photo_edge_to_center);
}
關于overridePendingTransition(enterAnim,exitAnim)方法
關于此方法,我們可以點進去看一下源碼的注釋怎么說。
/**
* Call immediately after one of the flavors of {@link #startActivity(Intent)}
* or {@link #finish} to specify an explicit transition animation to
* perform next.
*
*
As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
* to using this with starting activities is to supply the desired animation
* information through a {@link ActivityOptions} bundle to
* {@link #startActivity(Intent, Bundle)} or a related function. This allows
* you to specify a custom animation even when starting an activity from
* outside the context of the current top activity.
*
* @param enterAnim A resource ID of the animation resource to use for
* the incoming activity. Use 0 for no animation.
* @param exitAnim A resource ID of the animation resource to use for
* the outgoing activity. Use 0 for no animation.
*/
public void overridePendingTransition(int enterAnim, int exitAnim) {
try {
ActivityManager.getService().overridePendingTransition(
mToken, getPackageName(), enterAnim, exitAnim);
} catch (RemoteException e) {
}
}
根據此處的說明,第一個參數 enterAnim 是指即將要到來的 Activity 的動畫資源文件,第二個參數exitAnim是即將要離開 Activity 的動畫資源文件。兩個參數都可以傳入0作為無動畫。
關于
總結
以上是生活随笔為你收集整理的android 电视关闭动画,Activity 展开和关闭动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android删除文件夹代码,Andro
- 下一篇: 如何把html转换成音频网址,怎么把视频