1. 程式人生 > >Xamarin仿微信Activity動畫切換效果實現

Xamarin仿微信Activity動畫切換效果實現

1.在Resources——values下新建styles.xml,新增內容如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="@android:style/Theme">     
        <item name="android:windowNoTitle">true</item> <!-- 設定沒有標題 -->
        <item name="android:textSize">20sp</item>   <!-- 設定文字大小 -->
          <item name="android:textColor">#F34141</item>
        <item name="android:background">@android:color/background_light</item>
        <!-- 設定activity切換動畫 -->
        <item name="android:windowAnimationStyle">@style/activityAnimation</item>
    </style>
    <!-- animation 樣式 -->
    <style name="activityAnimation" parent="@android:style/Animation">
        <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item>
      <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item>
        <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item>
        <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item>
    </style>
</resources>

2.在Resources下新建資料夾anim,在這個新建的資料夾下面新建四個檔案,分別是

(1)slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:duration="300" android:fromXDelta="-100.0%p" android:toXDelta="0.0" />
</set>

(2)slide_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:duration="300" android:fromXDelta="0.0" android:toXDelta="-100.0%p" />
</set>

(3)slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:duration="300" android:fromXDelta="100.0%p" android:toXDelta="0.0" />
</set>

(4)slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:duration="300" android:fromXDelta="0.0" android:toXDelta="100.0%p" />
</set>

3.修改AndroidManifest.xml檔案(紅色標註的部分)

<application android:theme="@style/AppTheme"></application>
4.在MainActivity.cs中的切換按鈕的程式碼:

            button.Click += delegate
            {
                Intent intent = new Intent();
                intent.SetClass(this, typeof(Activity2));
                StartActivity(intent);
                OverridePendingTransition(Resource.Animation.slide_left_in, Resource.Animation.slide_right_out);
            };