代码之家  ›  专栏  ›  技术社区  ›  Entretoize

在xamarin/android中,如何使两个布局在按钮点击上滑动?

  •  0
  • Entretoize  · 技术社区  · 8 年前

    我从xamarin和android开始,我不确定该怎么做。 然后,我创建了一个主活动和一个第二活动,以及它们各自的布局。 在第一个按钮中,我放置了一个带有该代码的按钮:

    button.Click += (sender, e) =>
    {
    var intent = new Android.Content.Intent(this, typeof(Activity1));
    StartActivity(intent);
    OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft);
    };
    

    在第二种情况下:

    button.Click += (sender, e) =>
    {
    Finish();
    };
    

    但这并不是我想要的,我想要两个布局,两张幻灯片,就像并排的一样,但在这里,第二个布局只是在第一个上滑动。它也来自左边,如果我希望它来自右边,没有 SlideInright . 使用两个活动也是正确的吗?我不能只有一个活动和两个布局(视图?)。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Community Mohan Dere    5 年前

    首先,您可以使用一个包含两个的活动 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>