安卓activity和fragment之间切换动作--左右滑动的效果

来源:互联网 发布:php在线手册 编辑:程序博客网 时间:2024/05/05 10:35

     一开始我给activity定义了切换动作,不知道为什么效果一直没出来,切换动作都是安卓默认的。后来问了问别人,改了改终于好了,为什么我也不知道。但是如果大家遇到activity切换的问题的话,可以直接套用我这个style样式,代码如下:

下面是通过定义style布局文件的方式来添加动作,但是这样会有一个问题,假如你按下了桌面键退出应用的时候,主屏幕会有你应用滑动的画面,这样会让用户非常不爽,大家可以按照style的方式添加一下试试

1.这是个style的文件:

<style name="ThemeMain" parent="android:Theme">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@null</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowAnimationStyle">@style/style1</item>
</style>
<style name="style1" parent="@android:style/Animation.Translucent">
    <item name="android:windowEnterAnimation">@anim/enter_from_left</item>
    <item name="android:windowExitAnimation">@anim/exit_to_left</item>
</style>
2.这个是enter_from_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="250" />
</set>
3.这个是exit_to_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="250" />
</set>
4.这个manifest.xml的配置文件:

<activity
    android:name=".main.MainActivity"
    android:theme="@style/ThemeMain">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
5.这个是enter_from_right.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="250" />
</set>
6.这个是exit_to_right.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="250" />
</set>

正确的方法应该是用overridePendingTransition(int,int)方法

1>从activity切换到activity的写法:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
//新的activity进入动画,旧的activity退出的动画
overridePendingTransition(R.anim.enter_from_left, R.anim.exit_to_right);
2>从fragment切换到activity的写法:
     Intent intent = new Intent();
intent.setAction("android.intent.action.LIBDETAIL");
startActivity(intent);
getActivity().overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);


0 0
原创粉丝点击