1. 程式人生 > >Android實現Activity介面進入動畫

Android實現Activity介面進入動畫

下面介紹的是實現右進右出

一、在res下新建一個anim資料夾(有了就不用新建了)

1、新建in_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromXDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toXDelta="0%p" >

</translate>

2、新建out_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromXDelta="0%p"
    android:toXDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator" >

</translate>

二、在Activity操作使用介紹

1、進入操作

這個的實現是其實可以新建一個BaseActvity然後寫入統一的跳轉方法 然後每個activity直接可以使用的

 startActivity(new Intent(getActivity(), NetClosedActivity.class));
 overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);

下面的程式碼就是在BaseActivity中實現的公用的方法 

下面這塊還做了跳轉的時候雙擊的處理

    /**
     * 跳轉activity
     *
     * @param clz the clz
     */
    public void startActivity(Class<?> clz) {
        if (isFastDoubleClick()) {
            startActivity(clz, null);
            overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);
        }
    }
   /**
     * Is fast double click boolean.
     *
     * @return the boolean
     */
    public boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 800) {
            return false;
        }
        lastClickTime = time;
        return true;

    }

 下面的方法是上面呼叫的

    /**
     * 含有Bundle通過Class跳轉介面
     *
     * @param cls    the cls
     * @param bundle the bundle
     */
    protected void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

2、退出操作

   @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
    }

三、Fragment中的操作流程

下面這個就是要加個getActivity

                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);

 四、問題

可能你會問我為啥不在style中設定他的

windowEnterAnimation
windowExitAnimation

這倆我用了 有問題,一會顯示動畫一會不顯示的,手機不一樣還有問題,經過實踐得到的就是上面講述的操作