首先,您可以使用一个包含两个的活动
ViewGroup
. 至于你的问题,你可以使用
Animation
和
FrameLayout
其中包含两个
RelativeLayout
要完成您想要的内容,请执行以下操作:
主要活动。axml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:a="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl2"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/holo_green_dark">
<Button
android:id="@+id/bt2"
android:text="page2"
android:layout_height="100dp"
android:layout_width="100dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/holo_red_dark">
<Button
android:id="@+id/bt1"
android:text="page1"
android:layout_height="100dp"
android:layout_width="100dp" />
</RelativeLayout>
</FrameLayout>
主要活动:
[Activity(Label = "Aniamtion", MainLauncher = true)]
public class MainActivity : Activity
{
Button bt1,bt2;
RelativeLayout rl1, rl2;
int width;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
getScreenWidth();//get the screen's width, so we can define how much the animation can translate
initView();
initListener();
}
private void getScreenWidth()
{
DisplayMetrics dm = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(dm);
width = dm.WidthPixels;
}
private void initListener()
{
bt1.Click += toPage1;
bt2.Click += toPage2;
}
private void toPage1(object sender, EventArgs e)
{
//use ObjectAnimator to complete it
ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(rl1, "translationX", 0, -width);
objectAnimator.SetDuration(500);
objectAnimator.Start();
ObjectAnimator objectAnimator1 = ObjectAnimator.OfFloat(rl2, "translationX", width, 0);
objectAnimator1.SetDuration(500);
objectAnimator1.Start();
}
private void toPage2(object sender, EventArgs e)
{
ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(rl1, "translationX", -width, 0);
objectAnimator.SetDuration(500);
objectAnimator.Start();
ObjectAnimator objectAnimator1 = ObjectAnimator.OfFloat(rl2, "translationX", 0, width);
objectAnimator1.SetDuration(500);
objectAnimator1.Start();
}
private void initView()
{
bt1 = FindViewById<Button>(Resource.Id.bt1);
bt2 = FindViewById<Button>(Resource.Id.bt2);
rl1 = FindViewById<RelativeLayout>(Resource.Id.rl1);
rl2 = FindViewById<RelativeLayout>(Resource.Id.rl2);
}
}
第二,关于这个
OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft)
方法,可以使用的参数
System resource
,或者你可以
custom it
.
更新:
将其放入您的资源中->动画->向右滑动,
this is about customing animation in Xamarin.Android, it is same as Android
和
this is document which you can refer to
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate android:fromXDelta="50%p"
android:toXDelta="0"
android:duration="300"/>
</set>